Class: WCC::JsonAPI::BaseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/json_api/base_client.rb,
lib/wcc/json_api/base_client/api_error.rb,
lib/wcc/json_api/base_client/http_adapter.rb,
lib/wcc/json_api/base_client/base_response.rb

Defined Under Namespace

Classes: ApiError, BaseResponse, HttpAdapter, NotFoundError, TyphoeusAdapter

Constant Summary collapse

ADAPTERS =
{
  faraday: ['faraday', '~> 0.9'],
  typhoeus: ['typhoeus', '~> 1.0']
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_url:, headers: nil, **options) ⇒ BaseClient

Returns a new instance of BaseClient.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wcc/json_api/base_client.rb', line 12

def initialize(api_url:, headers: nil, **options)
  # normalizes a URL to have a slash on the end
  @api_url = api_url.gsub(/\/+$/, '') + '/'

  @adapter = BaseClient.load_adapter(options[:adapter])

  @options = options
  @query_defaults = {}
  @headers = {
    'Accept' => 'application/json'
  }.merge(headers || {}).freeze
  @response_class = options[:response_class] || BaseResponse
end

Instance Attribute Details

#api_urlObject (readonly)

Returns the value of attribute api_url.



10
11
12
# File 'lib/wcc/json_api/base_client.rb', line 10

def api_url
  @api_url
end

Class Method Details

.load_adapter(adapter) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/wcc/json_api/base_client.rb', line 70

def self.load_adapter(adapter)
  case adapter
  when nil
    ADAPTERS.each do |a, spec|
      begin
        gem(*spec)
        return load_adapter(a)
      rescue Gem::LoadError
        next
      end
    end
    raise ArgumentError, 'Unable to load adapter!  Please install one of '\
      "#{ADAPTERS.values.map(&:join).join(',')}"
  when :faraday
    require 'faraday'
    ::Faraday.new do |faraday|
      faraday.response :logger, (Rails.logger if defined?(Rails)), { headers: false, bodies: false }
      faraday.adapter :net_http
    end
  when :typhoeus
    require_relative 'base_client/typhoeus_adapter'
    TyphoeusAdapter.new
  else
    unless adapter.respond_to?(:get)
      raise ArgumentError, "Adapter #{adapter} is not invokeable!  Please "\
        "pass use one of #{ADAPTERS.keys} or create a Faraday-compatible adapter"
    end
    adapter
  end
end

Instance Method Details

#delete(path) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/wcc/json_api/base_client.rb', line 57

def delete(path)
  url = URI.join(@api_url, path)

  @response_class.new(self,
    { url: url },
    delete_http(url))
end

#get(path, query = {}) ⇒ Object

performs an HTTP GET request to the specified path within the configured space and environment. Query parameters are merged with the defaults and appended to the request.



29
30
31
32
33
34
35
# File 'lib/wcc/json_api/base_client.rb', line 29

def get(path, query = {})
  url = URI.join(@api_url, path)

  @response_class.new(self,
    { url: url, query: query },
    get_http(url, query))
end

#post(path, body = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/wcc/json_api/base_client.rb', line 37

def post(path, body = {})
  url = URI.join(@api_url, path)

  @response_class.new(self,
    { url: url },
    post_http(url,
      body.to_json,
      headers: { 'Content-Type': 'application/json' }))
end

#put(path, body = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/wcc/json_api/base_client.rb', line 47

def put(path, body = {})
  url = URI.join(@api_url, path)

  @response_class.new(self,
    { url: url },
    put_http(url,
      body.to_json,
      headers: { 'Content-Type': 'application/json' }))
end