Class: HttpWrapper
- Inherits:
-
Object
- Object
- HttpWrapper
- Defined in:
- lib/datagun/concerns/http_wrapper.rb
Overview
Decorator class for the RestClient, mainly used to add the headers with authorization
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
writeonly
Sets the attribute endpoint.
-
#payload ⇒ Object
Returns the value of attribute payload.
Instance Method Summary collapse
- #delete ⇒ Object
- #get ⇒ Object
-
#initialize(base_url:) ⇒ HttpWrapper
constructor
A new instance of HttpWrapper.
- #post ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(base_url:) ⇒ HttpWrapper
Returns a new instance of HttpWrapper.
12 13 14 15 16 17 |
# File 'lib/datagun/concerns/http_wrapper.rb', line 12 def initialize(base_url:) @base_url = base_url @headers = { 'Authorization': Datagun.config.api_key } end |
Instance Attribute Details
#endpoint=(value) ⇒ Object (writeonly)
Sets the attribute endpoint
10 11 12 |
# File 'lib/datagun/concerns/http_wrapper.rb', line 10 def endpoint=(value) @endpoint = value end |
#payload ⇒ Object
Returns the value of attribute payload.
9 10 11 |
# File 'lib/datagun/concerns/http_wrapper.rb', line 9 def payload @payload end |
Instance Method Details
#delete ⇒ Object
31 32 33 34 35 |
# File 'lib/datagun/concerns/http_wrapper.rb', line 31 def delete JSON.parse(RestClient.delete(url, { Authorization: Datagun.config.api_key }).body)['data'] rescue StandardError => e { error: e. } end |
#get ⇒ Object
25 26 27 28 29 |
# File 'lib/datagun/concerns/http_wrapper.rb', line 25 def get JSON.parse(RestClient.get(url, { Authorization: Datagun.config.api_key, params: payload }).body)['data'] rescue StandardError => e { error: e. } end |
#post ⇒ Object
19 20 21 22 23 |
# File 'lib/datagun/concerns/http_wrapper.rb', line 19 def post JSON.parse(RestClient.post(url, payload, @headers).body)['data'] rescue StandardError => e { error: e. } end |
#url ⇒ Object
37 38 39 |
# File 'lib/datagun/concerns/http_wrapper.rb', line 37 def url "#{@base_url}/#{@endpoint}" end |