Class: Pennylane::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pennylane/client.rb

Constant Summary collapse

BASE_URI =
'app.pennylane.com/api/external'.freeze
VERSION =
'v1'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, version: 'v1') ⇒ Client

Returns a new instance of Client.



8
9
10
11
# File 'lib/pennylane/client.rb', line 8

def initialize(key, version: 'v1')
  @key = key
  @version = version
end

Instance Attribute Details

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#authorization(key) ⇒ Object



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

def authorization(key)
  "Bearer #{key}"
end

#base_uriObject



19
20
21
# File 'lib/pennylane/client.rb', line 19

def base_uri
  URI("https://#{BASE_URI}")
end

#httpObject



27
28
29
30
31
# File 'lib/pennylane/client.rb', line 27

def http
  Net::HTTP.new(base_uri.host, base_uri.port).tap do |http|
    http.use_ssl = true
  end
end

#klass(method) ⇒ Object



33
34
35
# File 'lib/pennylane/client.rb', line 33

def klass(method)
  Net::HTTP.const_get(method.to_s.capitalize)
end

#request(method, path, params: {}, opts: {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/pennylane/client.rb', line 37

def request method, path, params: {}, opts: {}
  req = initialize_request(method, path, params[:query], opts).tap do |req|
    req.body = params[:body].to_json if params[:body]
  end

  http.request(req).tap do |resp|
    handle_error_response(resp) if should_handle_as_error?(resp.code)
  end
end

#url(path, query = {}) ⇒ Object



13
14
15
16
17
# File 'lib/pennylane/client.rb', line 13

def url(path, query={})
  URI("https://#{BASE_URI}/#{VERSION}#{path}").tap do |uri|
    uri.query = URI.encode_www_form(query) if query
  end
end