next-big-sound 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -28,28 +28,36 @@ This code was written by jeff at musicxray.com if you think you can do better se
28
28
  # get the first artist that you find and find
29
29
  # all associated profiles. myspace, ilike ....
30
30
 
31
- profiles = searchobj.artists.first.profiles
32
-
31
+ artist = searchobj.artists.first
32
+
33
+ artist.profiles
33
34
  # get the url for a specific service
34
35
 
35
- profiles["MYSPACE"]
36
-
36
+ artist.profiles["MYSPACE"]
37
37
  # in addition. There is are magic method for retrieving data
38
38
  # if you want myspace plays for example you would get it like this
39
39
 
40
- artist = searchobj.artists.first
41
-
42
- # this will fetch you a month worth of myspace plays data
43
- artist.myspace_plays("1/1/2010","2/1/2010")
40
+ # Each profile can have multiple metrics
41
+ # each metric has an array of datapoints.
42
+ # datapoints consist of a DateTime Object and a float
43
+ # example usage follows
44
+
45
+ profile = artist.profiles["MYSPACE"]
46
+
47
+ metrics = profile.metrics("4/24/2009","4/24/2010")
48
+ datapoints = metrics["plays"].data_points
44
49
 
45
- # this will find all fans for a particular artist across all
46
- # services.
47
- artist.all_fans("1/1/2010","2/1/2010")
50
+ #The datapoints array can then be iterated over.
48
51
 
49
- # The data points are returned as an array of datasets
50
- # One dataset per profile Datasets can be sorted by date
52
+ #shortcut methods have been provided for convenience is as follows
53
+
54
+ artist.myspace_plays_datapoints("4/24/2009","4/24/2010")
55
+
56
+ This will return the datapoints you are looking for ....
57
+
58
+
59
+
51
60
 
52
- # TODO: this stuff is still be implemented.
53
61
 
54
62
 
55
63
  == Note on Patches/Pull Requests
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
data/lib/artist.rb CHANGED
@@ -3,13 +3,15 @@ module NBS
3
3
 
4
4
  attr_accessor :artist_id, :name, :xml, :options
5
5
 
6
+ PROFILES = %w(all myspace lastfm ilike facebook twitter youtube reverbnation ourstage soundcloud purevolume bebo virb amiestreet jamlegend vimeo)
7
+
6
8
  def initialize(artist_id, name="",options={})
7
9
  self.artist_id=artist_id
8
10
  self.name = name
9
11
  self.options = {"artistID"=>self.artist_id,"apiKey"=>$nbs_api_key,"format"=>"xml"}.merge(options)
10
12
  end
11
13
 
12
- def fetch_profile
14
+ def fetch_profiles
13
15
  self.xml=Net::HTTP.get(URI.parse("#{NBS::NBS_CONFIG["base_url"]}getProfilesForArtist?#{self.options.to_url_params}")).to_s
14
16
  end
15
17
 
@@ -18,27 +20,22 @@ module NBS
18
20
  ####
19
21
  profs = {}
20
22
  self.to_hash["Profiles"][0]["Profile"].each do |item|
21
- profs[item["service"]]=item["url"]
23
+ profs[item["service"].to_s]=ArtistProfile.new(self.artist_id, item["service"] ,item["url"])
22
24
  end
23
25
  return profs
24
26
  end
25
-
26
27
  def to_xml
27
- self.xml ||= fetch_profile
28
+ self.xml ||= fetch_profiles
28
29
  end
29
30
  def to_hash
30
31
  Hash.from_xml(self.to_xml)
31
32
  end
32
33
 
33
34
  def method_missing(method_name,*args)
34
- if splits = method_name.to_s.split("_")
35
- sdate = Date.parse(args[0].to_s)
36
- edate = Date.parse(args[1].to_s)
37
- opts = options.merge({"service"=>splits[0],"metric"=>splits[1],"start"=>sdate.to_s,"end"=>edate.to_s})
38
- return Net::HTTP.get(URI.parse("#{NBS::NBS_CONFIG["base_url"]}getDataForArtist?#{opts.to_url_params}")).to_s
39
- else
40
- raise "Sorry that method does not exist. The proper format for finding data is service_metric(start,end)"
41
- end
35
+ splits = method_name.to_s.split("_")
36
+ prof = self.profiles[splits[0].upcase]
37
+ metrics = prof.metrics(args[0],args[1])
38
+ return metrics[splits[1].downcase.to_s].data_points
42
39
  end
43
40
 
44
41
  end
@@ -0,0 +1,26 @@
1
+ module NBS
2
+
3
+ class ArtistProfile
4
+ METRICS = %w(plays fans views comments downloads likes price)
5
+
6
+ attr_accessor :service_type,:url, :artist_id, :options
7
+
8
+ def initialize(artist_id,service_type, url="" ,options={})
9
+ self.service_type = service_type
10
+ self.artist_id = artist_id
11
+ self.url = url
12
+ end
13
+
14
+ def metrics(sdate,edate)
15
+ @metrics ||= load_metrics(sdate,edate)
16
+ end
17
+
18
+ def load_metrics(sdate,edate)
19
+ metrics ={}
20
+ METRICS.each do |metric_string|
21
+ metrics[metric_string.to_s] = Metric.new(self.artist_id,self.service_type,metric_string.to_s,sdate,edate)
22
+ end
23
+ return metrics
24
+ end
25
+ end
26
+ end
data/lib/datapoint.rb ADDED
@@ -0,0 +1,12 @@
1
+ module NBS
2
+ class DataPoint
3
+
4
+ attr_accessor :time, :value
5
+
6
+ def initialize(time,value)
7
+ self.time = DateTime.parse(time.to_s)
8
+ self.value = value.to_f
9
+ end
10
+
11
+ end
12
+ end
data/lib/metric.rb ADDED
@@ -0,0 +1,37 @@
1
+ module NBS
2
+
3
+ class Metric
4
+
5
+ attr_accessor :artist_id,:service_type,:metric,:sdate,:edate, :xml
6
+ def initialize(artist_id,service_type,metric,sdate,edate)
7
+ self.artist_id = artist_id
8
+ self.service_type = service_type
9
+ self.metric = metric
10
+ self.sdate = Date.parse(sdate.to_s)
11
+ self.edate = Date.parse(edate.to_s)
12
+ end
13
+ def fetch
14
+ options = {"service"=>self.service_type,"metric"=>self.metric,"start"=>self.sdate.to_s,"end"=>self.edate.to_s,"artistID"=>self.artist_id,"apiKey"=>$nbs_api_key,"format"=>"xml"}
15
+ puts "#{NBS::NBS_CONFIG["base_url"]}getDataForArtist?#{options.to_url_params}"
16
+ statsxml = Net::HTTP.get(URI.parse("#{NBS::NBS_CONFIG["base_url"]}getDataForArtist?#{options.to_url_params}")).to_s
17
+ end
18
+ def data_points
19
+ begin
20
+ points = to_hash["Profiles"][0]["Profile"][0]["DataPoint"]
21
+ dps = []
22
+ points.each do |dp|
23
+ dps << DataPoint.new(dp["date"],dp["value"])
24
+ end
25
+ return dps
26
+ rescue
27
+ return []
28
+ end
29
+ end
30
+ def to_xml
31
+ self.xml ||= fetch
32
+ end
33
+ def to_hash
34
+ Hash.from_xml(to_xml)
35
+ end
36
+ end
37
+ end
@@ -5,6 +5,10 @@ module NBS
5
5
  require 'hash_extension'
6
6
  require 'search'
7
7
  require 'artist'
8
+ require 'artist_profile'
9
+ require 'metric'
10
+ require 'datapoint'
11
+
8
12
 
9
13
  NBS_CONFIG = YAML.load_file("config.yml")
10
14
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{next-big-sound}
8
- s.version = "0.1.2"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["jeff durand"]
12
- s.date = %q{2010-03-27}
12
+ s.date = %q{2010-03-29}
13
13
  s.description = %q{A simple wrapper class for the Next Big Sound api. docs for the api can be found at api.nextbigsound.com}
14
14
  s.email = %q{jeff.durand@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -25,7 +25,10 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "config.yml",
27
27
  "lib/artist.rb",
28
+ "lib/artist_profile.rb",
29
+ "lib/datapoint.rb",
28
30
  "lib/hash_extension.rb",
31
+ "lib/metric.rb",
29
32
  "lib/next-big-sound.rb",
30
33
  "lib/search.rb",
31
34
  "next-big-sound.gemspec",
data/test/helper.rb CHANGED
@@ -4,7 +4,7 @@ require 'shoulda'
4
4
 
5
5
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
6
  $LOAD_PATH.unshift(File.dirname(__FILE__))
7
- require 'base'
7
+ require 'next-big-sound'
8
8
 
9
9
  class Test::Unit::TestCase
10
10
  end
data/test/test_search.rb CHANGED
@@ -22,8 +22,8 @@ class TestNextBigSound < Test::Unit::TestCase
22
22
  should "have first result with artist name of The Killers" do
23
23
  assert_equal "The Killers", @searchobj.artists.first.name
24
24
  end
25
- should "be able to get some data" do
26
- assert @searchobj.artists.first.myspace_plays("4/24/2009","4/24/2010").is_a?(Array)
25
+ should "metrics for profile" do
26
+ assert @searchobj.artists.first.profiles.first.is_a?(Profile)
27
27
  end
28
28
  end
29
29
  #should "have many re"
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
7
  - 2
9
- version: 0.1.2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - jeff durand
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-27 00:00:00 -04:00
17
+ date: 2010-03-29 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -47,7 +47,10 @@ files:
47
47
  - VERSION
48
48
  - config.yml
49
49
  - lib/artist.rb
50
+ - lib/artist_profile.rb
51
+ - lib/datapoint.rb
50
52
  - lib/hash_extension.rb
53
+ - lib/metric.rb
51
54
  - lib/next-big-sound.rb
52
55
  - lib/search.rb
53
56
  - next-big-sound.gemspec