Class: Tambur::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-tambur.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key, app_id, secret, options = {}) ⇒ Connector

Returns a new instance of Connector.



8
9
10
11
12
13
14
15
# File 'lib/ruby-tambur.rb', line 8

def initialize(api_key, app_id, secret, options = {})
    @app_id = app_id
    @api_host = options[:api_host] || 'api.tambur.io'
    @oauth = Tambur::Util::OAuth.new()
    @oauth.consumer_key=api_key
    @oauth.consumer_secret=secret
    @oauth.req_method='POST'
end

Instance Method Details

#generate_auth_token(stream, subscriber_id) ⇒ Object



33
34
35
# File 'lib/ruby-tambur.rb', line 33

def generate_auth_token(stream, subscriber_id)
    return generate_mode_token('auth', stream, subscriber_id)
end

#generate_direct_token(stream, user_id, subscriber_id) ⇒ Object



41
42
43
# File 'lib/ruby-tambur.rb', line 41

def generate_direct_token(stream, user_id, subscriber_id)
    return generate_mode_token('direct', stream + ':' + user_id, subscriber_id)
end

#generate_presence_token(stream, user_id, subscriber_id) ⇒ Object



37
38
39
# File 'lib/ruby-tambur.rb', line 37

def generate_presence_token(stream, user_id, subscriber_id)
    return generate_mode_token('presence', stream + ':' + user_id, subscriber_id)
end

#publish(stream, message) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby-tambur.rb', line 17

def publish(stream, message)
    path = '/app/' + @app_id + '/stream/' + stream.to_str+'?api_version=1.0&message='+ CGI::escape(message)
    url = URI.parse('http://' + @api_host + path)
    Net::HTTP.start(url.host) do | http |
        request = Net::HTTP::Post.new(url.path)
        request.body = @oauth.sign(url).query_string()
        request['Content-Type'] = 'application/x-www-form-urlencoded'
        response = http.request(request)
        if response.code == '204'
            return true
        else
            raise 'publish error: '+response.read_body
        end
    end
end