Class: Sendicate::Resource
- Inherits:
-
Object
- Object
- Sendicate::Resource
show all
- Includes:
- HTTParty
- Defined in:
- lib/sendicate/resource.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Resource
Returns a new instance of Resource.
10
11
12
13
|
# File 'lib/sendicate/resource.rb', line 10
def initialize(attributes={})
@attributes = attributes
@errors = {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/sendicate/resource.rb', line 15
def method_missing(method, *args)
method_string = method.to_s
is_setter_method = method_string[-1, 1] == '='
key = method_string.gsub(/\=/, '')
if is_setter_method
attributes[key] = args.first
elsif attributes.include?(key)
attributes[key]
else
super
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
8
9
10
|
# File 'lib/sendicate/resource.rb', line 8
def attributes
@attributes
end
|
#errors ⇒ Object
Returns the value of attribute errors.
8
9
10
|
# File 'lib/sendicate/resource.rb', line 8
def errors
@errors
end
|
#response ⇒ Object
Returns the value of attribute response.
8
9
10
|
# File 'lib/sendicate/resource.rb', line 8
def response
@response
end
|
Class Method Details
.all ⇒ Object
35
36
37
38
39
40
|
# File 'lib/sendicate/resource.rb', line 35
def all
response = get(collection_path)
response.parsed_response.map do |attributes|
new(attributes)
end
end
|
.base_path(path) ⇒ Object
31
32
33
|
# File 'lib/sendicate/resource.rb', line 31
def base_path(path)
@base_path = path
end
|
.collection_path ⇒ Object
51
52
53
|
# File 'lib/sendicate/resource.rb', line 51
def collection_path
@base_path
end
|
.delete(*args) ⇒ Object
75
76
77
|
# File 'lib/sendicate/resource.rb', line 75
def delete(*args)
Request.delete(*args)
end
|
.find(param) ⇒ Object
42
43
44
45
46
47
48
49
|
# File 'lib/sendicate/resource.rb', line 42
def find(param)
if param.nil?
raise ResourceNotFound
else
response = get(member_path(param))
new(response.parsed_response)
end
end
|
.get(*args) ⇒ Object
59
60
61
|
# File 'lib/sendicate/resource.rb', line 59
def get(*args)
Request.get(*args)
end
|
.member_path(param) ⇒ Object
55
56
57
|
# File 'lib/sendicate/resource.rb', line 55
def member_path(param)
[@base_path, param].join("/")
end
|
.patch(*args) ⇒ Object
67
68
69
|
# File 'lib/sendicate/resource.rb', line 67
def patch(*args)
Request.patch(*args)
end
|
.post(*args) ⇒ Object
63
64
65
|
# File 'lib/sendicate/resource.rb', line 63
def post(*args)
Request.post(*args)
end
|
.put(*args) ⇒ Object
71
72
73
|
# File 'lib/sendicate/resource.rb', line 71
def put(*args)
Request.put(*args)
end
|
Instance Method Details
#destroy ⇒ Object
96
97
98
|
# File 'lib/sendicate/resource.rb', line 96
def destroy
response = self.class.delete(member_path)
end
|
#id ⇒ Object
104
105
106
|
# File 'lib/sendicate/resource.rb', line 104
def id
attributes['id'] || attributes[:id]
end
|
#member_path ⇒ Object
116
117
118
|
# File 'lib/sendicate/resource.rb', line 116
def member_path
self.class.member_path(to_param)
end
|
#persisted? ⇒ Boolean
100
101
102
|
# File 'lib/sendicate/resource.rb', line 100
def persisted?
!to_param.nil?
end
|
#save ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/sendicate/resource.rb', line 80
def save
response = if persisted?
self.class.put(member_path, body: to_json)
else
self.class.post(member_path, body: to_json)
end
if response.success?
@attributes = response.parsed_response
true
else
@errors = response.parsed_response
false
end
end
|
#to_json ⇒ Object
112
113
114
|
# File 'lib/sendicate/resource.rb', line 112
def to_json
MultiJson.dump(attributes)
end
|
#to_param ⇒ Object
108
109
110
|
# File 'lib/sendicate/resource.rb', line 108
def to_param
id
end
|