class Puddle::HttpAdapter

Constants

DO_API_BASE
DO_API_VERSION

Attributes

headers[R]
timeout[R]
token[R]

Public Class Methods

digital_ocean_api_url() click to toggle source
# File lib/puddle/http_adapter.rb, line 11
def self.digital_ocean_api_url
  "#{DO_API_BASE}/#{DO_API_VERSION}"
end
endpoint_url(endpoint) click to toggle source
# File lib/puddle/http_adapter.rb, line 15
def self.endpoint_url(endpoint)
  "#{digital_ocean_api_url}/#{endpoint}"
end
new(api_token, options = {}) click to toggle source
# 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

Public Instance Methods

accept_encoding_header() click to toggle source
# File lib/puddle/http_adapter.rb, line 41
def accept_encoding_header
  'gzip, deflate'
end
accept_header() click to toggle source
# File lib/puddle/http_adapter.rb, line 29
def accept_header
  '*/*; q=0.5, application/json'
end
api(method, endpoint, options = {}) click to toggle source
# 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_header() click to toggle source
# File lib/puddle/http_adapter.rb, line 33
def authorization_header
  "Bearer #{token}"
end
build_headers(headers = {}) click to toggle source
# 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_header() click to toggle source
# File lib/puddle/http_adapter.rb, line 25
def content_type_header
  'application/json'
end
user_agent_header() click to toggle source
# File lib/puddle/http_adapter.rb, line 37
def user_agent_header
  'Puddle'
end