Parent

Files

Class/Module Index [+]

Quicksearch

Marshmallow::Connection

Public Class Methods

new(options) click to toggle source
# File lib/god/contacts/campfire.rb, line 20
def initialize(options)
  raise "Required option :subdomain not set." unless options[:subdomain]
  raise "Required option :token not set." unless options[:token]
  @options = options
end

Public Instance Methods

base_url() click to toggle source
# File lib/god/contacts/campfire.rb, line 26
def base_url
  scheme = @options[:ssl] ? 'https' : 'http'
  subdomain = @options[:subdomain]
  "#{scheme}://#{subdomain}.campfirenow.com"
end
find_room_id_by_name(room) click to toggle source
# File lib/god/contacts/campfire.rb, line 32
def find_room_id_by_name(room)
  url = URI.parse("#{base_url}/rooms.json")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true if @options[:ssl]

  req = Net::HTTP::Get.new(url.path)
  req.basic_auth(@options[:token], 'X')

  res = http.request(req)
  case res
    when Net::HTTPSuccess
      rooms = JSON.parse(res.body)
      room = rooms['rooms'].select { |x| x['name'] == room }
      rooms.empty? ? nil : room.first['id']
    else
      raise res.error!
  end
end
speak(room, message) click to toggle source
# File lib/god/contacts/campfire.rb, line 52
def speak(room, message)
  room_id = find_room_id_by_name(room)
  raise "No such room: #{room}." unless room_id

  url = URI.parse("#{base_url}/room/#{room_id}/speak.json")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true if @options[:ssl]

  req = Net::HTTP::Post.new(url.path)
  req.basic_auth(@options[:token], 'X')
  req.set_content_type('application/json')
  req.body = { 'message' => { 'body' => message } }.to_json

  res = http.request(req)
  case res
    when Net::HTTPSuccess
      true
    else
      raise res.error!
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.