Class: Shipay::Model

Inherits:
ShipayObject show all
Defined in:
lib/shipay/model.rb

Direct Known Subclasses

Item, OrderCommom, Wallet, WalletList

Constant Summary

Constants inherited from ShipayObject

ShipayObject::RESOURCES

Instance Attribute Summary

Attributes inherited from ShipayObject

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ShipayObject

#==, #[]=, convert, #empty?, #initialize, #respond_to?, #to_hash, #unsaved_attributes

Constructor Details

This class inherits a constructor from Shipay::ShipayObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Shipay::ShipayObject

Class Method Details

.class_nameObject



60
61
62
# File 'lib/shipay/model.rb', line 60

def class_name
  self.name.split('::').last.downcase
end

.create(*args) ⇒ Object



32
33
34
# File 'lib/shipay/model.rb', line 32

def create(*args)
  self.new(*args).create
end

.find_by_id(id, **options) ⇒ Object Also known as: find

Raises:



36
37
38
39
# File 'lib/shipay/model.rb', line 36

def find_by_id(id, **options)
  raise RequestError.new('Invalid ID') unless id.present?
  Shipay::Request.get(url(id), options).call underscored_class_name
end

.underscored_class_nameObject



64
65
66
# File 'lib/shipay/model.rb', line 64

def underscored_class_name
  self.name.split('::').last.gsub(/[a-z0-9][A-Z]/){|s| "#{s[0]}_#{s[1]}"}.downcase
end

.url(*params) ⇒ Object

def all(*args, **params)

params = extract_page_count_or_params(*args, **params)
find_by params

end alias :where :all



56
57
58
# File 'lib/shipay/model.rb', line 56

def url(*params)
  ["/#{ CGI.escape class_name }", *params].join '/'
end

Instance Method Details

#class_nameObject



27
28
29
# File 'lib/shipay/model.rb', line 27

def class_name
  self.class.to_s.split('::').last
end

#createObject



3
4
5
6
# File 'lib/shipay/model.rb', line 3

def create
  update Shipay::Request.post(self.class.url, params: to_hash).call(class_name)
  self
end

#fetchObject



18
19
20
21
# File 'lib/shipay/model.rb', line 18

def fetch
  update self.class.find(primary_key, client_key: client_key)
  self
end

#primary_keyObject



23
24
25
# File 'lib/shipay/model.rb', line 23

def primary_key
  id
end

#saveObject



8
9
10
11
# File 'lib/shipay/model.rb', line 8

def save
  update Shipay::Request.put(url, params: unsaved_attributes).call(class_name)
  self
end

#url(*params) ⇒ Object

Raises:



13
14
15
16
# File 'lib/shipay/model.rb', line 13

def url(*params)
  raise RequestError.new('Invalid ID') unless primary_key.present?
  self.class.url CGI.escape(primary_key.to_s), *params
end