Class: Delighted::Client

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

Constant Summary collapse

DEFAULT_API_BASE_URL =
"https://api.delightedapp.com/v1"
DEFAULT_HTTP_ADAPTER =
HTTPAdapter.new

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client


6
7
8
9
10
# File 'lib/delighted/client.rb', line 6

def initialize(opts = {})
  @api_key = opts[:api_key] or raise ArgumentError, "You must provide an API key by setting Delighted.api_key = '123abc' or passing { :api_key => '123abc' } when instantiating Delighted::Client.new"
  @api_base_url = opts[:api_base_url] || DEFAULT_API_BASE_URL
  @http_adapter = opts[:http_adapter] || DEFAULT_HTTP_ADAPTER
end

Instance Method Details

#delete_json(path, params = {}) ⇒ Object


42
43
44
45
46
47
48
49
50
# File 'lib/delighted/client.rb', line 42

def delete_json(path, params = {})
  headers = default_headers.dup.merge('Accept' => 'application/json', 'Content-Type' => 'application/json')

  uri = URI.parse(File.join(@api_base_url, path))
  data = JSON.dump(params) unless params.empty?

  response = @http_adapter.request(:delete, uri, headers, data)
  handle_json_response(response)
end

#get_json(path, params = {}) ⇒ Object


12
13
14
15
16
17
18
19
20
# File 'lib/delighted/client.rb', line 12

def get_json(path, params = {})
  headers = default_headers.dup.merge('Accept' => 'application/json')

  uri = URI.parse(File.join(@api_base_url, path))
  uri.query = Utils.to_query(params) unless params.empty?

  response = @http_adapter.request(:get, uri, headers)
  handle_json_response(response)
end

#post_json(path, params = {}) ⇒ Object


22
23
24
25
26
27
28
29
30
# File 'lib/delighted/client.rb', line 22

def post_json(path, params = {})
  headers = default_headers.dup.merge('Accept' => 'application/json', 'Content-Type' => 'application/json')

  uri = URI.parse(File.join(@api_base_url, path))
  data = JSON.dump(params) unless params.empty?

  response = @http_adapter.request(:post, uri, headers, data)
  handle_json_response(response)
end

#put_json(path, params = {}) ⇒ Object


32
33
34
35
36
37
38
39
40
# File 'lib/delighted/client.rb', line 32

def put_json(path, params = {})
  headers = default_headers.dup.merge('Accept' => 'application/json', 'Content-Type' => 'application/json')

  uri = URI.parse(File.join(@api_base_url, path))
  data = JSON.dump(params) unless params.empty?

  response = @http_adapter.request(:put, uri, headers, data)
  handle_json_response(response)
end