Class: Holded::Request
- Inherits:
-
Object
- Object
- Holded::Request
- Defined in:
- lib/holded/request.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
Instance Method Summary collapse
- #create(params: nil) ⇒ Object
- #delete(params: nil) ⇒ Object
-
#initialize(api_key: nil, api_version: 'v1') ⇒ Request
constructor
A new instance of Request.
-
#method_missing(method, *args) ⇒ Object
Si el method no está definido lo extraemos y lo incluimos en el path si tiene argumentos los añadimos al path.
- #path ⇒ Object
- #search(params: nil) ⇒ Object
- #update(params: nil, body: nil) ⇒ Object
Constructor Details
#initialize(api_key: nil, api_version: 'v1') ⇒ Request
Returns a new instance of Request.
6 7 8 9 10 |
# File 'lib/holded/request.rb', line 6 def initialize(api_key: nil, api_version: 'v1') @path_parts = [] @api_key = api_key || self.class.api_key || ENV['HOLDED_API_KEY'] @api_version = api_version end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Si el method no está definido lo extraemos y lo incluimos en el path si tiene argumentos los añadimos al path
Ejemplo: Holded::Request.new.contacts.create ‘contacts’ será parte del path_parts #
21 22 23 24 25 26 |
# File 'lib/holded/request.rb', line 21 def method_missing(method, *args) @path_parts << method.to_s.gsub("_", "-").downcase @path_parts << args if args.length > 0 @path_parts << @api_version if @path_parts.length == 1 self end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
4 5 6 |
# File 'lib/holded/request.rb', line 4 def api_key @api_key end |
Instance Method Details
#create(params: nil) ⇒ Object
32 33 34 35 36 |
# File 'lib/holded/request.rb', line 32 def create(params: nil) APIRequest.new(builder: self).post(params: params) ensure reset_path_parts end |
#delete(params: nil) ⇒ Object
50 51 52 53 54 |
# File 'lib/holded/request.rb', line 50 def delete(params: nil) APIRequest.new(builder: self).delete(params: params) ensure reset_path_parts end |
#path ⇒ Object
28 29 30 |
# File 'lib/holded/request.rb', line 28 def path @path_parts.join('/') end |
#search(params: nil) ⇒ Object
44 45 46 47 48 |
# File 'lib/holded/request.rb', line 44 def search(params: nil) APIRequest.new(builder: self).get(params: params) ensure reset_path_parts end |
#update(params: nil, body: nil) ⇒ Object
38 39 40 41 42 |
# File 'lib/holded/request.rb', line 38 def update(params: nil, body: nil) APIRequest.new(builder: self).put(params: params) ensure reset_path_parts end |