Module: Helium::Client::Http

Included in:
Helium::Client
Defined in:
lib/helium/client/http.rb

Constant Summary

BASE_HTTP_HEADERS =
{
  'User-Agent'    => 'helium-ruby'
}

Instance Method Summary collapse

Instance Method Details

#base_urlObject



63
64
65
66
67
# File 'lib/helium/client/http.rb', line 63

def base_url
  url = "#{PROTOCOL}://#{@api_host}"
  url += "/#{@api_version}" if @api_version
  url
end

#delete(path, opts = {}) ⇒ Object



32
33
34
35
# File 'lib/helium/client/http.rb', line 32

def delete(path, opts = {})
  response = run(path, :delete, opts)
  response.code == 204
end

#get(path, opts = {}) ⇒ Object



8
9
10
# File 'lib/helium/client/http.rb', line 8

def get(path, opts = {})
  run(path, :get, opts)
end

#paginated_get(path, opts = {}) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/helium/client/http.rb', line 12

def paginated_get(path, opts = {})
  klass  = opts.fetch(:klass)
  cursor_klass  = opts.fetch(:cursor_klass, Helium::Cursor)
  params = opts.fetch(:params, {})

  cursor_klass.new(client: self, path: path, klass: klass, params: params)
end

#patch(path, opts = {}) ⇒ Object



24
25
26
# File 'lib/helium/client/http.rb', line 24

def patch(path, opts = {})
  run(path, :patch, opts)
end

#post(path, opts = {}) ⇒ Object



20
21
22
# File 'lib/helium/client/http.rb', line 20

def post(path, opts = {})
  run(path, :post, opts)
end

#put(path, opts = {}) ⇒ Object



28
29
30
# File 'lib/helium/client/http.rb', line 28

def put(path, opts = {})
  run(path, :put, opts)
end

#stream_from(path, opts = {}) {|Helium::Resource| ... } ⇒ Object

Stream data from the provided path

Parameters:

  • path (String)

    a relative path

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :klass (Class)

    a class to be initialized with received data

  • :params (Hash)

    a hash of params to be used as query params

Yields:

  • (Helium::Resource)

    accepts logic to perform on the initialized Helium::Resource class



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/helium/client/http.rb', line 42

def stream_from(path, opts = {}, &block)
  klass = opts.fetch(:klass)
  params = opts.fetch(:params, {})
  request = generate_request(path, {
    method: :get,
    content_type: :stream,
    params: params
  })

  request.on_body do |chunk|
    if chunk =~ /data:/
      json_string = chunk[chunk.index('{')..chunk.rindex('}')]
      json_data = JSON.parse(json_string)["data"]
      object = klass.new(client: self, params: json_data)
      yield object
    end
  end

  run_request(request)
end

#url_for(path) ⇒ Object

Contructs a proper url given a path. If the path is already a full url it will simply pass through



71
72
73
74
75
76
# File 'lib/helium/client/http.rb', line 71

def url_for(path)
  return path if path =~ /^http/

  path = path.gsub(/^\//, '')
  "#{base_url}/#{path}"
end