Class: Luchadeer::Search

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(opts = {}) {|_self| ... } ⇒ Search

Returns a new instance of Search.

Yields:

  • (_self)

Yield Parameters:



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

#fetchObject



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