Class: BooticClient::Client

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

Constant Summary collapse

USER_AGENT =
"[BooticClient v#{VERSION}] Ruby-#{RUBY_VERSION} - #{RUBY_PLATFORM}".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_root, options = {}, &block) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bootic_client/client.rb', line 15

def initialize(api_root, options = {}, &block)
  @api_root = api_root
  @options = {
    access_token: nil,
    logging: false
  }.merge(options.dup)

  @options[:cache_store] = @options[:cache_store] || Faraday::HttpCache::MemoryStore.new

  conn &block if block_given?
end

Instance Attribute Details

#api_root ⇒ Object (readonly)

Returns the value of attribute api_root.



13
14
15
# File 'lib/bootic_client/client.rb', line 13

def api_root
  @api_root
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/bootic_client/client.rb', line 13

def options
  @options
end

Instance Method Details

#get(href, query = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bootic_client/client.rb', line 31

def get(href, query = {})
  validate_request!

  resp = conn.get do |req|
    req.url href
    req.params.update(query)
    req.headers['Authorization'] = "Bearer #{options[:access_token]}"
    req.headers['User-Agent'] = USER_AGENT
  end

  raise_if_invalid! resp

  resp
end

#get_and_wrap(href, wrapper_class, query = {}) ⇒ Object



27
28
29
# File 'lib/bootic_client/client.rb', line 27

def get_and_wrap(href, wrapper_class, query = {})
  wrapper_class.new get(href, query).body, self
end