Class: Puddle::HttpAdapter
- Inherits:
-
Object
- Object
- Puddle::HttpAdapter
- 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
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Class Method Summary collapse
Instance Method Summary collapse
- #accept_encoding_header ⇒ Object
- #accept_header ⇒ Object
- #api(method, endpoint, options = {}) ⇒ Object
- #authorization_header ⇒ Object
- #build_headers(headers = {}) ⇒ Object
- #content_type_header ⇒ Object
-
#initialize(api_token, options = {}) ⇒ HttpAdapter
constructor
A new instance of HttpAdapter.
- #user_agent_header ⇒ Object
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, = {}) @token = api_token @headers = build_headers([:headers] || {}) @timeout = [:timeout] || 10 end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
9 10 11 |
# File 'lib/puddle/http_adapter.rb', line 9 def headers @headers end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
9 10 11 |
# File 'lib/puddle/http_adapter.rb', line 9 def timeout @timeout end |
#token ⇒ Object (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_url ⇒ Object
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_header ⇒ Object
41 42 43 |
# File 'lib/puddle/http_adapter.rb', line 41 def accept_encoding_header 'gzip, deflate' end |
#accept_header ⇒ Object
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, = {}) headers = build_headers(['headers'] || {}) result = RestClient::Request.execute( method: method, url: self.class.endpoint_url(endpoint), headers: headers, timeout: timeout ) return result if ['format'] == 'json' JSON.parse(result) end |
#authorization_header ⇒ Object
33 34 35 |
# File 'lib/puddle/http_adapter.rb', line 33 def "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' => , 'Content-Type' => content_type_header, 'User-Agent' => user_agent_header, } defaults.merge(headers) end |
#content_type_header ⇒ Object
25 26 27 |
# File 'lib/puddle/http_adapter.rb', line 25 def content_type_header 'application/json' end |
#user_agent_header ⇒ Object
37 38 39 |
# File 'lib/puddle/http_adapter.rb', line 37 def user_agent_header 'Puddle' end |