Class: Delighted::Client
- Inherits:
-
Object
- Object
- Delighted::Client
- Defined in:
- lib/delighted/client.rb
Constant Summary collapse
- DEFAULT_API_BASE_URL =
"https://api.delightedapp.com/v1"
- DEFAULT_HTTP_ADAPTER =
HTTPAdapter.new
- DEFAULT_ACCEPT_HEADER =
"application/json"
Instance Method Summary collapse
- #delete_json(path, params = {}) ⇒ Object
- #get_json(path, params = {}) ⇒ Object
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #post_json(path, params = {}) ⇒ Object
- #put_json(path, params = {}) ⇒ Object
- #request_get(path, opts = {}) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 |
# File 'lib/delighted/client.rb', line 7 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
51 52 53 54 55 56 57 58 59 |
# File 'lib/delighted/client.rb', line 51 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
13 14 15 |
# File 'lib/delighted/client.rb', line 13 def get_json(path, params = {}) request_get(path, {params: params})[:json] end |
#post_json(path, params = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/delighted/client.rb', line 31 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
41 42 43 44 45 46 47 48 49 |
# File 'lib/delighted/client.rb', line 41 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 |
#request_get(path, opts = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/delighted/client.rb', line 17 def request_get(path, opts = {}) accept_header = opts.fetch(:accept_header, DEFAULT_ACCEPT_HEADER) params = opts.fetch(:params, {}) headers = default_headers.dup.merge('Accept' => accept_header) path = File.join(@api_base_url, path) unless is_full_url(path) uri = URI.parse(path) uri.query = Utils.to_query(params) unless params.empty? response = @http_adapter.request(:get, uri, headers) { json: handle_json_response(response), response: response } end |