Class: Supergood::Api
- Inherits:
-
Object
- Object
- Supergood::Api
- Defined in:
- lib/supergood/api.rb
Instance Method Summary collapse
- #get_remote_config ⇒ Object
- #header_options ⇒ Object
-
#initialize(client_id, client_secret, base_url) ⇒ Api
constructor
A new instance of Api.
- #log ⇒ Object
- #post_errors(payload) ⇒ Object
- #post_events(payload) ⇒ Object
- #set_logger(logger) ⇒ Object
Constructor Details
#initialize(client_id, client_secret, base_url) ⇒ Api
Returns a new instance of Api.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/supergood/api.rb', line 7 def initialize(client_id, client_secret, base_url) @base_url = base_url @header_options = { 'Content-Type' => 'application/json', 'Authorization' => "Basic #{Base64.encode64("#{client_id}:#{client_secret}").gsub(/\n/, '')}", 'supergood-api' => 'supergood-rb', 'supergood-api-version' => VERSION } @local_only = client_id == LOCAL_CLIENT_ID && client_secret == LOCAL_CLIENT_SECRET end |
Instance Method Details
#get_remote_config ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/supergood/api.rb', line 59 def get_remote_config uri = URI(@base_url + '/config') response = Net::HTTP.get_response(uri, @header_options) return JSON.parse(response.body) if response.code == '200' raise SupergoodException.new ERRORS[:CONFIG_FETCH_ERROR] end |
#header_options ⇒ Object
18 19 20 |
# File 'lib/supergood/api.rb', line 18 def @header_options end |
#log ⇒ Object
22 23 24 |
# File 'lib/supergood/api.rb', line 22 def log @log end |
#post_errors(payload) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/supergood/api.rb', line 47 def post_errors(payload) if @local_only @log.debug(payload) else uri = URI("#{@base_url}/errors") response = Net::HTTP.post(uri, payload.to_json, @header_options) return JSON.parse(response.body, symbolize_names: true) if response.code == '200' @log.warn(ERRORS[:POSTING_ERRORS]) end end |
#post_events(payload) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/supergood/api.rb', line 30 def post_events(payload) if @local_only @log.debug(payload) else uri = URI("#{@base_url}/events") response = Net::HTTP.post(uri, payload.to_json, @header_options) return JSON.parse(response.body) if response.code == '200' if response.code == '401' raise SupergoodException.new ERRORS[:UNAUTHORIZED] elsif response.code != '200' && response.code != '201' raise SupergoodException.new ERRORS[:POSTING_EVENTS] end end end |
#set_logger(logger) ⇒ Object
26 27 28 |
# File 'lib/supergood/api.rb', line 26 def set_logger(logger) @log = logger end |