dada 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ = 0.1
2
+ === 24th May, 2008
3
+
4
+ Initial release of the dada ruby library.
data/README ADDED
@@ -0,0 +1,7 @@
1
+ = dada - A ruby interface to the Dada Entertainment API
2
+
3
+ This library will allow you to easily interface with the Dada Entertainment API, through ruby.
4
+
5
+ = Author
6
+
7
+ Copyright (c) 2008 by Edward Waller
@@ -0,0 +1,7 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'cgi'
4
+ require 'rexml/document'
5
+ require 'dada/dada_result'
6
+ require 'dada/dada_results'
7
+ require 'dada/dada_searcher'
@@ -0,0 +1,47 @@
1
+ module Dada
2
+ class DadaResult
3
+ def initialize(xml)
4
+ @xml = xml
5
+ @result = xml
6
+ end
7
+
8
+ attr_reader :xml
9
+
10
+ def content_id
11
+ @result.elements['contentId'].text
12
+ end
13
+
14
+ def title
15
+ @result.elements['title'].text
16
+ end
17
+
18
+ def artist_id
19
+ @result.elements['artistId'].text
20
+ end
21
+
22
+ def artist
23
+ @result.elements['artist'].text
24
+ end
25
+
26
+ def artist_url
27
+ @result.elements['artisturl'].text
28
+ end
29
+
30
+ def landing_page_artist_url
31
+ @result.elements['landingpageartisturl'].text
32
+ end
33
+
34
+ def landing_page_content_url
35
+ @result.elements['landingpagecontenturl'].text
36
+ end
37
+
38
+ def preview_url
39
+ @result.elements['previewURL'].text
40
+ end
41
+
42
+ def image_medium_url
43
+ @result.elements['imageMediumURL'].text
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,40 @@
1
+ module Dada
2
+ class DadaResults < Array
3
+ # Returns a ruby representation of dada search results.
4
+ def initialize(xml)
5
+ @xml = xml
6
+ doc = REXML::Document.new(xml)
7
+ @response = doc.root
8
+ @results = []
9
+ @response.elements.each('results/result') do |result|
10
+ self << DadaResult.new(result)
11
+ end
12
+ end
13
+
14
+ attr_reader :xml
15
+
16
+ def artist
17
+ @response.elements['searchRequest/artist'].text
18
+ end
19
+
20
+ def query_duration
21
+ @response.elements['searchRequest/queryDuration'].text.to_f
22
+ end
23
+
24
+ def url_path
25
+ @response.elements['searchRequest/urlPath'].text
26
+ end
27
+
28
+ def content_types
29
+ @response.elements['searchRequest/contentTypes'].text.split('|')
30
+ end
31
+
32
+ def total_result_count
33
+ @response.elements['searchRequest/totalResultCount'].text.to_i
34
+ end
35
+
36
+ def status
37
+ @response.elements['status'].text
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,84 @@
1
+ module Dada
2
+ class DadaSearcher
3
+ # Returns a new DadaSearcher object, with the specified api key, and optional partner key
4
+ def initialize(api_key, partner_key = nil)
5
+ @api_key = api_key
6
+ @partner_key = partner_key
7
+ end
8
+
9
+ attr_accessor :partner_key, :api_key
10
+
11
+ # Builds the url needed to make the api request.
12
+ def build_url(opts)
13
+ opts = setup_opts(opts)
14
+ url = URI.parse('http://api.dada-ent.com/')
15
+ url +=
16
+ if opts[:artist] and opts[:title]
17
+ "/search/artist/#{opts[:artist]}/title/#{opts[:title]}/"
18
+ elsif opts[:artist]
19
+ "/search/artist/#{opts[:artist]}/"
20
+ elsif opts[:title]
21
+ "/search/title/#{opts[:title]}/"
22
+ elsif opts[:keywords]
23
+ "/keywords/#{opts[:keywords].join('+')}/"
24
+ elsif opts[:category]
25
+ "/chart/category/#{opts[:category]}/"
26
+ end
27
+ url += "contenttypes/#{opts[:content_types].join('%7C')}/" if opts[:content_types]
28
+ url += "numresults/#{opts[:num_results]}/" if opts[:num_results]
29
+ url += "start/#{opts[:start]}/" if opts[:start]
30
+ url += "partner/#{opts[:partner_key]}/" if opts[:partner_key]
31
+
32
+ url
33
+ end
34
+
35
+ # Searches the Dada network for ringtones.
36
+ #
37
+ # Options are:
38
+ # * :artist - a musical artist
39
+ # * :title - title of a song
40
+ # * :keywords - an array of keywords to use
41
+ # * :category - the musical category the ringtone would be under
42
+ # * :content_types - an array of content types to search for (mp3, real, etc)
43
+ # * :num_results - the number of results you would like to receive
44
+ # * :start - the start index of the results
45
+ # * :partner_key - your partner key
46
+ #
47
+ # Examples:
48
+ # searcher = DadaSearcher.new('my-api-key')
49
+ #
50
+ # # find all ringtones with justin timberlake songs
51
+ # searcher.search(:artist => 'justin timberlake')
52
+ #
53
+ # # find the ringtone for justin timberlake's song "rock your body"
54
+ # searcher.search(:artist => 'justin timberlake', :title => 'Rock Your Body')
55
+ #
56
+ # # find the ringtones for the top rock songs on the chart
57
+ # searcher.search(:category => 'rock')
58
+ #
59
+ # # find all real and mp3 ringtones by justin timberlake
60
+ # searcher.search(:artist => 'justin timberlake', :content_types => ['real', 'mp3'])
61
+ def search(opts)
62
+ url = build_url(opts)
63
+ response = Net::HTTP.start(url.host) do |http|
64
+ http.get(url.path, 'API-Key' => @api_key)
65
+ end
66
+
67
+ DadaResults.new(response.body)
68
+ end
69
+
70
+ private
71
+
72
+ # Sets up the options for use by the url builder
73
+ def setup_opts(opts)
74
+ opts[:partner_key] ||= @partner_key if @partner_key
75
+ opts.each do |key, value|
76
+ if key == :keywords or key == :content_types
77
+ opts[key] = value.map { |x| CGI::escape(x) }
78
+ else
79
+ opts[key] = CGI::escape(value)
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,45 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ include Dada
3
+
4
+ describe DadaResult do
5
+ setup do
6
+ xml = File.read("spec/chris_brown.xml")
7
+ @result = DadaResults.new(xml).first
8
+ end
9
+
10
+ it "should have the correct content id" do
11
+ @result.content_id.should == '90563'
12
+ end
13
+
14
+ it "should have the correct artist id" do
15
+ @result.artist_id.should == '430'
16
+ end
17
+
18
+ it "should have the correct title" do
19
+ @result.title.should == 'Take You Down'
20
+ end
21
+
22
+ it "should have the correct artist" do
23
+ @result.artist.should == 'Chris Brown'
24
+ end
25
+
26
+ it "should have the correct artist url" do
27
+ @result.artist_url.should == 'http://us.dada.net/music/chrisbrown/'
28
+ end
29
+
30
+ it "should have the correct landing page artist url" do
31
+ @result.landing_page_artist_url.should == 'http://us.sso.dada.net/landing.php?artist=Chris+Brown&partner=XXX&artisturl=http://us.dada.net/music/chrisbrown/'
32
+ end
33
+
34
+ it "should have the correct landing page content url" do
35
+ @result.landing_page_content_url.should == 'http://us.sso.dada.net/landing.php?artist=Chris+Brown&partner=XXX&contentID=90563&artisturl=http://us.dada.net/music/chrisbrown/'
36
+ end
37
+
38
+ it "should have the correct image (medium) url" do
39
+ @result.image_medium_url.should == 'http://preview.dada.net/mobi/9/0/5/90563/90563_m.gif'
40
+ end
41
+
42
+ it "should have the correct mp3 preview url" do
43
+ @result.preview_url.should == 'http://preview.dada.net/mobi/9/0/5/90563/90563_20.mp3'
44
+ end
45
+ end
@@ -0,0 +1,38 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ include Dada
3
+
4
+ describe DadaResults do
5
+ setup do
6
+ xml = File.read("spec/chris_brown.xml")
7
+ @results = DadaResults.new(xml)
8
+ end
9
+
10
+ it "should have the correct artist" do
11
+ @results.artist.should == 'chris brown'
12
+ end
13
+
14
+ it "should have the correct status" do
15
+ @results.status.should == 'success'
16
+ end
17
+
18
+ it "should have the correct query duration" do
19
+ @results.query_duration.should == 0.0037
20
+ end
21
+
22
+ it "should have the correct url path" do
23
+ @results.url_path.should == '/search/artist/chris brown/contenttypes/mp3|real/'
24
+ end
25
+
26
+ it "should have the correct content types" do
27
+ @results.content_types.should == ['mp3', 'real']
28
+ end
29
+
30
+ it "should have the correct total result count" do
31
+ @results.total_result_count.should == 10
32
+ end
33
+
34
+ it "should have the same number of results as specified" do
35
+ @results.total_result_count.should == @results.size
36
+ end
37
+
38
+ end
@@ -0,0 +1,79 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ include Dada
3
+
4
+ describe DadaSearcher do
5
+ setup do
6
+ @dada = DadaSearcher.new('')
7
+ end
8
+ describe "building urls" do
9
+ it "should work with an artist" do
10
+ @dada.build_url(:artist => 'justin timberlake').to_s.should ==
11
+ 'http://api.dada-ent.com/search/artist/justin+timberlake/'
12
+ end
13
+
14
+ it "should work with an artist and title" do
15
+ @dada.build_url(:artist => 'justin timberlake', :title => 'cry me a river').to_s.should ==
16
+ 'http://api.dada-ent.com/search/artist/justin+timberlake/title/cry+me+a+river/'
17
+ end
18
+
19
+ it "should work with a title" do
20
+ @dada.build_url(:title => 'cry me a river').to_s.should ==
21
+ 'http://api.dada-ent.com/search/title/cry+me+a+river/'
22
+ end
23
+
24
+ it "should work with a single keyword" do
25
+ @dada.build_url(:keywords => ['justin']).to_s.should ==
26
+ 'http://api.dada-ent.com/keywords/justin/'
27
+ end
28
+
29
+ it "should work with multiple keywords" do
30
+ @dada.build_url(:keywords => %w{justin timberlake}).to_s.should ==
31
+ 'http://api.dada-ent.com/keywords/justin+timberlake/'
32
+ end
33
+
34
+ it "should work with categories" do
35
+ @dada.build_url(:category => 'rock').to_s.should ==
36
+ 'http://api.dada-ent.com/chart/category/rock/'
37
+ end
38
+
39
+ it "should work with a single content type" do
40
+ @dada.build_url(:artist => 'justin timberlake', :content_types => ['mp3']).to_s.should ==
41
+ 'http://api.dada-ent.com/search/artist/justin+timberlake/contenttypes/mp3/'
42
+ end
43
+
44
+ it "should work with multiple content types" do
45
+ @dada.build_url(:artist => 'justin timberlake', :content_types => ['mp3', 'real']).to_s.should ==
46
+ 'http://api.dada-ent.com/search/artist/justin+timberlake/contenttypes/mp3%7Creal/'
47
+ end
48
+
49
+ it "should work with a start index" do
50
+ @dada.build_url(:artist => 'justin timberlake', :start => '5').to_s.should ==
51
+ 'http://api.dada-ent.com/search/artist/justin+timberlake/start/5/'
52
+ end
53
+
54
+ it "should work with a specified number of results" do
55
+ @dada.build_url(:artist => 'justin timberlake', :num_results => '5').to_s.should ==
56
+ 'http://api.dada-ent.com/search/artist/justin+timberlake/numresults/5/'
57
+ end
58
+
59
+ it "should work with a specified number of results and a start index" do
60
+
61
+ @dada.build_url(:artist => 'justin timberlake', :num_results => '5', :start => '10').to_s.should ==
62
+ 'http://api.dada-ent.com/search/artist/justin+timberlake/numresults/5/start/10/'
63
+ end
64
+
65
+ it "should work with a specified partner id" do
66
+ @dada.build_url(:artist => 'justin timberlake', :partner_key => '12345').to_s.should ==
67
+ 'http://api.dada-ent.com/search/artist/justin+timberlake/partner/12345/'
68
+ end
69
+
70
+ it "should override the partner key if specified" do
71
+ @dada.partner_key = '54321'
72
+ @dada.build_url(:artist => 'justin timberlake').to_s.should ==
73
+ 'http://api.dada-ent.com/search/artist/justin+timberlake/partner/54321/'
74
+ @dada.build_url(:artist => 'justin timberlake', :partner_key => '12345').to_s.should ==
75
+ 'http://api.dada-ent.com/search/artist/justin+timberlake/partner/12345/'
76
+ end
77
+ end
78
+
79
+ end
@@ -0,0 +1,2 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'dada'
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dada
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Edward Waller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-24 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/dada/dada_result.rb
26
+ - lib/dada/dada_results.rb
27
+ - lib/dada/dada_searcher.rb
28
+ - lib/dada.rb
29
+ - spec/dada_result_spec.rb
30
+ - spec/dada_results_spec.rb
31
+ - spec/dada_searcher_spec.rb
32
+ - spec/spec_helper.rb
33
+ - README
34
+ - CHANGELOG
35
+ has_rdoc: true
36
+ homepage: http://wallerdev.com/
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project: dada
57
+ rubygems_version: 1.0.1
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: A library for interfacing with the Dada Entertainment API.
61
+ test_files: []
62
+