Module: Ey::Core::Mock::Searching
- Included in:
- Client::Mock
- Defined in:
- lib/ey-core/mock/searching.rb
Instance Method Summary collapse
- #page(params, collection, options = {}) ⇒ Object
- #pluralize(word) ⇒ Object
-
#search(resources, params) ⇒ Object
Filter a mocked data collection by given search parameters.
- #search_and_page(params, collection, options = {}) ⇒ Object
- #url_for_page(collection, page_index, page_size, rel) ⇒ Object
Instance Method Details
#page(params, collection, options = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ey-core/mock/searching.rb', line 7 def page(params, collection, ={}) if params["url"] raise "url param should be extracted before paging" end unless resources = .delete(:resources) raise "tried to `page` #{collection} without given `resources` option." end if .any? raise ArgumentError, "Unknown options: #{.keys}" end page_size = (params["per_page"] || 20).to_i page_index = (params["page"] || 1).to_i total_pages = (resources.size.to_f / page_size.to_f).ceil offset = (page_index - 1) * page_size links = [] resource_page = resources.values.reverse.slice(offset, page_size) if page_index < total_pages links << url_for_page(collection, page_index + 1, page_size, 'next') end if page_index - 1 > 0 links << url_for_page(collection, page_index - 1, page_size, 'prev') end links << url_for_page(collection, total_pages, page_size, 'last') headers = { "Link" => links.join(", "), "X-Total-Count" => resources.size } [headers, resource_page] end |
#pluralize(word) ⇒ Object
45 46 47 48 |
# File 'lib/ey-core/mock/searching.rb', line 45 def pluralize(word) # @fixme make it better "#{word}s" end |
#search(resources, params) ⇒ Object
Filter a mocked data collection by given search parameters
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ey-core/mock/searching.rb', line 53 def search(resources, params) search_params = Cistern::Hash.stringify_keys(params) search_params.select { |k,v| v.respond_to?(:id) }.each do |k,v| search_params[k] = v.id end resources.select do |id, attrs| search_params.all? do |term, condition| if condition.kind_of? Array condition.include?(attrs[term]) || condition.include?(attrs["_#{term}"]) else # @fixme the search functions should define more accurate / explicit definition of search terms # this is a cheap mapping # [provider, 3] => "http://api-test.engineyard.com/providers/3" resource_url = url_for("/#{pluralize(term.to_s).gsub("_", "-")}/#{condition}") hidden_term = "_#{term}" ( attrs.key?(term) && ( attrs[term] == resource_url || # find by relation to a specific resource attrs[term] == condition # find by direct attribute ) ) || ( attrs.key?(hidden_term) && (attrs[hidden_term] == condition) # find by a hidden attribute ) end end end end |
#search_and_page(params, collection, options = {}) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ey-core/mock/searching.rb', line 91 def search_and_page(params, collection, ={}) deleted_key = .delete(:deleted_key) || "deleted_at" resources = (.delete(:resources) || self.data[collection]) or raise ArgumentError, "Could not find collection: #{collection}" search_keys = .delete(:search_keys) or raise ArgumentError, "Missing required option 'search_keys'" search_params = Cistern::Hash.slice(params, *search_keys) unless (bad_params = params.keys - search_keys - %w[deleted with_deleted page per_page]).empty? raise "Cannot search #{collection} by params: #{bad_params.join(', ')}" end @logger.debug('request.search') { "Searching #{collection} with: #{params.inspect}" } resources = search(resources, search_params) # filter out deleted resources by default filter = lambda { |_, attrs| attrs[deleted_key] } unless params["with_deleted"] params["deleted"] ? resources.select!(&filter) : resources.reject!(&filter) end page(params, collection, .merge(resources: resources)) end |
#url_for_page(collection, page_index, page_size, rel) ⇒ Object
3 4 5 |
# File 'lib/ey-core/mock/searching.rb', line 3 def url_for_page(collection, page_index, page_size, rel) "<#{url_for(collection)}?page=#{page_index}&per_page=#{page_size}>; rel=\"#{rel}\"" end |