Class: Firering::Connection
- Inherits:
-
Object
- Object
- Firering::Connection
- Includes:
- Requests
- Defined in:
- lib/firering/connection.rb
Defined Under Namespace
Classes: HTTPError
Instance Attribute Summary (collapse)
-
- (Object) host
Returns the value of attribute host.
-
- (Object) logger
Returns the value of attribute logger.
-
- (Object) login
Returns the value of attribute login.
-
- (Object) max_retries
Returns the value of attribute max_retries.
-
- (Object) password
Returns the value of attribute password.
-
- (Object) performed_retries
readonly
Returns the value of attribute performed_retries.
-
- (Object) redirects
Returns the value of attribute redirects.
-
- (Object) retry_delay
Returns the value of attribute retry_delay.
-
- (Object) streaming_host
Returns the value of attribute streaming_host.
-
- (Object) token
Returns the value of attribute token.
Instance Method Summary (collapse)
- - (Object) auth_headers
- - (Object) http(method, path, data = nil, &callback)
-
- (Connection) initialize(host, streaming_host = "https://streaming.campfirenow.com")) {|_self| ... }
constructor
A new instance of Connection.
- - (Boolean) max_retries_reached?
- - (Object) parameters(data = nil)
-
- (Object) stream(room, &callback)
Streaming.
- - (Object) subdomain
Methods included from Requests
#authenticate, #room, #rooms, #search_messages, #star_message, #user
Constructor Details
- (Connection) initialize(host, streaming_host = "https://streaming.campfirenow.com")) {|_self| ... }
A new instance of Connection
17 18 19 20 21 |
# File 'lib/firering/connection.rb', line 17 def initialize(host, streaming_host = "https://streaming.campfirenow.com") @retry_delay, @redirects, @max_retries, @performed_retries = 2, 1, 2, 0 self.host, self.streaming_host = host, streaming_host yield self if block_given? end |
Instance Attribute Details
- (Object) host
Returns the value of attribute host
5 6 7 |
# File 'lib/firering/connection.rb', line 5 def host @host end |
- (Object) logger
Returns the value of attribute logger
10 11 12 |
# File 'lib/firering/connection.rb', line 10 def logger @logger ||= Logger.new(STDOUT) end |
- (Object) login
Returns the value of attribute login
13 14 15 |
# File 'lib/firering/connection.rb', line 13 def login @login end |
- (Object) max_retries
Returns the value of attribute max_retries
7 8 9 |
# File 'lib/firering/connection.rb', line 7 def max_retries @max_retries end |
- (Object) password
Returns the value of attribute password
11 12 13 |
# File 'lib/firering/connection.rb', line 11 def password @password end |
- (Object) performed_retries (readonly)
Returns the value of attribute performed_retries
15 16 17 |
# File 'lib/firering/connection.rb', line 15 def performed_retries @performed_retries end |
- (Object) redirects
Returns the value of attribute redirects
9 10 11 |
# File 'lib/firering/connection.rb', line 9 def redirects @redirects end |
- (Object) retry_delay
Returns the value of attribute retry_delay
8 9 10 |
# File 'lib/firering/connection.rb', line 8 def retry_delay @retry_delay end |
- (Object) streaming_host
Returns the value of attribute streaming_host
6 7 8 |
# File 'lib/firering/connection.rb', line 6 def streaming_host @streaming_host end |
- (Object) token
Returns the value of attribute token
12 13 14 |
# File 'lib/firering/connection.rb', line 12 def token @token end |
Instance Method Details
- (Object) auth_headers
39 40 41 |
# File 'lib/firering/connection.rb', line 39 def auth_headers token ? {'authorization' => [token, "X"] } : {'authorization' => [login, password] } end |
- (Object) http(method, path, data = nil, &callback)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/firering/connection.rb', line 52 def http(method, path, data = nil, &callback) uri = host.join(path) logger.info("performing request to #{uri}") http = EventMachine::HttpRequest.new(uri).send(method, parameters(data)) http.errback do perform_retry(http) do http(method, path, data, &callback) end end http.callback { reset_retries_counter if callback data = Yajl::Parser.parse(http.response, :symbolize_keys => true) rescue Hash.new callback.call(data, http) end } http end |
- (Boolean) max_retries_reached?
118 119 120 |
# File 'lib/firering/connection.rb', line 118 def max_retries_reached? @performed_retries && @performed_retries >= @max_retries end |
- (Object) parameters(data = nil)
43 44 45 46 47 48 49 50 |
# File 'lib/firering/connection.rb', line 43 def parameters(data = nil) parameters = { :redirects => redirects, :head => auth_headers.merge("Content-Type" => "application/json") } parameters.merge!(:body => data.is_a?(String) ? data : Yajl::Encoder.encode(data)) if data parameters end |
- (Object) stream(room, &callback)
Streaming
The Streaming API allows you to monitor a room in real time. The authenticated user must already have joined the room in order to use this API.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/firering/connection.rb', line 80 def stream(room, &callback) parser = Yajl::Parser.new(:symbolize_keys => true) parser.on_parse_complete = proc do |data| callback.call(Firering::Message.instantiate(self, data)) if callback end # timeout 5s because the campfire streaming API chunked connection sends # "1\r\n \r\n" every 3s, and we want to bail as soon as that stops = {:keepalive => true, :timeout => 5} uri = streaming_host.join("/room/#{room.id}/live.json") logger.info("performing streaming request to #{uri.to_s}") http = EventMachine::HttpRequest.new(uri).get(.merge(parameters)) http.stream do |chunk| begin parser << chunk; reset_retries_counter rescue Yajl::ParseError perform_retry(http) do room.stream(&callback) end end end # Campfire servers will try to hold the streaming connections open indefinitely. # However, API clients must be able to handle occasional timeouts or # disruptions. Upon unexpected disconnection, API clients should wait for a # few seconds before trying to reconnect. http.errback do perform_retry(http) do room.stream(&callback) end end http end |
- (Object) subdomain
35 36 37 |
# File 'lib/firering/connection.rb', line 35 def subdomain host.host.split(".").first end |