Class: HttpWrapper

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Parameters:

  • value

    the value to set the attribute endpoint to.



10
11
12
# File 'lib/datagun/concerns/http_wrapper.rb', line 10

def endpoint=(value)
  @endpoint = value
end

#payloadObject

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

#deleteObject



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.message }
end

#getObject



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.message }
end

#postObject



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.message }
end

#urlObject



37
38
39
# File 'lib/datagun/concerns/http_wrapper.rb', line 37

def url
  "#{@base_url}/#{@endpoint}"
end