lib/pagarme/model.rb in pagarme-2.0.2 vs lib/pagarme/model.rb in pagarme-2.1.0
- old
+ new
@@ -1,64 +1,59 @@
module PagarMe
class Model < PagarMeObject
- def self.class_name
- self.name.split('::')[-1]
+ def create
+ update PagarMe::Request.post(self.class.url, params: to_hash).run
+ self
end
- def self.url()
- if self == Model
- raise PagarMeError.new('Cant request for model')
- end
- "/#{CGI.escape(class_name.downcase)}s"
+ def save
+ update PagarMe::Request.put(url, params: unsaved_attributes).run
+ self
end
- def url
- raise PagarMeError.new("ID invalido", 'id') if !self.id
- "#{self.class.url}/#{CGI.escape(id.to_s)}"
+ def url(*params)
+ raise RequestError.new('Invalid ID') unless id.present?
+ self.class.url CGI.escape(id.to_s), *params
end
- def create
- request = PagarMe::Request.new(self.class.url, 'POST')
- request.parameters = self.to_hash
- response = request.run
- update(response)
- end
+ class << self
- def save
- request = PagarMe::Request.new(self.url, 'PUT')
- request.parameters = self.unsaved_values
- response = request.run
- update(response)
- end
+ def create(*args, &block)
+ self.new(*args, &block).create
+ end
- def self.find_by_id(id)
- request = PagarMe::Request.new(self.url + "/#{id}", 'GET')
- response = request.run
- PagarMe::Util.convert_to_pagarme_object(response)
- end
+ def find_by_id(id)
+ raise RequestError.new('Invalid ID') unless id.present?
+ PagarMe::Request.get(url id).call
+ end
+ alias :find :find_by_id
- def self.find_by(hash, page = 1, count = 10)
- raise RequestError.new("Invalid page count") if page < 1 or count < 1
+ def find_by(hash, page = 1, count = 10)
+ raise RequestError.new('Invalid page count') if page < 1 or count < 1
- request = PagarMe::Request.new(self.url, 'GET')
+ PagarMe::Request.get(url, params: hash.merge(
+ page: page,
+ count: count
+ )).call
+ end
+ alias :find_by_hash :find_by
- request.parameters = hash
- request.parameters.merge!({:page => page, :count => count})
- response = request.run
- PagarMe::Util.convert_to_pagarme_object(response)
- end
+ def all(page = 1, count = 10)
+ find_by Hash.new, page, count
+ end
- def self.all(page = 1, count = 10)
- raise RequestError.new("Invalid page count") if page < 1 or count < 1
+ def url(*params)
+ ["/#{ CGI.escape underscored_class_name }s", *params].join '/'
+ end
- request = PagarMe::Request.new(url, 'GET')
- request.parameters = {
- :page => page,
- :count => count
- }
+ def class_name
+ self.name.split('::').last
+ end
- response = request.run
- response.map { |obj_response| self.new(obj_response) }
+ def underscored_class_name
+ class_name.gsub(/[a-z0-9][A-Z]/){|s| "#{s[0]}_#{s[1]}"}.downcase
+ end
+
end
end
-end
+end
\ No newline at end of file