Class: Spot::Search
- Inherits:
-
Object
- Object
- Spot::Search
- Defined in:
- lib/spot.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#clean!(value) ⇒ Object
String A cleaned string.
- #find(type, all, s) ⇒ Object
-
#initialize ⇒ Search
constructor
A new instance of Search.
- #method_missing(method, *args, &blk) ⇒ Object
- #page(value) ⇒ Object
- #prefix(value) ⇒ Object
- #prime ⇒ Object
- #result ⇒ Object
- #results ⇒ Object
- #strip ⇒ Object
- #territory(value) ⇒ Object
Constructor Details
#initialize ⇒ Search
Returns a new instance of Search.
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 44 45 46 |
# File 'lib/spot.rb', line 15 def initialize @methods = { :artists => { :selector => :artists, :class => Spot::Artist, :url => generate_url("artist") }, :songs => { :selector => :tracks, :class => Spot::Song, :url => generate_url("track") }, :albums => { :selector => :albums, :class => Spot::Album, :url => generate_url("album") } } @cache = {} @exclude = YAML.load(File.read("#{File.dirname(__FILE__)}/spot/ignore.yml")) @config = { :exclude => 2, :popularity => 7, :limit => 0.7, :offset => 10 } @options = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &blk) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/spot.rb', line 52 def method_missing(method, *args, &blk) if method.to_s =~ /^find(_all)?_([a-z]+)$/i find($2, !!$1, args.first) elsif scrape and content["info"].keys.include?(method.to_s) content["info"][method.to_s] else super(method, *args, &blk) end end |
Class Method Details
Instance Method Details
#clean!(value) ⇒ Object
Returns String A cleaned string.
113 114 115 |
# File 'lib/spot.rb', line 113 def clean!(value) Spot::Clean.new(value).process end |
#find(type, all, s) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/spot.rb', line 74 def find(type, all, s) tap { @search = s @type = all ? type.to_sym : "#{type}s".to_sym raise NoMethodError.new(@type) unless @methods.keys.include?(@type) } end |
#page(value) ⇒ Object
62 63 64 |
# File 'lib/spot.rb', line 62 def page(value) tap { @page = value } end |
#prefix(value) ⇒ Object
70 71 72 |
# File 'lib/spot.rb', line 70 def prefix(value) tap { @prefix = value } end |
#prime ⇒ Object
66 67 68 |
# File 'lib/spot.rb', line 66 def prime tap { @prime = true } end |
#result ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/spot.rb', line 98 def result if @prime Spot::Prime.new(results.sort_by(&:popularity).reverse[0..6], search). results. select(&:valid?). first else results.select(&:valid?).first end end |
#results ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/spot.rb', line 82 def results unless @prime @_results ||= scrape.select(&:valid?) else @_results ||= scrape end end |
#strip ⇒ Object
90 91 92 |
# File 'lib/spot.rb', line 90 def strip tap { @strip = true } end |
#territory(value) ⇒ Object
94 95 96 |
# File 'lib/spot.rb', line 94 def territory(value) tap { @options.merge!(:territory => value) } end |