Module: Vayacondios::Client::HttpRead

Includes:
Connection
Included in:
HttpClient
Defined in:
lib/vayacondios/client/http_methods.rb

Instance Method Summary collapse

Methods included from Connection

base_uri, #configure_connection, factory, #http_connection, #organization, #url

Instance Method Details

#events(topic, query = {}) ⇒ Object

Search events



18
19
20
21
22
# File 'lib/vayacondios/client/http_methods.rb', line 18

def events(topic, query = {})
  http_connection.get url('events', topic) do |req|
    req.body = query
  end
end

#get(topic, id = nil) ⇒ Object

Retrieve one stash



6
7
8
# File 'lib/vayacondios/client/http_methods.rb', line 6

def get(topic, id = nil)
  http_connection.get url('stash', topic, id)
end

#get_many(query = {}) ⇒ Object

Search stashes



11
12
13
14
15
# File 'lib/vayacondios/client/http_methods.rb', line 11

def get_many(query = {})
  http_connection.get url('stashes') do |req|
    req.body = query
  end
end

#stream(topic, query = {}, &on_event) ⇒ Object

Stream events only works with net/http



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vayacondios/client/http_methods.rb', line 26

def stream(topic, query = {}, &on_event)
  uri = http_connection.url_prefix
  Net::HTTP.start(uri.host, uri.port) do |http|
    path = File.join(uri.request_uri, url('stream', topic))
    request = Net::HTTP::Get.new(path)
    http.request(request) do |response|
      buffer = ''
      response.read_body do |chunk|
        buffer += chunk
        while line = buffer.slice!(/^[^\n].*\n/)
          on_event.call MultiJson.load(line.strip)
        end
      end
    end
  end
end