Class: Dropwallet::Core::Model
- Inherits:
-
Object
- Object
- Dropwallet::Core::Model
show all
- Includes:
- ActiveModel::Serialization, ActiveModel::Validations
- Defined in:
- lib/dropwallet/core/model.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}, options = {}) ⇒ Model
attr_accessor :attributes
7
8
9
10
11
12
13
|
# File 'lib/dropwallet/core/model.rb', line 7
def initialize(attributes = {}, options = {})
@attributes = attributes
defaults = {:new => true}
options = defaults.merge(options)
@new = options[:new]
return @attributes
end
|
Class Method Details
.all(page = 1, options = {}) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/dropwallet/core/model.rb', line 85
def self.all(page=1, options={})
defaults = {:path => "#{serviceUrl}"}
options.merge(defaults)
options[:path] = (options[:path] << "?page=#{page}&limit=20")
doc = JSON.parse(RestClient.get(options[:path], {:accept => :json}))
if doc.include? 'errorMessage'
raise doc['errorMessage']
end
objects = []
doc['items'].each do |item|
objects << self.new(item)
end
return objects
end
|
.baseServiceUrl ⇒ Object
48
49
50
51
52
53
|
# File 'lib/dropwallet/core/model.rb', line 48
def self.baseServiceUrl
if Dropwallet::apiUrl.to_s == ''
return "https://api.dropwallet.net"
end
return Dropwallet::apiUrl.to_s
end
|
.count(options = {}) ⇒ Object
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/dropwallet/core/model.rb', line 101
def self.count(options={})
defaults = {:path => "#{serviceUrl}"}
options.merge(defaults)
options[:path] = (options[:path] << "?page=1&limit=1")
doc = JSON.parse(RestClient.get(options[:path], {:accept => :json}))
if doc.include? 'errorMessage'
raise doc['errorMessage']
end
return doc['pages']
end
|
.create(attributes = {}, options = {}) ⇒ Object
19
20
21
22
|
# File 'lib/dropwallet/core/model.rb', line 19
def self.create(attributes = {}, options = {})
model = self.new(attributes, options)
return model
end
|
.debug? ⇒ Boolean
60
61
62
|
# File 'lib/dropwallet/core/model.rb', line 60
def self.debug?
true
end
|
.delete(itemId, options = {}) ⇒ Object
157
158
159
160
161
162
163
164
|
# File 'lib/dropwallet/core/model.rb', line 157
def self.delete(itemId, options={})
defaults = {:path => "#{serviceUrl}/#{itemId}"}
defaults.merge(options)
item = JSON.parse(RestClient.delete(defaults[:path].strip, {:accept => :json}))
if item.include? 'errorMessage'
raise item['errorMessage']
end
end
|
.field(name, options = {}) ⇒ Object
24
25
26
27
28
29
30
31
32
|
# File 'lib/dropwallet/core/model.rb', line 24
def self.field(name, options = {})
name = name.to_s
define_method("#{name}=") do |value|
write_attribute(name, value)
end
define_method(name) do
read_attribute(name)
end
end
|
.find(id, options = {}) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/dropwallet/core/model.rb', line 73
def self.find(id, options = {})
if options[:path].nil?
options[:path] = serviceUrl
end
options[:path] = (options[:path] << "/#{id}")
doc = JSON.parse(RestClient.get(options[:path], {:accept => :json}))
if doc.include? 'errorMessage'
raise doc['errorMessage']
end
return self.new(doc, :new=>false)
end
|
.pages(options = {}) ⇒ Object
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/dropwallet/core/model.rb', line 112
def self.pages(options={})
defaults = {:path => "#{serviceUrl}"}
options.merge(defaults)
options[:path] = (options[:path] << "?page=1&limit=20")
doc = JSON.parse(RestClient.get(options[:path], {:accept => :json}))
if doc.include? 'errorMessage'
raise doc['errorMessage']
end
return doc['pages']
end
|
.serviceUrl ⇒ Object
55
56
57
58
|
# File 'lib/dropwallet/core/model.rb', line 55
def self.serviceUrl
className = self.name.split('::').last.downcase
"#{baseServiceUrl}/#{className}s"
end
|
Instance Method Details
#delete(options = {}) ⇒ Object
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/dropwallet/core/model.rb', line 146
def delete(options={})
defaults = {:path => "#{self.class.serviceUrl}/#{id}"}
defaults.merge(options)
item = JSON.parse(RestClient.delete(defaults[:path].strip, {:accept => :json}))
if item.include? 'errorMessage'
raise item['errorMessage']
end
@attributes = item
return self
end
|
#isNew? ⇒ Boolean
15
16
17
|
# File 'lib/dropwallet/core/model.rb', line 15
def isNew?
return @new
end
|
#read_attribute(key) ⇒ Object
38
39
40
|
# File 'lib/dropwallet/core/model.rb', line 38
def read_attribute(key)
@attributes[key.to_s]
end
|
#read_attribute_for_validation(key) ⇒ Object
34
35
36
|
# File 'lib/dropwallet/core/model.rb', line 34
def read_attribute_for_validation(key)
return read_attribute(key.to_s)
end
|
#save(options = {}) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/dropwallet/core/model.rb', line 123
def save(options = {})
unless isNew?
return update
end
defaults = {:path => "#{self.class.serviceUrl}"}
defaults.merge(options)
item = JSON.parse(RestClient.post(defaults[:path].strip, to_json, {:accept => :json, :'content-type'=> 'application/json'}))
if item.include? 'errorMessage'
raise item['errorMessage']
end
@attributes = item
return self
end
|
#to_json ⇒ Object
68
69
70
|
# File 'lib/dropwallet/core/model.rb', line 68
def to_json
return @attributes.to_json
end
|
#to_s ⇒ Object
64
65
66
|
# File 'lib/dropwallet/core/model.rb', line 64
def to_s
return @attributes.to_s
end
|
#update(options = {}) ⇒ Object
137
138
139
140
141
142
143
144
|
# File 'lib/dropwallet/core/model.rb', line 137
def update(options = {})
if isNew?
return save
end
defaults = {:path => "#{self.class.serviceUrl}/#{id}"}
defaults.merge(options)
return JSON.parse(RestClient.put(defaults[:path].strip, @attributes, {:accept => :json}))end
|
#write_attribute(key, value) ⇒ Object
44
45
46
|
# File 'lib/dropwallet/core/model.rb', line 44
def write_attribute(key, value)
@attributes[key.to_s] = value
end
|