Class: AbstractApiWrapper::Request
- Inherits:
-
Object
- Object
- AbstractApiWrapper::Request
- Defined in:
- lib/abstract_api_wrapper.rb
Defined Under Namespace
Classes: NoResourceGiven
Constant Summary collapse
- METHODS_MAP =
{ 'all' => 'get', 'head' => 'head', 'find' => 'get', 'create' => 'post', 'update' => 'put', 'destroy' => 'delete' }.freeze
Instance Attribute Summary collapse
-
#chain ⇒ Object
Returns the value of attribute chain.
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#params ⇒ Object
Returns the value of attribute params.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #endpoint ⇒ Object
- #headers ⇒ Object
-
#initialize(*options) ⇒ Request
constructor
A new instance of Request.
- #method_missing(name, *params, &block) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(*options) ⇒ Request
Returns a new instance of Request.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/abstract_api_wrapper.rb', line 33 def initialize(*) @client = [1] @path = [] @chain = [] @filters = Hashie::Mash.new @params = Hashie::Mash.new method_name = [0] unless method_name.nil? @path << method_name @chain << method_name end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *params, &block) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/abstract_api_wrapper.rb', line 48 def method_missing(name, *params, &block) method_name = name.to_s return self if chain.last == method_name chain.push(method_name) case method_name when 'all' filters.merge!(params.first || {}) when 'head' filters.merge!(params.first || {}) when 'find' path.push(params.first.to_s) when 'create' @params = Hashie::Mash.new(params.first || {}) when 'update' if chain[-2] == 'find' @params = Hashie::Mash.new(params.first || {}) end when 'destroy' if chain[-2] != 'find' raise NoResourceGiven.new('Missing resource id to destroy') end else path.push(method_name) end self end |
Instance Attribute Details
#chain ⇒ Object
Returns the value of attribute chain.
22 23 24 |
# File 'lib/abstract_api_wrapper.rb', line 22 def chain @chain end |
#filters ⇒ Object
Returns the value of attribute filters.
22 23 24 |
# File 'lib/abstract_api_wrapper.rb', line 22 def filters @filters end |
#params ⇒ Object
Returns the value of attribute params.
22 23 24 |
# File 'lib/abstract_api_wrapper.rb', line 22 def params @params end |
#path ⇒ Object
Returns the value of attribute path.
22 23 24 |
# File 'lib/abstract_api_wrapper.rb', line 22 def path @path end |
Instance Method Details
#endpoint ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/abstract_api_wrapper.rb', line 101 def endpoint # Here is a Query String API versioning with the `apiver` param # You can change this to whatever your API versioning system is filters.apiver = @client.apiver query_string = filters.map { |k, v| "#{k}=#{v}" }.join('&') "#{@client.base_url}/#{path.join('/')}?#{query_string}" end |
#headers ⇒ Object
94 95 96 97 98 99 |
# File 'lib/abstract_api_wrapper.rb', line 94 def headers { 'Authorization' => "token #{@client.access_token}", 'Content-Type' => 'application/json' } end |
#run ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/abstract_api_wrapper.rb', line 78 def run method = METHODS_MAP[@chain.last] || 'get' params = @params.any? ? @params.to_json : nil request = Faraday.send(method, endpoint, params, headers) response = Response.new(request) # Reset variables path = [] chain = [] filters = Hashie::Mash.new params = Hashie::Mash.new response.body end |