Class: Dada::DadaSearcher
- Inherits:
-
Object
- Object
- Dada::DadaSearcher
- Defined in:
- lib/dada/dada_searcher.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#partner_key ⇒ Object
Returns the value of attribute partner_key.
Instance Method Summary collapse
-
#build_url(opts) ⇒ Object
Builds the url needed to make the api request.
-
#initialize(api_key, partner_key = nil) ⇒ DadaSearcher
constructor
Returns a new DadaSearcher object, with the specified api key, and optional partner key.
-
#search(opts) ⇒ Object
Searches the Dada network for ringtones.
Constructor Details
#initialize(api_key, partner_key = nil) ⇒ DadaSearcher
Returns a new DadaSearcher object, with the specified api key, and optional partner key
4 5 6 7 |
# File 'lib/dada/dada_searcher.rb', line 4 def initialize(api_key, partner_key = nil) @api_key = api_key @partner_key = partner_key end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key
9 10 11 |
# File 'lib/dada/dada_searcher.rb', line 9 def api_key @api_key end |
#partner_key ⇒ Object
Returns the value of attribute partner_key
9 10 11 |
# File 'lib/dada/dada_searcher.rb', line 9 def partner_key @partner_key end |
Instance Method Details
#build_url(opts) ⇒ Object
Builds the url needed to make the api request.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/dada/dada_searcher.rb', line 12 def build_url(opts) opts = setup_opts(opts) url = URI.parse('http://api.dada-ent.com/') url += if opts[:artist] and opts[:title] "/search/artist/#{opts[:artist]}/title/#{opts[:title]}/" elsif opts[:artist] "/search/artist/#{opts[:artist]}/" elsif opts[:title] "/search/title/#{opts[:title]}/" elsif opts[:keywords] "/keywords/#{opts[:keywords].join('+')}/" elsif opts[:category] "/chart/category/#{opts[:category]}/" end url += "contenttypes/#{opts[:content_types].join('%7C')}/" if opts[:content_types] url += "numresults/#{opts[:num_results]}/" if opts[:num_results] url += "start/#{opts[:start]}/" if opts[:start] url += "partner/#{opts[:partner_key]}/" if opts[:partner_key] url end |
#search(opts) ⇒ Object
Searches the Dada network for ringtones.
Options are:
-
:artist - a musical artist
-
:title - title of a song
-
:keywords - an array of keywords to use
-
:category - the musical category the ringtone would be under
-
:content_types - an array of content types to search for (mp3, real, etc)
-
:num_results - the number of results you would like to receive
-
:start - the start index of the results
-
:partner_key - your partner key
Examples:
searcher = DadaSearcher.new('my-api-key')
# find all ringtones with justin timberlake songs
searcher.search(:artist => 'justin timberlake')
# find the ringtone for justin timberlake's song "rock your body"
searcher.search(:artist => 'justin timberlake', :title => 'Rock Your Body')
# find the ringtones for the top rock songs on the chart
searcher.search(:category => 'rock')
# find all real and mp3 ringtones by justin timberlake
searcher.search(:artist => 'justin timberlake', :content_types => ['real', 'mp3'])
61 62 63 64 65 66 67 68 |
# File 'lib/dada/dada_searcher.rb', line 61 def search(opts) url = build_url(opts) response = Net::HTTP.start(url.host) do |http| http.get(url.path, 'API-Key' => @api_key) end DadaResults.new(response.body) end |