Module: Braque::Model::ClassMethods

Includes:
Collection, Relations
Defined in:
lib/braque/model.rb

Instance Method Summary collapse

Methods included from Relations

#belongs_to, #has_many

Instance Method Details

#clientObject



118
119
120
121
122
123
124
125
# File 'lib/braque/model.rb', line 118

def client
  fail 'Please define api_root for all Braque::Model classes' unless config[:api_root_url]
  Hyperclient.new(config[:api_root_url]) do |client|
    client.headers['Http-Authorization'] = config[:http_authorization_header] if config[:http_authorization_header]
    client.headers['Authorization'] = config[:authorization_header] if config[:authorization_header]
    client.headers['Accept'] = config[:accept_header] if config[:accept_header]
  end
end

#collection_method_nameObject



110
111
112
# File 'lib/braque/model.rb', line 110

def collection_method_name
  (config[:remote_resource_name].present? ? config[:remote_resource_name] : name).tableize
end

#create(resource_params = {}, options = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/braque/model.rb', line 100

def create(resource_params = {}, options = {})
  response = client.method(collection_method_name)
             .call
             ._post(
               { "#{instance_method_name}" => resource_params }
                .merge(options)
             )
  new response
end

#find(options = {}) ⇒ Object



95
96
97
98
# File 'lib/braque/model.rb', line 95

def find(options = {})
  response = client.method(instance_method_name).call(options)
  new response
end

#instance_method_nameObject



114
115
116
# File 'lib/braque/model.rb', line 114

def instance_method_name
  collection_method_name.singularize
end

#list(options = {}) ⇒ Object



89
90
91
92
93
# File 'lib/braque/model.rb', line 89

def list(options = {})
  options = Hash[options.map { |k, v| [CGI.escape(k.to_s), v] }]
  response = client.method(collection_method_name).call(options)
  LinkedArray.new response, self
end