Class: TinyClient::Resource
- Inherits:
-
Object
- Object
- TinyClient::Resource
- Includes:
- NestedSupport, PaginationSupport
- Defined in:
- lib/tiny_client/resource.rb
Overview
This is the core of TinyClient. Subclass Resource in order to create an HTTP/JSON tiny client.
Instance Attribute Summary collapse
-
#changes ⇒ Object
readonly
the fields that has beem modified, and will be save on #save!.
-
#id ⇒ Object
A resource always have an id.
Class Method Summary collapse
-
.build(hash, track_changes = true) ⇒ Resource
Create a resouce instance from an Hash.
-
.conf(config) ⇒ Object
Set this resource client configuration.
-
.create(content) ⇒ Object
POST /<resource_path>.json Create a new resource.
-
.delete(id = nil, name = nil, resource_class = nil) ⇒ Object
delete /<path>/#id.json.
- .fields(*names) ⇒ Object
-
.get(params = {}, id = nil, name = nil, resource_class = nil) ⇒ Object
GET /<path>/#id/<name>.
-
.index(params = {}) ⇒ Enumerator
GET /<path>.json.
-
.last_response ⇒ Response
The last response that has been received for that resource.
- .low_name ⇒ Object
-
.path(path = nil) ⇒ Object
Set the resource path, default is the class name in lower case.
-
.post(data, id = nil, name = nil, resource_class = nil) ⇒ Object
POST /<path>/#id/<name>.
-
.put(data, id = nil, name = nil, resource_class = nil) ⇒ Object
PUT /<path>/#id/<name>.
-
.show(id, params = {}) ⇒ Object
GET /<resource_path>/#id.
-
.update(id, content) ⇒ Object
Will query PUT /<path>/#id.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Hash (also: #to_h)
-
#clear_changes! ⇒ Object
Mark all fields has not changed.
-
#destroy! ⇒ Object
Destroy this resource.
-
#initialize(*_args) ⇒ Resource
constructor
A new instance of Resource.
-
#load!(params = {}) ⇒ Object
Load/Reload this resource from the server.
-
#save! ⇒ Resource
Save the resource fields that has changed, or create it, if it’s a new one! Create the a new resource if id is not set or update the corresonding resource.
Methods included from NestedSupport
included, #nested_all, #nested_create, #nested_delete, #nested_index, #nested_show, #nested_update
Methods included from PaginationSupport
Constructor Details
#initialize(*_args) ⇒ Resource
Returns a new instance of Resource.
163 164 165 166 |
# File 'lib/tiny_client/resource.rb', line 163 def initialize(*_args) @id = nil @changes = Set.new # store the fields change here end |
Instance Attribute Details
#changes ⇒ Object (readonly)
the fields that has beem modified, and will be save on #save!
161 162 163 |
# File 'lib/tiny_client/resource.rb', line 161 def changes @changes end |
#id ⇒ Object
A resource always have an id
158 159 160 |
# File 'lib/tiny_client/resource.rb', line 158 def id @id end |
Class Method Details
.build(hash, track_changes = true) ⇒ Resource
Create a resouce instance from an Hash.
107 108 109 110 111 112 113 114 |
# File 'lib/tiny_client/resource.rb', line 107 def build(hash, track_changes = true) resource = fields.each_with_object(new) do |field, r| value = hash.fetch(field.to_s, hash[field.to_sym]) r.send("#{field}=", value) end resource.clear_changes! unless track_changes resource end |
.conf(config) ⇒ Object
Set this resource client configuration
18 19 20 |
# File 'lib/tiny_client/resource.rb', line 18 def conf(config) @conf ||= config end |
.create(content) ⇒ Object
POST /<resource_path>.json Create a new resource. The resource will be indexed by it’s name.
46 47 48 49 |
# File 'lib/tiny_client/resource.rb', line 46 def create(content) data = { low_name => content } post(data) end |
.delete(id = nil, name = nil, resource_class = nil) ⇒ Object
delete /<path>/#id.json
94 95 96 97 |
# File 'lib/tiny_client/resource.rb', line 94 def delete(id = nil, name = nil, resource_class = nil) resp = @conf.requestor.delete(@path, id, name) (resource_class || self).from_response resp end |
.fields(*names) ⇒ Object
29 30 31 |
# File 'lib/tiny_client/resource.rb', line 29 def fields(*names) @fields ||= field_accessor(names) && names end |
.get(params = {}, id = nil, name = nil, resource_class = nil) ⇒ Object
GET /<path>/#id/<name>
61 62 63 64 |
# File 'lib/tiny_client/resource.rb', line 61 def get(params = {}, id = nil, name = nil, resource_class = nil) resp = @conf.requestor.get(@path, params, id, name) (resource_class || self).from_response resp end |
.index(params = {}) ⇒ Enumerator
GET /<path>.json
37 38 39 |
# File 'lib/tiny_client/resource.rb', line 37 def index(params = {}) get(params) end |
.last_response ⇒ Response
Returns the last response that has been received for that resource.
117 118 119 |
# File 'lib/tiny_client/resource.rb', line 117 def last_response Thread.current[:_tclr] end |
.low_name ⇒ Object
99 100 101 |
# File 'lib/tiny_client/resource.rb', line 99 def low_name @low_name ||= name.demodulize.underscore end |
.path(path = nil) ⇒ Object
Set the resource path, default is the class name in lower case.
24 25 26 |
# File 'lib/tiny_client/resource.rb', line 24 def path(path = nil) @path ||= path || low_name end |
.post(data, id = nil, name = nil, resource_class = nil) ⇒ Object
POST /<path>/#id/<name>
69 70 71 72 |
# File 'lib/tiny_client/resource.rb', line 69 def post(data, id = nil, name = nil, resource_class = nil) resp = @conf.requestor.post(data, @path, id, name) (resource_class || self).from_response resp end |
.put(data, id = nil, name = nil, resource_class = nil) ⇒ Object
PUT /<path>/#id/<name>
87 88 89 90 |
# File 'lib/tiny_client/resource.rb', line 87 def put(data, id = nil, name = nil, resource_class = nil) resp = @conf.requestor.put(data, @path, id, name) (resource_class || self).from_response resp end |
.show(id, params = {}) ⇒ Object
GET /<resource_path>/#id
55 56 57 |
# File 'lib/tiny_client/resource.rb', line 55 def show(id, params = {}) get(params, id) end |
.update(id, content) ⇒ Object
Will query PUT /<path>/#id
79 80 81 82 |
# File 'lib/tiny_client/resource.rb', line 79 def update(id, content) data = { low_name => content } put(data, id) end |
Instance Method Details
#as_json(options = {}) ⇒ Hash Also known as: to_h
219 220 221 222 223 |
# File 'lib/tiny_client/resource.rb', line 219 def as_json( = {}) self.class.fields.each_with_object({}) do |field, h| h[field] = send(field) end.as_json() end |
#clear_changes! ⇒ Object
Mark all fields has not changed. This mean that calling save! will not modify this resource until a field attribute has been changed.
211 212 213 |
# File 'lib/tiny_client/resource.rb', line 211 def clear_changes! @changes.clear end |
#destroy! ⇒ Object
Destroy this resource. It will call delete on this resource id. DELETE /path/id
187 188 189 190 191 |
# File 'lib/tiny_client/resource.rb', line 187 def destroy! raise ResourceError, 'Cannot delete resource if @id not present' if id.blank? self.class.delete(id) self end |
#load!(params = {}) ⇒ Object
Load/Reload this resource from the server. It will reset all fields that has been retrieved through the request. It will do a GET request on the resource id (:show)
200 201 202 203 204 205 206 207 |
# File 'lib/tiny_client/resource.rb', line 200 def load!(params = {}) raise ResourceError, 'Cannot load resource if @id not present' if id.blank? # get the values from the persistence layer reloaded = self.class.show(@id, params) clone_fields(reloaded) clear_changes! reloaded end |
#save! ⇒ Resource
Save the resource fields that has changed, or create it, if it’s a new one!
Create the a new resource if id is not set or update the corresonding resource.
Create is done by calling POST on the resource path
Update is done by calling PUT on the resource id ( path/id )
174 175 176 177 178 179 180 |
# File 'lib/tiny_client/resource.rb', line 174 def save! data = @changes.to_a.each_with_object({}) { |field, h| h[field] = send(field) } saved = id.present? ? self.class.update(id, data) : self.class.create(data) clone_fields(saved) clear_changes! self end |