Sha256: 0b1c4e77d4a8db6c1e92859c510bb1540a8e72b78aa14b1ee13cb6d043f381f7

Contents?: true

Size: 1.47 KB

Versions: 14

Compression:

Stored size: 1.47 KB

Contents

require 'httparty'

module ForestLiana
  class ForestApiRequester
    def self.get(route, query: nil, headers: {})
      begin
        HTTParty.get("#{forest_api_url}#{route}", {
          :verify => Rails.env.production?,
          headers: base_headers.merge(headers),
          query: query,
        }).response
      rescue => error
        FOREST_LOGGER.error "Cannot reach Forest API at #{forest_api_url}#{route}, it seems to be down right now."
        FOREST_REPORTER.report error
        raise error
      end
    end

    def self.post(route, body: nil, query: nil, headers: {})
      begin
        if route.start_with?('https://')
          post_route = route
        else
          post_route = "#{forest_api_url}#{route}"
        end

        HTTParty.post(post_route, {
          :verify => Rails.env.production?,
          headers: base_headers.merge(headers),
          query: query,
          body: body.to_json,
        }).response
      rescue => error
        FOREST_LOGGER.error "Cannot reach Forest API at #{post_route}, it seems to be down right now."
        FOREST_REPORTER.report error
        raise error
      end
    end

    private

    def self.base_headers
      base_headers = {
        'Content-Type' => 'application/json',
      }
      base_headers['forest-secret-key'] = ForestLiana.env_secret if !ForestLiana.env_secret.nil?
      return base_headers
    end

    def self.forest_api_url
      ENV['FOREST_URL'] || 'https://api.forestadmin.com'
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
forest_liana-9.11.1 app/services/forest_liana/forest_api_requester.rb
forest_liana-9.11.0 app/services/forest_liana/forest_api_requester.rb
forest_liana-9.10.6 app/services/forest_liana/forest_api_requester.rb
forest_liana-9.10.5 app/services/forest_liana/forest_api_requester.rb
forest_liana-9.10.4 app/services/forest_liana/forest_api_requester.rb
forest_liana-9.10.3 app/services/forest_liana/forest_api_requester.rb
forest_liana-9.10.2 app/services/forest_liana/forest_api_requester.rb
forest_liana-9.10.1 app/services/forest_liana/forest_api_requester.rb
forest_liana-9.10.0 app/services/forest_liana/forest_api_requester.rb
forest_liana-9.9.1 app/services/forest_liana/forest_api_requester.rb
forest_liana-9.9.0 app/services/forest_liana/forest_api_requester.rb
forest_liana-9.8.0 app/services/forest_liana/forest_api_requester.rb
forest_liana-9.7.0 app/services/forest_liana/forest_api_requester.rb
forest_liana-9.6.4 app/services/forest_liana/forest_api_requester.rb