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 |
# File 'lib/tiny_client/resource.rb', line 163 def initialize(*_args) @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
16 17 18 |
# File 'lib/tiny_client/resource.rb', line 16 def id @id end |
Class Method Details
.build(hash, track_changes = true) ⇒ Resource
Create a resouce instance from an Hash.
110 111 112 113 114 115 116 117 |
# File 'lib/tiny_client/resource.rb', line 110 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
21 22 23 |
# File 'lib/tiny_client/resource.rb', line 21 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.
49 50 51 52 |
# File 'lib/tiny_client/resource.rb', line 49 def create(content) data = { low_name => content } post(data) end |
.delete(id = nil, name = nil, resource_class = nil) ⇒ Object
delete /<path>/#id.json
97 98 99 100 |
# File 'lib/tiny_client/resource.rb', line 97 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
32 33 34 |
# File 'lib/tiny_client/resource.rb', line 32 def fields(*names) @fields ||= field_accessor(names) && names end |
.get(params = {}, id = nil, name = nil, resource_class = nil) ⇒ Object
GET /<path>/#id/<name>
64 65 66 67 |
# File 'lib/tiny_client/resource.rb', line 64 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
40 41 42 |
# File 'lib/tiny_client/resource.rb', line 40 def index(params = {}) get(params) end |
.last_response ⇒ Response
Returns the last response that has been received for that resource.
120 121 122 |
# File 'lib/tiny_client/resource.rb', line 120 def last_response Thread.current[:_tclr] end |
.low_name ⇒ Object
102 103 104 |
# File 'lib/tiny_client/resource.rb', line 102 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.
27 28 29 |
# File 'lib/tiny_client/resource.rb', line 27 def path(path = nil) @path ||= path || low_name end |
.post(data, id = nil, name = nil, resource_class = nil) ⇒ Object
POST /<path>/#id/<name>
72 73 74 75 |
# File 'lib/tiny_client/resource.rb', line 72 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>
90 91 92 93 |
# File 'lib/tiny_client/resource.rb', line 90 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
58 59 60 |
# File 'lib/tiny_client/resource.rb', line 58 def show(id, params = {}) get(params, id) end |
.update(id, content) ⇒ Object
Will query PUT /<path>/#id
82 83 84 85 |
# File 'lib/tiny_client/resource.rb', line 82 def update(id, content) data = { low_name => content } put(data, id) end |
Instance Method Details
#as_json(options = {}) ⇒ Hash Also known as: to_h
218 219 220 221 222 |
# File 'lib/tiny_client/resource.rb', line 218 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.
210 211 212 |
# File 'lib/tiny_client/resource.rb', line 210 def clear_changes! @changes.clear end |
#destroy! ⇒ Object
Destroy this resource. It will call delete on this resource id. DELETE /path/id
186 187 188 189 190 |
# File 'lib/tiny_client/resource.rb', line 186 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)
199 200 201 202 203 204 205 206 |
# File 'lib/tiny_client/resource.rb', line 199 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 )
173 174 175 176 177 178 179 |
# File 'lib/tiny_client/resource.rb', line 173 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 |