Class: ESP::Resource
- Inherits:
-
ActiveResource::Base
- Object
- ActiveResource::Base
- ESP::Resource
- Defined in:
- lib/esp/resources/resource.rb
Overview
:nodoc:
Direct Known Subclasses
Alert, CloudTrailEvent, ContactRequest, CustomSignature, CustomSignature::Definition, CustomSignature::Result, CustomSignature::Result::Alert, Dashboard, ExternalAccount, Metadata, Organization, Region, Report, ESP::Report::Export::Integration, ScanInterval, Service, Signature, Stat, StatCustomSignature, StatRegion, StatService, StatSignature, SubOrganization, Suppression, Suppression::Region, Suppression::Signature, Suppression::UniqueIdentifier, Tag, Team, User
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
- .arrange_options(options) ⇒ Object
-
.filters(params) ⇒ Object
rubocop:disable Metrics/MethodLength.
- .find(*arguments) ⇒ Object
-
.make_pageable(object, options = {}) ⇒ Object
rubocop:disable Style/OptionHash.
- .where(clauses = {}) ⇒ Object
Instance Method Summary collapse
-
#serializable_hash ⇒ Object
Pass a json api compliant hash to the api.
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.() if [:params].present? page = [:params][:page] ? { page: [:params].delete(:page) } : {} include = [:params][:include] ? { include: [:params].delete(:include) } : {} [:params].merge!([:params].delete(:filter)) if [:params][:filter] [:params] = filters([:params]).merge!(page).merge!(include) end if [:include].present? [:params] ||= {} [:params].merge!(.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) = (arguments.slice!(0) || {}).with_indifferent_access () super(scope, ).tap do |object| make_pageable object, 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, = {}) # rubocop:disable Style/OptionHash = .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 = ['from'] collection.original_params = ['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 } (clauses) , = (clauses) instantiate_collection((format.decode(connection.put("#{from}.json", clauses[:params].to_json).body) || []), , ).tap do |collection| make_pageable collection, clauses.merge(from: from) end end |
Instance Method Details
#serializable_hash ⇒ Object
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 |