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
68 69 70 71 |
# File 'lib/arke/resource/model.rb', line 68 def after_initialize(&block) @after_initializers ||= [] @after_initializers << block end |
#all(params = {}) ⇒ Object
55 56 57 58 |
# File 'lib/arke/resource/model.rb', line 55 def all(params={}) body = make_request(:get, {}, params) self._collection.new(self, body) end |
#collection(collection) ⇒ Object
94 95 96 |
# File 'lib/arke/resource/model.rb', line 94 def collection(collection) self._collection = collection if collection end |
#deserialize(body) ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/arke/resource/model.rb', line 148 def deserialize(body) begin self._deserializer.call(body) rescue => e raise Errors::DeserializationError.new(e) end end |
#deserializer(&block) ⇒ Object
144 145 146 |
# File 'lib/arke/resource/model.rb', line 144 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.
139 140 141 142 |
# File 'lib/arke/resource/model.rb', line 139 def endpoint(endpoint=nil) @endpoint = endpoint if endpoint @endpoint ||= resource_name end |
#find(id) ⇒ Object
50 51 52 53 |
# File 'lib/arke/resource/model.rb', line 50 def find(id) body = make_request(:get, {}, id: id) new(body) end |
#handle(code, &block) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/arke/resource/model.rb', line 73 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
60 61 62 63 64 65 66 |
# File 'lib/arke/resource/model.rb', line 60 def has_many(name, ={}) self.associations ||= {} self.associations[name] = { association: Associations::HasManyAssociation, options: } end |
#host(host = nil) ⇒ String
Gets or sets the resource host.
116 117 118 119 |
# File 'lib/arke/resource/model.rb', line 116 def host(host=nil) @host = host if host @host end |
#response_handlers ⇒ Object
90 91 92 |
# File 'lib/arke/resource/model.rb', line 90 def response_handlers RESPONSE_HANDLERS end |
#table_name ⇒ Object
156 157 158 |
# File 'lib/arke/resource/model.rb', line 156 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.
128 129 130 131 |
# File 'lib/arke/resource/model.rb', line 128 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
107 108 109 |
# File 'lib/arke/resource/model.rb', line 107 def url_template(template) self._url_template = Addressable::Template.new(template) end |