Sha256: 417a3e43db384c04057e70f3a1ad187c5fbc6e4326bd0f66bceb29b0a387fd5f

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require 'httparty'
require 'json'

module Messenger

  class Campfire

    def self.valid_url?(url)
      !!matcher(url)
    rescue NoMethodError
      false
    end

    # URL format:
    #     campfire://api-key:room-id@subdomain.campfirenow.com
    def self.send(url, body, options={})
      raise URLError, "The URL provided is invalid" unless valid_url?(url)
      api_key, room, subdomain = matcher(url)
      response = HTTParty.post(
        "http://#{subdomain}.campfirenow.com/room/#{room}/speak.json",
        :basic_auth => { :username => api_key, :password => "x" },
        :headers => { "Content-Type" => "application/json" },
        :body => { "message" => { "body" => body } }.to_json
      )
      Result.new(success?(response), response)
    end

    def self.obfuscate(url)
      raise URLError, "The URL provided is invalid" unless valid_url?(url)
      api_key, room, subdomain = matcher(url)
      "campfire://xxxx:#{room}@#{subdomain}.campfirenow.com"
    end


  private

    def self.matcher(url)
      url.match(/^campfire:\/\/([^:]+):([^@]+)@([^\.]+).campfirenow.com/)[1,3]
    end

    def self.success?(response)
      case response.code
      when 200, 201: true
      else
        false
      end
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
messenger-0.1.1 lib/messenger/campfire.rb