Class: Zoho::Subscriptions::ResourceBase
- Inherits:
-
Object
- Object
- Zoho::Subscriptions::ResourceBase
- Includes:
- ActiveModel::Model, ActiveSupport::Configurable
- Defined in:
- lib/zoho/subscriptions/resource_base.rb
Direct Known Subclasses
Class Method Summary collapse
- .all(filter = {}) ⇒ Object
- .create(attributes) ⇒ Object
- .custom_action(action_name, http_method:, send_params_through: :body) ⇒ Object
- .custom_request(http_method, api_path, http_options) ⇒ Object
- .destroy(id) ⇒ Object
- .find(id) ⇒ Object
- .inherited(resource) ⇒ Object
- .resource_attributes(*attribute_names) ⇒ Object
- .resource_id ⇒ Object
- .resource_path ⇒ Object
- .unexpected_response(response) ⇒ Object
- .update(id, attributes) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #destroy ⇒ Object
- #id ⇒ Object
- #id=(new_id) ⇒ Object
- #resource_path ⇒ Object
- #update(attributes) ⇒ Object
Class Method Details
.all(filter = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 23 def all(filter = {}) response = Client.get "/#{pluralized_resource_name}", query: filter case response.code when 200 response[pluralized_resource_name].map { |attributes| new attributes.slice(*attribute_names.map(&:to_s)) } else unexpected_response response end end |
.create(attributes) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 47 def create(attributes) response = Client.post resource_path, body: attributes.to_json case response.code when 201 new response[resource_name].slice(*attribute_names.map(&:to_s)) else unexpected_response response end end |
.custom_action(action_name, http_method:, send_params_through: :body) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 87 def custom_action(action_name, http_method:, send_params_through: :body) unless [:get, :post, :put, :delete].include? http_method raise ArgumentError, "unsupported HTTP method: #{http_method}" end unless [:query, :body].include? send_params_through raise ArgumentError, "unsupported params method: #{send_params_through}" end define_method action_name do |**params| formatted_params = if send_params_through == :body params.to_json else params end new_attributes = custom_request http_method, "#{resource_path}/#{action_name}", send_params_through => formatted_params new_attributes.each do |attribute_name, value| public_send "#{attribute_name}=", value end end end |
.custom_request(http_method, api_path, http_options) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 76 def custom_request(http_method, api_path, ) response = Client.send http_method, api_path, case response.code when 200, 201 response[resource_name].slice(*attribute_names.map(&:to_s)) else unexpected_response response end end |
.destroy(id) ⇒ Object
62 63 64 65 66 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 62 def destroy(id) response = Client.delete "#{resource_path}/#{id}" unexpected_response response unless response.code == 200 end |
.find(id) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 34 def find(id) response = Client.get "#{resource_path}/#{id}" case response.code when 200 new response[resource_name].slice(*attribute_names.map(&:to_s)) when 404, 400 # Could be a bug in the API but currently when not found the response code is 400 raise Errors::NotFound, "cannot find #{resource_name} with id:#{id} " else unexpected_response response end end |
.inherited(resource) ⇒ Object
12 13 14 15 16 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 12 def inherited(resource) resource.configure do |config| config.resource_name = resource.name.gsub("Zoho::Subscriptions::", "").underscore end end |
.resource_attributes(*attribute_names) ⇒ Object
18 19 20 21 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 18 def resource_attributes(*attribute_names) @attribute_names = attribute_names attr_accessor *attribute_names end |
.resource_id ⇒ Object
72 73 74 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 72 def resource_id :"#{resource_name}_id" end |
.resource_path ⇒ Object
68 69 70 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 68 def resource_path "/#{pluralized_resource_name}" end |
.unexpected_response(response) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 113 def unexpected_response(response) case response.code when 400 raise Errors::BadRequest, response["message"] when 401 raise Errors::, response["message"] when 405 raise Errors::MethodNotAllowed, response["message"] when 429 raise Errors::TooManyRequests, response["message"] when 500 raise Errors::InternalServerError else raise Errors::Error, "unexpected response code: #{response.code}" end end |
.update(id, attributes) ⇒ Object
58 59 60 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 58 def update(id, attributes) custom_request :put, "#{resource_path}/#{id}", body: attributes.to_json end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
149 150 151 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 149 def ==(other) super || other.instance_of?(self.class) && !id.nil? && other.id == id end |
#destroy ⇒ Object
162 163 164 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 162 def destroy self.class.destroy id end |
#id ⇒ Object
141 142 143 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 141 def id public_send resource_id end |
#id=(new_id) ⇒ Object
145 146 147 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 145 def id=(new_id) public_send "#{resource_id}=", new_id end |
#resource_path ⇒ Object
166 167 168 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 166 def resource_path "#{self.class.resource_path}/#{id}" end |
#update(attributes) ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/zoho/subscriptions/resource_base.rb', line 154 def update(attributes) new_attributes = self.class.update id, attributes new_attributes.each do |attribute_name, value| public_send "#{attribute_name}=", value end end |