Class: Unit::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/unit-ruby/util/connection.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custom_headers = {}) ⇒ Connection

Returns a new instance of Connection.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/unit-ruby/util/connection.rb', line 13

def initialize(custom_headers = {})
  @connection = Faraday.new(self.class.base_url) do |f|
    f.headers['UNIT_TRUST_TOKEN'] = self.class.trust_token if self.class.trust_token
    f.headers['Authorization'] = "Bearer #{self.class.api_key}"
    f.headers['Content-Type'] = 'application/vnd.api+json'
    f.headers.merge!(custom_headers)
    f.request :json # encode req bodies as JSON
    f.request :retry # retry transient failures
    f.response :json # decode response bodies as JSON
  end
end

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



8
9
10
# File 'lib/unit-ruby/util/connection.rb', line 8

def api_key
  @api_key
end

.base_urlObject

Returns the value of attribute base_url.



8
9
10
# File 'lib/unit-ruby/util/connection.rb', line 8

def base_url
  @base_url
end

.trust_tokenObject

Returns the value of attribute trust_token.



8
9
10
# File 'lib/unit-ruby/util/connection.rb', line 8

def trust_token
  @trust_token
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



11
12
13
# File 'lib/unit-ruby/util/connection.rb', line 11

def connection
  @connection
end

Instance Method Details

#delete(path) ⇒ Object

Executes a DELETE request to the API

Returns:

  • boolean



53
54
55
56
57
58
59
# File 'lib/unit-ruby/util/connection.rb', line 53

def delete(path)
  response = connection.delete(path)

  handle_errors(response)

  from_json_api(response.body)
end

#from_json_api(response_body) ⇒ Object



82
83
84
85
86
# File 'lib/unit-ruby/util/connection.rb', line 82

def from_json_api(response_body)
  response_body.deep_transform_keys do |key|
    key.to_s.underscore.to_sym
  end.fetch(:data)
end

#get(path, params = nil, raw: false) ⇒ Object

Executes a GET request to the API

Returns:

  • the resource (or array of resources) returned from the API



28
29
30
31
32
33
34
# File 'lib/unit-ruby/util/connection.rb', line 28

def get(path, params = nil, raw: false)
  response = connection.get(path, params)

  handle_errors(response)

  raw ? response.body : from_json_api(response.body)
end

#handle_errors(response) ⇒ Object

Raises:



88
89
90
91
92
# File 'lib/unit-ruby/util/connection.rb', line 88

def handle_errors(response)
  return if response.success?

  raise(Error, response.body)
end

#patch(path, data = nil) ⇒ Object

Executes a PATCH request to the API



62
63
64
65
66
67
68
69
70
71
# File 'lib/unit-ruby/util/connection.rb', line 62

def patch(path, data = nil)
  response = connection.patch do |req|
    req.url path
    req.body = data.deep_transform_keys! { |key| key.to_s.camelize(:lower) } if data
  end

  handle_errors(response)

  from_json_api(response.body)
end

#post(path, data = nil) ⇒ Unit::APIResource

Executes a POST request to the API

Returns:



39
40
41
42
43
44
45
46
47
48
# File 'lib/unit-ruby/util/connection.rb', line 39

def post(path, data = nil)
  response = connection.post do |req|
    req.url path
    req.body = data.deep_transform_keys! { |key| key.to_s.camelize(:lower) } if data
  end

  handle_errors(response)

  from_json_api(response.body)
end

#put(path, data = nil) ⇒ Object

Executes a PUT of a JSON object



74
75
76
77
78
79
80
# File 'lib/unit-ruby/util/connection.rb', line 74

def put(path, data = nil)
  response = connection.put(path, data)

  handle_errors(response)

  from_json_api(response.body)
end