Class: Luchadeer::Search
- Inherits:
-
Object
- Object
- Luchadeer::Search
- Defined in:
- lib/luchadeer/search.rb
Constant Summary collapse
- RESOURCE_TYPES =
{ 'character' => Luchadeer::Character, 'company' => Luchadeer::Company, 'concept' => Luchadeer::Concept, 'franchise' => Luchadeer::Franchise, 'game' => Luchadeer::Game, 'location' => Luchadeer::Location, 'object' => Luchadeer::Object, 'person' => Luchadeer::Person, 'video' => Luchadeer::Video }
- QUERY_PARAMS =
[:query, :limit, :page, :fields, :resources, :sort]
Instance Method Summary collapse
- #fetch ⇒ Object
-
#initialize(opts = {}) {|_self| ... } ⇒ Search
constructor
A new instance of Search.
- #resources(resources = nil) ⇒ Object
- #sort(attribute = nil, dir = :asc) ⇒ Object
Constructor Details
#initialize(opts = {}) {|_self| ... } ⇒ Search
Returns a new instance of Search.
17 18 19 20 21 22 23 |
# File 'lib/luchadeer/search.rb', line 17 def initialize(opts = {}) opts.each do |key, value| send(:"#{key}=", value) end yield self if block_given? end |
Instance Method Details
#fetch ⇒ Object
25 26 27 28 29 30 |
# File 'lib/luchadeer/search.rb', line 25 def fetch Luchadeer.client.get('search', search_params).body[:results].map do |r| type = r.delete(:resource_type) RESOURCE_TYPES[type] && RESOURCE_TYPES[type].new(r) end.compact end |
#resources(resources = nil) ⇒ Object
44 45 46 47 48 |
# File 'lib/luchadeer/search.rb', line 44 def resources(resources = nil) return @resources unless resources append_resources(resources) self end |
#sort(attribute = nil, dir = :asc) ⇒ Object
50 51 52 53 54 |
# File 'lib/luchadeer/search.rb', line 50 def sort(attribute = nil, dir = :asc) return @sort unless attribute @sort = "#{attribute}:#{dir}" self end |