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

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

Instance Method Summary collapse

Instance Method Details

#after_initialize(&block) ⇒ Object



64
65
66
67
# File 'lib/arke/resource/model.rb', line 64

def after_initialize(&block)
  @after_initializers ||= []
  @after_initializers << block
end

#all(params = {}) ⇒ Object



51
52
53
54
# File 'lib/arke/resource/model.rb', line 51

def all(params={})
  body = make_request(:get, {}, params)
  self._collection.new(self, body)
end

#collection(collection) ⇒ Object



90
91
92
# File 'lib/arke/resource/model.rb', line 90

def collection(collection)
  self._collection = collection if collection
end

#deserialize(body) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/arke/resource/model.rb', line 144

def deserialize(body)
  begin
    self._deserializer.call(body)
  rescue => e
    raise Errors::DeserializationError.new(e)
  end
end

#deserializer(&block) ⇒ Object



140
141
142
# File 'lib/arke/resource/model.rb', line 140

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



135
136
137
138
# File 'lib/arke/resource/model.rb', line 135

def endpoint(endpoint=nil)
  @endpoint = endpoint if endpoint
  @endpoint ||= resource_name
end

#find(id) ⇒ Object



46
47
48
49
# File 'lib/arke/resource/model.rb', line 46

def find(id)
  body = make_request(:get, {}, id: id)
  new(body)
end

#handle(code, &block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/arke/resource/model.rb', line 69

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



56
57
58
59
60
61
62
# File 'lib/arke/resource/model.rb', line 56

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



112
113
114
115
# File 'lib/arke/resource/model.rb', line 112

def host(host=nil)
  @host = host if host
  @host
end

#response_handlersObject



86
87
88
# File 'lib/arke/resource/model.rb', line 86

def response_handlers
  RESPONSE_HANDLERS
end

#table_nameObject



152
153
154
# File 'lib/arke/resource/model.rb', line 152

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



124
125
126
127
# File 'lib/arke/resource/model.rb', line 124

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



103
104
105
# File 'lib/arke/resource/model.rb', line 103

def url_template(template)
  self._url_template  = Addressable::Template.new(template)
end