Class: Uktt::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/uktt/http.rb

Overview

An object for handling network requests

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = nil, version = nil, debug = false) ⇒ Http

Returns a new instance of Http.



7
8
9
10
11
12
13
14
# File 'lib/uktt/http.rb', line 7

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

Class Method Details

.api_hostObject



36
37
38
39
40
# File 'lib/uktt/http.rb', line 36

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

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

.spec_versionObject



30
31
32
33
34
# File 'lib/uktt/http.rb', line 30

def spec_version
  return @version unless @version.nil?

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

.use_productionObject



26
27
28
# File 'lib/uktt/http.rb', line 26

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

Instance Method Details

#retrieve(resource, return_json = false) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/uktt/http.rb', line 16

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