Sha256: d71ca660632d272a930da9becb1734cb1b298f350ba3e1c92991aad84f95a585

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

require 'dotenv'

Dotenv.load

module Supergood
  class Api
    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/, '')
      }
      @local_only = client_id == LOCAL_CLIENT_ID && client_secret == LOCAL_CLIENT_SECRET
    end

    def header_options
      @header_options
    end

    def log
      @log
    end

    def set_logger(logger)
      @log = logger
    end

    def post_events(payload)
      if @local_only
        @log.debug(payload)
      else
        uri = URI(@base_url + '/api/events')
        response = Net::HTTP.post(uri, payload.to_json, @header_options)
        if response.code == '200'
          return JSON.parse(response.body, symbolize_names: true)
        elsif response.code == '401'
          raise SupergoodException.new ERRORS[:UNAUTHORIZED]
        elsif response.code != '200' && response.code != '201'
          raise SupergoodException.new ERRORS[:POSTING_EVENTS]
        end
      end
    end

    def post_errors(payload)
      if @local_only
        @log.debug(payload)
      else
        uri = URI(@base_url + '/api/errors')
        response = Net::HTTP.post(uri, payload.to_json, @header_options)
        if response.code == '200'
          return JSON.parse(response.body, symbolize_names: true)
        else
          @log.warn(ERRORS[:POSTING_ERRORS])
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
supergood-0.1.4 lib/supergood/api.rb
supergood-0.1.3 lib/supergood/api.rb
supergood-0.1.2 lib/supergood/api.rb