class ESP::Resource
@private
Constants
- PREDICATES
List of predicates that can be used for searching
Public Class Methods
arrange_options(options)
click to toggle source
# File lib/esp/resources/resource.rb, line 71 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 return unless options[:include].present? options[:params] ||= {} options[:params].merge!(options.extract!(:include)) end
filters(params)
click to toggle source
# File lib/esp/resources/resource.rb, line 45 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)
click to toggle source
Calls superclass method
# File lib/esp/resources/resource.rb, line 36 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 = {})
click to toggle source
# File lib/esp/resources/resource.rb, line 61 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.fetch('params', {}) end end
where(clauses = {})
click to toggle source
# File lib/esp/resources/resource.rb, line 25 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 }.with_indifferent_access 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
Public Instance Methods
serializable_hash(*)
click to toggle source
Pass a json api compliant hash to the api.
# File lib/esp/resources/resource.rb, line 17 def serializable_hash(*) h = attributes.extract!('included') h['data'] = { 'type' => self.class.to_s.underscore.sub('esp/', '').pluralize, 'attributes' => changed_attributes.except('id', 'type', 'created_at', 'updated_at', 'relationships') } h['data']['id'] = id if id.present? h end