Module: Arke::Resource::Model::ClassMethods
- Defined in:
- lib/arke/resource/model.rb
Instance Method Summary collapse
- #after_initialize(&block) ⇒ Object
- #all(params = {}) ⇒ Object
- #collection(collection) ⇒ Object
- #deserialize(body) ⇒ Object
- #deserializer(&block) ⇒ Object
-
#endpoint(endpoint = nil) ⇒ String
Gets or sets the device endpoint.
- #find(id) ⇒ Object
- #handle(code, &block) ⇒ Object
- #has_many(name, options = {}) ⇒ Object
-
#host(host = nil) ⇒ String
Gets or sets the resource host.
- #response_handlers ⇒ Object
- #table_name ⇒ Object
-
#url(options = {}) ⇒ String
Gets the resource url.
-
#url_template(template) ⇒ Addressable::Template
Gets or sets the url template for the resource.
Instance Method Details
#after_initialize(&block) ⇒ Object
59 60 61 62 |
# File 'lib/arke/resource/model.rb', line 59 def after_initialize(&block) @after_initializers ||= [] @after_initializers << block end |
#all(params = {}) ⇒ Object
49 50 51 52 |
# File 'lib/arke/resource/model.rb', line 49 def all(params={}) body = make_request(:get, {}, params) self._collection.new(self, body) end |
#collection(collection) ⇒ Object
85 86 87 |
# File 'lib/arke/resource/model.rb', line 85 def collection(collection) self._collection = collection if collection end |
#deserialize(body) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/arke/resource/model.rb', line 139 def deserialize(body) begin self._deserializer.call(body) rescue => e raise Errors::DeserializationError.new(e) end end |
#deserializer(&block) ⇒ Object
135 136 137 |
# File 'lib/arke/resource/model.rb', line 135 def deserializer(&block) self._deserializer = block if block_given? end |
#endpoint(endpoint = nil) ⇒ String
Gets or sets the device endpoint. By default the endpoint uses #resource_name if an endpoint has not been provided.
130 131 132 133 |
# File 'lib/arke/resource/model.rb', line 130 def endpoint(endpoint=nil) @endpoint = endpoint if endpoint @endpoint ||= resource_name end |
#find(id) ⇒ Object
44 45 46 47 |
# File 'lib/arke/resource/model.rb', line 44 def find(id) body = make_request(:get, {}, id: id) new(body) end |
#handle(code, &block) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/arke/resource/model.rb', line 64 def handle(code, &block) raise Arke::Errors::MissingHandlerBlock unless block_given? case code when String, Integer, Fixnum response_handlers[code.to_i] = block return true when Range, Array code.to_a.each do |c| response_handlers[c.to_i] = block end return true else raise Errors::InvalidHandler. new("#{code.class.name} is an invalid class, please user an Integer, Fixnum, String, Range or Array") end end |
#has_many(name, options = {}) ⇒ Object
54 55 56 57 |
# File 'lib/arke/resource/model.rb', line 54 def has_many(name, ={}) self.associations ||= {} self.associations[name] = Associations::HasManyAssociation end |
#host(host = nil) ⇒ String
Gets or sets the resource host.
107 108 109 110 |
# File 'lib/arke/resource/model.rb', line 107 def host(host=nil) @host = host if host @host end |
#response_handlers ⇒ Object
81 82 83 |
# File 'lib/arke/resource/model.rb', line 81 def response_handlers RESPONSE_HANDLERS end |
#table_name ⇒ Object
147 148 149 |
# File 'lib/arke/resource/model.rb', line 147 def table_name self.name.split('::')[-1].tableize end |
#url(options = {}) ⇒ String
Gets the resource url. This compiles the url template using the passed options and returns the resulting string.
query.
119 120 121 122 |
# File 'lib/arke/resource/model.rb', line 119 def url(={}) template = [:template] ? Addressable::Template.new(.delete(:template)) : nil || _url_template host + template.(.merge(endpoint: endpoint)).to_s end |
#url_template(template) ⇒ Addressable::Template
Gets or sets the url template for the resource. The url template is a Addressable template and defaults to DEFAULT_URL_TEMPLATE. If a parameter is passed to this method it will set the url template to the passed parameter, otherwise it will just return the URL template.
url template
98 99 100 |
# File 'lib/arke/resource/model.rb', line 98 def url_template(template) self._url_template = Addressable::Template.new(template) end |