Sha256: b246dd16e38ce90446bb7e234e555a65dce993d63cf958e90f38834e1d70f692

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

require 'faraday'
require 'json'

module Uktt
  # An object for handling network requests
  class Http
    def initialize(host = nil, version = nil, debug = false)
      @host = host || API_HOST_LOCAL
      @version = version || API_VERSION
      @conn = Faraday.new(url: @host) do |faraday|
        faraday.response(:logger) if debug
        faraday.adapter Faraday.default_adapter
      end
    end

    def retrieve(resource, return_json = false)
      json = @conn.get do |request|
        request.url([@version, resource].join('/'))
      end.body
      return json if return_json

      JSON.parse(json, object_class: OpenStruct)
    end

    class << self
      def use_production
        !ENV['PROD'].nil? && ENV['PROD'].casecmp('true').zero?
      end

      def spec_version
        return @version unless @version.nil?

        ENV['VER'] ? ENV['VER'].to_s : 'v1'
      end

      def api_host
        return ENV['HOST'] if ENV['HOST']

        use_production ? Uktt::API_HOST_PROD : Uktt::API_HOST_LOCAL
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
uktt-0.2.15 lib/uktt/http.rb
uktt-0.2.14 lib/uktt/http.rb