Class: MagicBell::ApiResource
- Inherits:
-
Object
- Object
- MagicBell::ApiResource
show all
- Defined in:
- lib/magicbell/api_resource.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(client, attributes = {}) ⇒ ApiResource
Returns a new instance of ApiResource.
33
34
35
36
37
38
39
|
# File 'lib/magicbell/api_resource.rb', line 33
def initialize(client, attributes = {})
@client = client
@attributes = attributes
@id = @attributes["id"]
@retrieved = true if @id
end
|
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
31
32
33
|
# File 'lib/magicbell/api_resource.rb', line 31
def id
@id
end
|
Class Method Details
.create(client, attributes = {}) ⇒ Object
8
9
10
|
# File 'lib/magicbell/api_resource.rb', line 8
def create(client, attributes = {})
new(client, attributes).create
end
|
.create_path ⇒ Object
22
23
24
|
# File 'lib/magicbell/api_resource.rb', line 22
def create_path
"/#{name}s"
end
|
.create_url ⇒ Object
26
27
28
|
# File 'lib/magicbell/api_resource.rb', line 26
def create_url
MagicBell.api_host + create_path
end
|
.find(client, id) ⇒ Object
12
13
14
15
16
|
# File 'lib/magicbell/api_resource.rb', line 12
def find(client, id)
api_resource = new(client, "id" => id)
api_resource.retrieve
api_resource
end
|
.name ⇒ Object
18
19
20
|
# File 'lib/magicbell/api_resource.rb', line 18
def name
to_s.demodulize.underscore
end
|
Instance Method Details
#attribute(attribute_name) ⇒ Object
47
48
49
|
# File 'lib/magicbell/api_resource.rb', line 47
def attribute(attribute_name)
attributes[attribute_name]
end
|
#attributes ⇒ Object
Also known as:
to_h
41
42
43
44
|
# File 'lib/magicbell/api_resource.rb', line 41
def attributes
retrieve_unless_retrieved
@attributes
end
|
#create ⇒ Object
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/magicbell/api_resource.rb', line 78
def create
response = @client.post(
create_url,
body: { name => attributes }.to_json,
headers:
)
parse_response(response)
self
end
|
#create_path ⇒ Object
66
67
68
|
# File 'lib/magicbell/api_resource.rb', line 66
def create_path
"/#{name}s"
end
|
#create_url ⇒ Object
62
63
64
|
# File 'lib/magicbell/api_resource.rb', line 62
def create_url
MagicBell.api_host + create_path
end
|
#name ⇒ Object
58
59
60
|
# File 'lib/magicbell/api_resource.rb', line 58
def name
self.class.name
end
|
#path ⇒ Object
74
75
76
|
# File 'lib/magicbell/api_resource.rb', line 74
def path
"/#{name}s/#{id}"
end
|
#retrieve ⇒ Object
51
52
53
54
55
56
|
# File 'lib/magicbell/api_resource.rb', line 51
def retrieve
response = @client.get(url)
parse_response(response)
self
end
|
#update(new_attributes = {}) ⇒ Object
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/magicbell/api_resource.rb', line 89
def update(new_attributes = {})
response = @client.put(
url,
body: new_attributes.to_json,
headers:
)
parse_response(response)
self
end
|
#url ⇒ Object
70
71
72
|
# File 'lib/magicbell/api_resource.rb', line 70
def url
MagicBell.api_host + path
end
|