Class: ESP::Resource

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

Overview

:nodoc:

Constant Summary collapse

PREDICATES =

List of predicates that can be used for searching

%w(sorts m eq eq_any eq_all not_eq not_eq_any not_eq_all matches matches_any matches_all does_not_match does_not_match_any does_not_match_all lt lt_any lt_all lteq lteq_any lteq_all gt gt_any gt_all gteq gteq_any gteq_all in in_any in_all not_in not_in_any not_in_all cont cont_any cont_all not_cont not_cont_any not_cont_all start start_any start_all not_start not_start_any not_start_all end end_any end_all not_end not_end_any not_end_all true false present blank null not_null).join('|').freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.arrange_options(options) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/esp/resources/resource.rb', line 70

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

.filters(params) ⇒ Object

rubocop:disable Metrics/MethodLength



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/esp/resources/resource.rb', line 44

def self.filters(params) # rubocop:disable Metrics/MethodLength
  h = {}.tap do |filters|
    params.each do |attr, value|
      unless attr =~ /(#{PREDICATES})$/
        attr = if value.is_a? Enumerable
                 "#{attr}_in"
               else
                 "#{attr}_eq"
               end
      end
      filters[attr] = value
    end
  end
  { filter: h }
end

.find(*arguments) ⇒ Object



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

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

rubocop:disable Style/OptionHash



60
61
62
63
64
65
66
67
68
# File 'lib/esp/resources/resource.rb', line 60

def self.make_pageable(object, options = {}) # rubocop:disable Style/OptionHash
  options = options.with_indifferent_access
  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

.where(clauses = {}) ⇒ Object



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

def self.where(clauses = {})
  fail ArgumentError, "expected a clauses Hash, got #{clauses.inspect}" unless clauses.is_a? Hash
  from = clauses.delete(:from) || "#{prefix}#{name.demodulize.pluralize.underscore}"
  clauses = { params: clauses }
  arrange_options(clauses)
  prefix_options, query_options = split_options(clauses)
  instantiate_collection((format.decode(connection.put("#{from}.json", clauses[:params].to_json).body) || []), query_options, prefix_options).tap do |collection|
    make_pageable collection, clauses.merge(from: from)
  end
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