Class: SierraApiClient

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

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ SierraApiClient

Returns a new instance of SierraApiClient.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nypl_sierra_api_client.rb', line 10

def initialize(config = {})
  config_defaults = {
    env: {
      base_url: 'SIERRA_API_BASE_URL',
      client_id: 'SIERRA_OAUTH_ID',
      client_secret: 'SIERRA_OAUTH_SECRET',
      oauth_url: 'SIERRA_OAUTH_URL'
    },
    static: {
      log_level: 'info'
    }
  }

  @config = config_defaults[:env].map {|k,v| [k, ENV[v]]}.to_h
    .merge config_defaults[:static]
    .merge config

  config_defaults[:env].each do |key, value|
    raise SierraApiClientError.new "Missing config: neither config.#{key} nor ENV.#{value} are set" unless @config[key]
  end

  @retries = 0
end

Instance Method Details

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



45
46
47
48
49
# File 'lib/nypl_sierra_api_client.rb', line 45

def delete (path, options = {})
options = parse_http_options options

do_request 'delete', path, options
end

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



51
52
53
54
55
# File 'lib/nypl_sierra_api_client.rb', line 51

def get (path, options = {})
  options = parse_http_options options

  do_request 'get', path, options
end

#post(path, body, options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nypl_sierra_api_client.rb', line 57

def post (path, body, options = {})
  options = parse_http_options options

  # Default to POSTing JSON unless explicitly stated otherwise
  options[:headers]['Content-Type'] = 'application/json' unless options[:headers]['Content-Type']

  do_request 'post', path, options do |request|
    request.body = body
    request.body = request.body.to_json unless options[:headers]['Content-Type'] != 'application/json'
  end
end

#put(path, body, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/nypl_sierra_api_client.rb', line 34

def put (path, body, options = {})
options = parse_http_options options
  # Default to POSTing JSON unless explicitly stated otherwise
  options[:headers]['Content-Type'] = 'application/json' unless options[:headers]['Content-Type']

  do_request 'put', path, options do |request|
    request.body = body
    request.body = request.body.to_json unless options[:headers]['Content-Type'] != 'application/json'
  end
end