Class: WAEasyAPI::Request
- Inherits:
-
Object
- Object
- WAEasyAPI::Request
- Includes:
- HTTParty
- Defined in:
- lib/waeasyapi/request.rb
Overview
Request objects are used to create fetch objects, which make requests to the server using HTTParty
Instance Method Summary collapse
- #all(options) ⇒ Object
- #create(data) ⇒ Object
-
#create_instance(res) ⇒ Object
Recursively builds entity instances out of all hashes in the response object.
- #delete(url) ⇒ Object
- #fetch(id) ⇒ Object
- #get(url, data = {}) ⇒ Object
-
#initialize(entity_name = nil) ⇒ Request
constructor
A new instance of Request.
-
#make_test_request ⇒ Object
Since we need to change the base route.
- #patch(id, data = {}) ⇒ Object
- #post(url, data = {}) ⇒ Object
- #put(id, data = {}) ⇒ Object
- #raise_error(error, status) ⇒ Object
- #raw_request(method, url, data = {}) ⇒ Object
- #request(method, url, data = {}) ⇒ Object
Constructor Details
#initialize(entity_name = nil) ⇒ Request
Returns a new instance of Request.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/waeasyapi/request.rb', line 14 def initialize(entity_name = nil) self.class.base_uri(WAEasyAPI::BASE_URI) @entity_name = entity_name custom_headers = WAEasyAPI.custom_headers || {} predefined_headers = { 'User-Agent' => "WAEasyAPI-Ruby/#{WAEasyAPI::VERSION}; Ruby/#{RUBY_VERSION}" } # Order is important to give precedence to predefined headers headers = custom_headers.merge(predefined_headers) @options = { basic_auth: WAEasyAPI.auth, timeout: 30, headers: headers } end |
Instance Method Details
#all(options) ⇒ Object
34 35 36 |
# File 'lib/waeasyapi/request.rb', line 34 def all() request :get, "/#{@entity_name}", end |
#create(data) ⇒ Object
58 59 60 |
# File 'lib/waeasyapi/request.rb', line 58 def create(data) request :post, "/#{@entity_name}", data end |
#create_instance(res) ⇒ Object
Recursively builds entity instances out of all hashes in the response object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/waeasyapi/request.rb', line 84 def create_instance(res) response = res.parsed_response if response.is_a?(Array)==true || response.to_s.length == 0 return response end # if there was an error, throw it raise_error(response['error'], res.code) if response.nil? || response.key?('error') && res.code !=200 # There must be a top level entity begin class_name = response['entity'].split('_').collect(&:capitalize).join klass = WAEasyAPI.const_get class_name rescue NameError # Use Entity class if we don't find any klass = WAEasyAPI::Entity end klass.new(response) end |
#delete(url) ⇒ Object
46 47 48 |
# File 'lib/waeasyapi/request.rb', line 46 def delete(url) request :delete, "/#{@entity_name}/#{url}" end |
#fetch(id) ⇒ Object
30 31 32 |
# File 'lib/waeasyapi/request.rb', line 30 def fetch(id) request :get, "/#{@entity_name}/#{id}" end |
#get(url, data = {}) ⇒ Object
42 43 44 |
# File 'lib/waeasyapi/request.rb', line 42 def get(url, data = {}) request :get, "/#{@entity_name}/#{url}", data end |
#make_test_request ⇒ Object
Since we need to change the base route
78 79 80 |
# File 'lib/waeasyapi/request.rb', line 78 def make_test_request self.class.get WAEasyAPI::TEST_URL, @options end |
#patch(id, data = {}) ⇒ Object
54 55 56 |
# File 'lib/waeasyapi/request.rb', line 54 def patch(id, data = {}) request :patch, "/#{@entity_name}/#{id}", data end |
#post(url, data = {}) ⇒ Object
38 39 40 |
# File 'lib/waeasyapi/request.rb', line 38 def post(url, data = {}) request :post, "/#{@entity_name}/#{url}", data end |
#put(id, data = {}) ⇒ Object
50 51 52 |
# File 'lib/waeasyapi/request.rb', line 50 def put(id, data = {}) request :put, "/#{@entity_name}/#{id}", data end |
#raise_error(error, status) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/waeasyapi/request.rb', line 105 def raise_error(error, status) # Get the error class name, require it and instantiate an error class_name = error['code'].split('_').map(&:capitalize).join('') args = [error['code'], status] args.push error['field'] if error.key?('field') require "waeasyapi/errors/#{error['code'].downcase}" klass = WAEasyAPI.const_get(class_name) raise klass.new(*args), error['description'] rescue NameError, LoadError # We got an unknown error, cast it to Error for now raise WAEasyAPI::Error.new, 'Unknown Error' end |
#raw_request(method, url, data = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/waeasyapi/request.rb', line 66 def raw_request(method, url, data = {}) case method when :get @options[:query] = data when :post, :put, :patch @options[:body] = data end self.class.send(method, url, @options) end |
#request(method, url, data = {}) ⇒ Object
62 63 64 |
# File 'lib/waeasyapi/request.rb', line 62 def request(method, url, data = {}) create_instance raw_request(method, url, data) end |