Module: Arke::Resource::Model::ClassMethods

Defined in:
lib/arke/resource/model.rb

Instance Method Summary collapse

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.

Parameters:

  • the (String)

    service endpoint. if left nil it will simply return the existing endpoint

Returns:

  • (String)

    the service endpoint



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, options={})
  self.associations ||= {}
  self.associations[name] =  Associations::HasManyAssociation
end

#host(host = nil) ⇒ String

Gets or sets the resource host.

Parameters:

  • host (String) (defaults to: nil)

    the resource host. if left nil, the method will simply return the current resource host

Returns:

  • (String)

    the current 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_handlersObject



81
82
83
# File 'lib/arke/resource/model.rb', line 81

def response_handlers
  RESPONSE_HANDLERS
end

#table_nameObject



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.

Parameters:

  • options (Hash) (defaults to: {})

    the url options to pass to the template. By default the template looks for a an id and a

Returns:

  • (String)

    the full resource url



119
120
121
122
# File 'lib/arke/resource/model.rb', line 119

def url(options={})
  template = options[:template] ? Addressable::Template.new(options.delete(:template)) : nil || _url_template
  host + template.expand(options.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

Parameters:

  • template (String)

    the url template to set, if left nil, the method will simply return the current

Returns:

  • (Addressable::Template)

    the url template. If none has been set, it references DEFAULT_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