Class: Puddle::HttpAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/puddle/http_adapter.rb

Constant Summary collapse

DO_API_BASE =
'https://api.digitalocean.com'
DO_API_VERSION =
'v2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_token, options = {}) ⇒ HttpAdapter

Returns a new instance of HttpAdapter.



19
20
21
22
23
# File 'lib/puddle/http_adapter.rb', line 19

def initialize(api_token, options = {})
  @token   = api_token
  @headers = build_headers(options[:headers] || {})
  @timeout = options[:timeout] || 10
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



9
10
11
# File 'lib/puddle/http_adapter.rb', line 9

def headers
  @headers
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



9
10
11
# File 'lib/puddle/http_adapter.rb', line 9

def timeout
  @timeout
end

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/puddle/http_adapter.rb', line 9

def token
  @token
end

Class Method Details

.digital_ocean_api_urlObject



11
12
13
# File 'lib/puddle/http_adapter.rb', line 11

def self.digital_ocean_api_url
  "#{DO_API_BASE}/#{DO_API_VERSION}"
end

.endpoint_url(endpoint) ⇒ Object



15
16
17
# File 'lib/puddle/http_adapter.rb', line 15

def self.endpoint_url(endpoint)
  "#{digital_ocean_api_url}/#{endpoint}"
end

Instance Method Details

#accept_encoding_headerObject



41
42
43
# File 'lib/puddle/http_adapter.rb', line 41

def accept_encoding_header
  'gzip, deflate'
end

#accept_headerObject



29
30
31
# File 'lib/puddle/http_adapter.rb', line 29

def accept_header
  '*/*; q=0.5, application/json'
end

#api(method, endpoint, options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/puddle/http_adapter.rb', line 57

def api(method, endpoint, options = {})
  headers = build_headers(options['headers'] || {})
  result = RestClient::Request.execute(
    method: method,
    url: self.class.endpoint_url(endpoint),
    headers: headers,
    timeout: timeout
  )

  return result if options['format'] == 'json'
  JSON.parse(result)
end

#authorization_headerObject



33
34
35
# File 'lib/puddle/http_adapter.rb', line 33

def authorization_header
  "Bearer #{token}"
end

#build_headers(headers = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/puddle/http_adapter.rb', line 45

def build_headers(headers = {})
  defaults = {
    'Accept'          => accept_header,
    'Accept-Encoding' => accept_encoding_header,
    'Authorization'   => authorization_header,
    'Content-Type'    => content_type_header,
    'User-Agent'      => user_agent_header,
  }

  defaults.merge(headers)
end

#content_type_headerObject



25
26
27
# File 'lib/puddle/http_adapter.rb', line 25

def content_type_header
  'application/json'
end

#user_agent_headerObject



37
38
39
# File 'lib/puddle/http_adapter.rb', line 37

def user_agent_header
  'Puddle'
end