Class: UriService::Client::Connection

Inherits:
Object
  • Object
show all
Includes:
Requests::CustomFields, Requests::Terms, Requests::Vocabularies
Defined in:
lib/uri_service/client/connection.rb

Constant Summary collapse

BASE_PATH =
'/api/v1'.freeze
JSON_MIME =
'application/json'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Requests::CustomFields

#create_custom_field, #delete_custom_field, #update_custom_field

Methods included from Requests::Vocabularies

#create_vocabulary, #delete_vocabulary, #update_vocabulary, #vocabularies, #vocabulary

Methods included from Requests::Terms

#create_term, #delete_term, #search_terms, #term, #update_term

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/uri_service/client/connection.rb', line 20

def initialize(options = {})
  config(options)

  @connection = Faraday.new(url: url) do |conn|
    conn.request    :url_encoded # form-encode POST params
    conn.token_auth api_key

    # Must be the last middleware, must be set.
    conn.adapter    Faraday.default_adapter
  end
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



15
16
17
# File 'lib/uri_service/client/connection.rb', line 15

def api_key
  @api_key
end

#connectionObject (readonly)

Returns the value of attribute connection.



15
16
17
# File 'lib/uri_service/client/connection.rb', line 15

def connection
  @connection
end

#urlObject (readonly)

Returns the value of attribute url.



15
16
17
# File 'lib/uri_service/client/connection.rb', line 15

def url
  @url
end

Instance Method Details

#form_fieldsObject



49
50
51
52
53
54
# File 'lib/uri_service/client/connection.rb', line 49

def form_fields
  response = request(:get, '/open_api_specification')
  UriService::Client::FormFields.new(response)
rescue StandardError
  raise UriService::Client::Error
end

#request(method, path, body: {}, params: {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/uri_service/client/connection.rb', line 32

def request(method, path, body: {}, params: {})
  response = connection.send(method, "#{BASE_PATH}#{path}") do |r|
    r.headers['Accept'] = JSON_MIME

    unless body.nil? || body.empty?
      r.body = body.to_json
      r.headers['Content-Type'] = JSON_MIME
    end

    params.each { |k, v| r.params[k] = v }
  end

  UriService::Client::Response.new(response)
rescue StandardError
  raise UriService::Client::Error
end