Class: ESP::Resource

Inherits:
ActiveResource::Base
  • Object
show all
Defined in:
lib/esp/resources/resource.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.arrange_options(options) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/esp/resources/resource.rb', line 55

def self.arrange_options(options)
  if options[:params].present?
    page = options[:params][:page] ? { page: options[:params].delete(:page) } : {}
    options[:params].merge!(options[:params].delete(:filter)) if options[:params][:filter]
    options[:params] = filters(options[:params]).merge!(page)
  end
  if options[:include].present?
    options[:params] ||= {}
    options[:params].merge!(options.extract!(:include))
  end
end

.filters(params) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/esp/resources/resource.rb', line 33

def self.filters(params)
  h = {}.tap do |filters|
    params.each do |attr, value|
      if value.is_a? Enumerable
        filters["#{attr.sub(/_in$/, '')}_in"] = value
      else
        filters["#{attr.sub(/_eq$/, '')}_eq"] = value
      end
    end
  end
  { filter: h }
end

.find(*arguments) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/esp/resources/resource.rb', line 24

def self.find(*arguments)
  scope = arguments.slice!(0)
  options = (arguments.slice!(0) || {}).with_indifferent_access
  arrange_options(options)
  super(scope, options).tap do |object|
    make_pageable object, options
  end
end

.make_pageable(object, options) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/esp/resources/resource.rb', line 46

def self.make_pageable(object, options)
  return object unless object.is_a? ActiveResource::PaginatedCollection
  # Need to set from so paginated collection can use it for page calls.
  object.tap do |collection|
    collection.from = options['from']
    collection.original_params = options['params']
  end
end

.whereObject



11
12
13
# File 'lib/esp/resources/resource.rb', line 11

def self.where(*)
  fail ESP::NotImplementedError
end

Instance Method Details

#serializable_hashObject

Pass a json api compliant hash to the api.



16
17
18
19
20
21
22
# File 'lib/esp/resources/resource.rb', line 16

def serializable_hash(*)
  h = attributes.extract!('included')
  h['data'] = { 'type' => self.class.to_s.underscore.sub('esp/', '').pluralize,
                'attributes' => attributes.except('id', 'type', 'created_at', 'updated_at', 'relationships') }
  h['data']['id'] = id if id.present?
  h
end