Class: SierraApiClient
- Inherits:
-
Object
- Object
- SierraApiClient
- Defined in:
- lib/nypl_sierra_api_client.rb
Instance Method Summary collapse
- #delete(path, options = {}) ⇒ Object
- #get(path, options = {}) ⇒ Object
-
#initialize(config = {}) ⇒ SierraApiClient
constructor
A new instance of SierraApiClient.
- #post(path, body, options = {}) ⇒ Object
- #put(path, body, options = {}) ⇒ Object
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, = {}) = do_request 'delete', path, end |
#get(path, options = {}) ⇒ Object
51 52 53 54 55 |
# File 'lib/nypl_sierra_api_client.rb', line 51 def get (path, = {}) = do_request 'get', path, 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, = {}) = # Default to POSTing JSON unless explicitly stated otherwise [:headers]['Content-Type'] = 'application/json' unless [:headers]['Content-Type'] do_request 'post', path, do |request| request.body = body request.body = request.body.to_json unless [: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, = {}) = # Default to POSTing JSON unless explicitly stated otherwise [:headers]['Content-Type'] = 'application/json' unless [:headers]['Content-Type'] do_request 'put', path, do |request| request.body = body request.body = request.body.to_json unless [:headers]['Content-Type'] != 'application/json' end end |