Class: Holistics::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/holistics/client.rb

Constant Summary collapse

DEFAULT_ENDPOINT =
'https://api.holistics.io'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, token) ⇒ Client

Returns a new instance of Client.


9
10
11
12
13
14
15
# File 'lib/holistics/client.rb', line 9

def initialize(endpoint, token)
  @conn = Faraday.new(:url => (endpoint || DEFAULT_ENDPOINT)) do |faraday|
    faraday.request :url_encoded             # form-encode POST params
    faraday.adapter Faraday.default_adapter  # make requests with Net::HTTP
  end
  @token = token
end

Instance Method Details

#delete(path, options = {}) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/holistics/client.rb', line 54

def delete(path, options = {})
  options[:params] ||= {}
  options[:headers] ||= {}
  options[:headers].merge!('Content-Type': 'application/json')
  response = @conn.delete do |req|
    req.url path
    req.headers.merge!(options[:headers])
    req.body = options[:params].merge({ '_utoken': @token }).to_s
  end
  [response.status, response.body]
end

#get(path, options = {}) ⇒ Object


17
18
19
20
21
22
23
24
25
26
# File 'lib/holistics/client.rb', line 17

def get(path, options = {})
  options[:params] ||= {}
  options[:headers] ||= {}
  response = @conn.get do |req|
    req.url path
    req.headers.merge(options[:headers])
    req.params = options[:params].merge({ '_utoken': @token })
  end
  [response.status, response.body]
end

#post(path, options = {}) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/holistics/client.rb', line 41

def post(path, options = {})
  options[:params] ||= {}
  options[:headers] ||= {}
  options[:headers].merge!('Content-Type': 'application/json')
  response = @conn.post do |req|
    req.url path
    req.headers.merge!(options[:headers])
    req.params['_utoken'] = @token
    req.body = options[:params].to_s
  end
  [response.status, response.body]
end

#put(path, options = {}) ⇒ Object


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/holistics/client.rb', line 28

def put(path, options = {})
  options[:params] ||= {}
  options[:headers] ||= {}
  options[:headers].merge!('Content-Type': 'application/json')
  response = @conn.put do |req|
    req.url path
    req.headers.merge!(options[:headers])
    req.params['_utoken'] = @token
    req.body = options[:params].to_s
  end
  [response.status, response.body]
end