Class: Essential::Resource
- Inherits:
-
Object
- Object
- Essential::Resource
show all
- Includes:
- AttrMethods
- Defined in:
- lib/essential/resource.rb,
lib/essential/resource/list.rb,
lib/essential/resource/create.rb,
lib/essential/resource/delete.rb,
lib/essential/resource/update.rb,
lib/essential/resource/attr_methods.rb,
lib/essential/resource/attr_relations.rb,
lib/essential/resource/paginator_proxy.rb
Defined Under Namespace
Modules: AttrMethods, AttrRelations, Create, Delete, List, Update
Classes: PaginatorProxy
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#filter_attrs, included
Constructor Details
#initialize(sid: nil, attrs: nil, headers: nil) ⇒ Resource
Returns a new instance of Resource.
45
46
47
48
49
50
|
# File 'lib/essential/resource.rb', line 45
def initialize(sid: nil, attrs: nil, headers: nil)
init_from(attrs)
@attributes['sid'] = sid if sid
=
self
end
|
Class Attribute Details
.schema ⇒ Object
Returns the value of attribute schema.
12
13
14
|
# File 'lib/essential/resource.rb', line 12
def schema
@schema
end
|
Class Method Details
.request(method, url: nil, params: nil, headers: nil) ⇒ Object
14
15
16
17
18
19
20
21
22
|
# File 'lib/essential/resource.rb', line 14
def request(method, url: nil, params: nil, headers: nil)
if
= .clone
sid = .delete(:sid)
token = .delete(:token)
end
Essential.request(method, url, sid: sid, token: token, params: params, headers: )
end
|
.url ⇒ Object
33
34
35
36
37
38
|
# File 'lib/essential/resource.rb', line 33
def self.url
if self == Resource
raise NotImplementedError, 'Resource is an abstract class'
end
"/v2/account/#{CGI.escape(class_name.downcase)}s"
end
|
Instance Method Details
#==(other) ⇒ Object
84
85
86
87
88
89
90
91
|
# File 'lib/essential/resource.rb', line 84
def ==(other)
case other
when Essential::Resource
self.sid == other.sid
else
false
end
end
|
#as_json(opts = {}) ⇒ Object
76
77
78
|
# File 'lib/essential/resource.rb', line 76
def as_json(opts={})
@attributes.dup
end
|
#fetch(headers: @headers) ⇒ Object
52
53
54
55
56
|
# File 'lib/essential/resource.rb', line 52
def fetch(headers: )
json = JSON.parse self.class.request(:get, url: self.url, headers: )
init_from json
self
end
|
#init_from(attributes) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/essential/resource.rb', line 58
def init_from(attributes)
attributes = (attributes || {}).clone
@attributes = filter_attrs(attributes)
if self.class.schema
self.class.schema.each do |key,type|
apply_schema(key, type)
end
end
self
end
|
#inspect ⇒ Object
93
94
95
96
97
98
99
100
|
# File 'lib/essential/resource.rb', line 93
def inspect
format(
'#<%s:0x%s @attributes=%s>',
self.class.name,
(self.object_id << 1).to_s(16),
JSON.pretty_generate(@attributes)
)
end
|
#loaded? ⇒ Boolean
72
73
74
|
# File 'lib/essential/resource.rb', line 72
def loaded?
@attributes.size > 1
end
|
#to_json(opts = {}) ⇒ Object
80
81
82
|
# File 'lib/essential/resource.rb', line 80
def to_json(opts={})
as_json.to_json(opts)
end
|
#url ⇒ Object
40
41
42
43
|
# File 'lib/essential/resource.rb', line 40
def url
raise ArgumentError, 'sid required' if sid.nil? || sid.empty?
format('%s/%s', self.class.url, CGI.escape(self.sid))
end
|