idiomag 0.1.0

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.
Files changed (46) hide show
  1. data/History +2 -0
  2. data/LICENSE +9 -0
  3. data/README.rdoc +151 -0
  4. data/Rakefile +54 -0
  5. data/VERSION.yml +4 -0
  6. data/lib/idiomag.rb +17 -0
  7. data/lib/idiomag/articles.rb +48 -0
  8. data/lib/idiomag/artist.rb +103 -0
  9. data/lib/idiomag/base.rb +9 -0
  10. data/lib/idiomag/helpers.rb +26 -0
  11. data/lib/idiomag/parser.rb +32 -0
  12. data/lib/idiomag/recommendation.rb +70 -0
  13. data/lib/idiomag/rest.rb +20 -0
  14. data/lib/idiomag/tag.rb +106 -0
  15. data/lib/idiomag/user.rb +114 -0
  16. data/spec/articles_spec.rb +61 -0
  17. data/spec/artist_spec.rb +149 -0
  18. data/spec/base_spec.rb +15 -0
  19. data/spec/fixtures/articles_featured.json +1 -0
  20. data/spec/fixtures/articles_latest.json +1 -0
  21. data/spec/fixtures/artist_articles.json +1 -0
  22. data/spec/fixtures/artist_info.json +1 -0
  23. data/spec/fixtures/artist_photos.json +1 -0
  24. data/spec/fixtures/artist_playlist.json +1 -0
  25. data/spec/fixtures/artist_tags.json +1 -0
  26. data/spec/fixtures/artist_videos.json +1 -0
  27. data/spec/fixtures/recommendation_articles.json +1 -0
  28. data/spec/fixtures/recommendation_artists.json +1 -0
  29. data/spec/fixtures/tag_articles.json +1 -0
  30. data/spec/fixtures/tag_artists.json +1 -0
  31. data/spec/fixtures/tag_list.txt +144 -0
  32. data/spec/fixtures/tag_photos.json +1 -0
  33. data/spec/fixtures/tag_playlist.json +1 -0
  34. data/spec/fixtures/tag_videos.json +1 -0
  35. data/spec/fixtures/user_articles.json +1 -0
  36. data/spec/fixtures/user_info.json +1 -0
  37. data/spec/fixtures/user_lovedarticles.json +1 -0
  38. data/spec/fixtures/user_photos.json +1 -0
  39. data/spec/fixtures/user_playlist.json +1 -0
  40. data/spec/fixtures/user_videos.json +1 -0
  41. data/spec/recommendation_spec.rb +88 -0
  42. data/spec/rest_spec.rb +41 -0
  43. data/spec/spec_helper.rb +15 -0
  44. data/spec/tag_spec.rb +149 -0
  45. data/spec/user_spec.rb +169 -0
  46. metadata +112 -0
@@ -0,0 +1,70 @@
1
+ module Idiomag
2
+ class Recommendation
3
+ ValidNetworks = [:lastfm,:mog,:ilike,:mystrands,:projectplaylist,:imeem,:pandora,:bebo,:myspace,:songza]
4
+
5
+ def initialize(options)
6
+ if !options[:network].blank? && !options[:user].blank?
7
+ @user = options[:user]
8
+ @network = options[:network]
9
+ raise ArgumentError if !ValidNetworks.include?(@network)
10
+ @query = {:network => @network,:username => @user}
11
+ elsif !options[:apml].blank?
12
+ @apml = options[:apml]
13
+ @query = {:apml => @apml}
14
+ elsif !options[:artists].blank? && options[:artists].is_a?(Array)
15
+ @artist_list = options[:artists]
16
+ @query = {:artists => @artist_list}
17
+ else
18
+ raise ArgumentError
19
+ end
20
+ end
21
+
22
+ def get(*args)
23
+ args.each do |action|
24
+ case action
25
+ when :articles
26
+ get_articles
27
+ when :artists
28
+ get_artists
29
+ else
30
+ raise ArgumentError
31
+ end
32
+ end
33
+ end
34
+
35
+ def respond_to?(method)
36
+ case method
37
+ when :articles,:artists
38
+ true
39
+ else
40
+ super
41
+ end
42
+ end
43
+
44
+ def method_missing(method, *args)
45
+ case method
46
+ when :articles
47
+ get_articles if @articles.nil?
48
+ @articles
49
+ when :artists
50
+ get_artists if @artists.nil?
51
+ @artists
52
+ else
53
+ super
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def get_articles
60
+ @articles = Parser.parse_articles(REST.fetch('recommendation/articles', @query))
61
+ end
62
+
63
+ def get_artists
64
+ artist_data = REST.fetch('recommendation/artists', @query)
65
+
66
+ @artists = {}
67
+ artist_data['artists'].each {|a| @artists[a['title']] = a['value'] }
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,20 @@
1
+ module Idiomag
2
+ class InvalidResult < StandardError; end;
3
+
4
+ class REST
5
+ def self.fetch(resource, query={}, parse=true)
6
+ raise ArgumentError, 'api key missing' if Base.api_key.blank?
7
+
8
+ options = {:query => {:key => Base.api_key}}
9
+ options[:query].merge!(query)
10
+ options[:format] = :json if parse
11
+ begin
12
+ data = HTTParty.get(API_URL + resource + "/json", options)
13
+ rescue Net::HTTPServerException => e
14
+ raise ArgumentError, 'invalid user, artist or tag' if e.message =~ /400/
15
+ end
16
+ raise InvalidResult if data.blank?
17
+ data
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,106 @@
1
+ module Idiomag
2
+ class Tag
3
+ def initialize(tag)
4
+ raise ArgumentError if tag.blank?
5
+ @tag = tag.to_s.gsub(/_/, ' ')
6
+ end
7
+
8
+ def get(*args)
9
+ args.each do |action|
10
+ case action
11
+ when :articles
12
+ get_articles
13
+ when :photos
14
+ get_photos
15
+ when :videos
16
+ get_videos
17
+ when :playlist, :tracks
18
+ get_playlist
19
+ when :artists
20
+ get_artists
21
+ else
22
+ raise ArgumentError
23
+ end
24
+ end
25
+ end
26
+
27
+ def respond_to?(method)
28
+ case method
29
+ when :articles,:photos,:videos,:playlist,:tracks,:artists,:list,:name
30
+ true
31
+ else
32
+ super
33
+ end
34
+ end
35
+
36
+ def method_missing(method, *args)
37
+ case method
38
+ when :name
39
+ @tag
40
+ when :articles
41
+ get_articles if @articles.nil?
42
+ @articles
43
+ when :photos
44
+ get_photos if @photos.nil?
45
+ @photos
46
+ when :videos
47
+ get_videos if @videos.nil?
48
+ @videos
49
+ when :playlist, :tracks
50
+ get_playlist if @playlist.nil?
51
+ @playlist
52
+ when :artists
53
+ get_artists if @artists.nil?
54
+ @artists
55
+ when :list
56
+ Tag.list
57
+ else
58
+ super
59
+ end
60
+ end
61
+
62
+ class << self
63
+ def list
64
+ if @list.blank?
65
+ @list_data = REST.fetch('tags', {}, false)
66
+
67
+ @list = @list_data.split("\n")
68
+ @list.map! do |t|
69
+ if t =~ /-/
70
+ t.strip.downcase
71
+ else
72
+ t.strip.downcase.gsub(/ /, '_').to_sym
73
+ end
74
+ end
75
+ else
76
+ @list
77
+ end
78
+ end
79
+ end
80
+
81
+ private
82
+
83
+ def get_articles
84
+ @articles = Parser.parse_articles(REST.fetch('tag/articles', :tag => @tag))
85
+ end
86
+
87
+ def get_photos
88
+ @photos = Parser.parse_photos(REST.fetch('tag/photos', :tag => @tag))
89
+ end
90
+
91
+ def get_videos
92
+ @videos = Parser.parse_videos(REST.fetch('tag/videos', :tag => @tag))
93
+ end
94
+
95
+ def get_playlist
96
+ @playlist = Parser.parse_playlist(REST.fetch('tag/playlist', :tag => @tag))
97
+ end
98
+
99
+ def get_artists
100
+ artist_data = REST.fetch('tag/artists', :tag => @tag)
101
+
102
+ @artists = {}
103
+ artist_data['profile']['artist'].each {|t| @artists[t['title']] = t['value']}
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,114 @@
1
+ module Idiomag
2
+ class User
3
+ attr_accessor :user
4
+
5
+ def initialize(user)
6
+ raise ArgumentError if user.blank?
7
+ @user = user
8
+ end
9
+
10
+ def get(*args)
11
+ args.each do |action|
12
+ case action
13
+ when :info
14
+ get_info
15
+ when :articles
16
+ get_articles
17
+ when :loved_articles
18
+ get_loved_articles
19
+ when :photos
20
+ get_photos
21
+ when :videos
22
+ get_videos
23
+ when :playlist, :tracks
24
+ get_playlist
25
+ else
26
+ raise ArgumentError
27
+ end
28
+ end
29
+ end
30
+
31
+ def respond_to?(method)
32
+ case method
33
+ when :name,:email,:url,:tags,:artists,:articles,:loved_articles,:photos,:videos,:playlist,:tracks
34
+ true
35
+ else
36
+ super
37
+ end
38
+ end
39
+
40
+ def method_missing(method, *args)
41
+ case method
42
+ when :name
43
+ get_info if @name.nil?
44
+ @name
45
+ when :email
46
+ get_info if @email.nil?
47
+ @email
48
+ when :url
49
+ get_info if @url.nil?
50
+ @url
51
+ when :artists
52
+ get_info if @artists.nil?
53
+ @artists
54
+ when :tags
55
+ get_info if @tags.nil?
56
+ @tags
57
+ when :articles
58
+ get_articles if @articles.nil?
59
+ @articles
60
+ when :loved_articles
61
+ get_loved_articles if @loved_articles.nil?
62
+ @loved_articles
63
+ when :photos
64
+ get_photos if @photos.nil?
65
+ @photos
66
+ when :videos
67
+ get_videos if @videos.nil?
68
+ @videos
69
+ when :playlist, :tracks
70
+ get_playlist if @playlist.nil?
71
+ @playlist
72
+ else
73
+ super
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ def get_info
80
+ info_data = REST.fetch('user/info', {:user => @user})
81
+
82
+ @name = info_data['name']
83
+ @email = info_data['email_sha1']
84
+ @url = info_data['url']
85
+
86
+ @artists = {}
87
+ info_data['artists']['artist'].each {|a| @artists[a['title']] = a['value'] } if !info_data['artists'].blank?
88
+
89
+ @tags = {}
90
+ info_data['tags']['tag'].each {|t| @tags[t['name']] = t['value']} if !info_data['tags'].blank?
91
+ @tags.keys_to_sym!
92
+ end
93
+
94
+ def get_articles
95
+ @articles = Parser.parse_articles(REST.fetch('user/articles', :user => @user))
96
+ end
97
+
98
+ def get_loved_articles
99
+ @loved_articles = Parser.parse_articles(REST.fetch('user/lovedarticles', :user => @user))
100
+ end
101
+
102
+ def get_photos
103
+ @photos = Parser.parse_photos(REST.fetch('user/photos', :user => @user))
104
+ end
105
+
106
+ def get_videos
107
+ @videos = Parser.parse_videos(REST.fetch('user/videos', :user => @user))
108
+ end
109
+
110
+ def get_playlist
111
+ @playlist = Parser.parse_playlist(REST.fetch('user/playlist', :user => @user))
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,61 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe 'Idiomag::Articles' do
4
+ before(:each) do
5
+ @articles = Idiomag::Articles.new
6
+ end
7
+
8
+ it 'should respond_to the correct methods' do
9
+ @articles.respond_to?(:get).should == true
10
+ @articles.respond_to?(:latest).should == true
11
+ @articles.respond_to?(:featured).should == true
12
+ end
13
+
14
+ it 'should error on invalid get argument' do
15
+ lambda { @articles.get(:foo) }.should raise_error(ArgumentError)
16
+ end
17
+
18
+ it 'should error on invalid method' do
19
+ lambda { @articles.foo }.should raise_error(NoMethodError)
20
+ end
21
+
22
+ describe 'latest' do
23
+ before(:each) do
24
+ @data = open(Fixtures + 'articles_latest.json').read
25
+ Idiomag::REST.should_receive(:fetch).with('articles/latest').and_return(JSON.parse(@data))
26
+ end
27
+
28
+ it 'should allow prefetching' do
29
+ @articles.get(:latest)
30
+ end
31
+
32
+ it 'should get latest articles' do
33
+ @articles.latest[0][:artist].should == 'K-Drama'
34
+ @articles.latest[0][:description].blank?.should_not == true
35
+ end
36
+
37
+ it 'should parse the dates' do
38
+ @articles.latest[0][:date].class.should == Time
39
+ end
40
+ end
41
+
42
+ describe 'featured' do
43
+ before(:each) do
44
+ @data = open(Fixtures + 'articles_featured.json').read
45
+ Idiomag::REST.should_receive(:fetch).with('articles/featured').and_return(JSON.parse(@data))
46
+ end
47
+
48
+ it 'should allow prefetching' do
49
+ @articles.get(:featured)
50
+ end
51
+
52
+ it 'should get featured articles' do
53
+ @articles.featured[0][:artist].should == 'Coldplay'
54
+ @articles.featured[0][:description].blank?.should_not == true
55
+ end
56
+
57
+ it 'should parse the dates' do
58
+ @articles.featured[0][:date].class.should == Time
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,149 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe 'Idiomag::Artist' do
4
+ before(:each) do
5
+ @artist = Idiomag::Artist.new('Anberlin')
6
+ end
7
+
8
+ it 'should require an artist' do
9
+ lambda { Idiomag::Artist.new }.should raise_error(ArgumentError)
10
+ end
11
+
12
+ it 'should know the artist name' do
13
+ @artist.name.should == 'Anberlin'
14
+ end
15
+
16
+ it 'should respond_to the correct methods' do
17
+ @artist.respond_to?(:get).should == true
18
+ @artist.respond_to?(:name).should == true
19
+ @artist.respond_to?(:links).should == true
20
+ @artist.respond_to?(:related).should == true
21
+ @artist.respond_to?(:tags).should == true
22
+ @artist.respond_to?(:articles).should == true
23
+ @artist.respond_to?(:photos).should == true
24
+ @artist.respond_to?(:videos).should == true
25
+ @artist.respond_to?(:playlist).should == true
26
+ @artist.respond_to?(:tracks).should == true
27
+ end
28
+
29
+ it 'should error on invalid get argument' do
30
+ lambda { @artist.get(:foo) }.should raise_error(ArgumentError)
31
+ end
32
+
33
+ it 'should error on invalid method' do
34
+ lambda { @artist.foo }.should raise_error(NoMethodError)
35
+ end
36
+
37
+ describe 'info' do
38
+ before(:each) do
39
+ @data = open(Fixtures + 'artist_info.json').read
40
+ Idiomag::REST.should_receive(:fetch).with('artist/info',:artist=>'Anberlin').and_return(JSON.parse(@data))
41
+ end
42
+
43
+ it 'should allow prefetching' do
44
+ @artist.get(:info)
45
+ end
46
+
47
+ it 'should get links' do
48
+ @artist.links.should include('http://www.anberlin.com')
49
+ end
50
+
51
+ it 'should get related artists' do
52
+ @artist.related['Relient K'].should include('http://www.idiomag.com/artist/relient_k/')
53
+ end
54
+ end
55
+
56
+ describe 'tags' do
57
+ before(:each) do
58
+ @data = open(Fixtures + 'artist_tags.json').read
59
+ Idiomag::REST.should_receive(:fetch).with('artist/tags',:artist=>'Anberlin').and_return(JSON.parse(@data))
60
+ end
61
+
62
+ it 'should allow prefetching' do
63
+ @artist.get(:tags)
64
+ end
65
+
66
+ it 'should get tags' do
67
+ @artist.tags[:alternative_rock].should == 0.37
68
+ end
69
+ end
70
+
71
+ describe 'articles' do
72
+ before(:each) do
73
+ @data = open(Fixtures + 'artist_articles.json').read
74
+ Idiomag::REST.should_receive(:fetch).with('artist/articles',:artist=>'Anberlin').and_return(JSON.parse(@data))
75
+ end
76
+
77
+ it 'should allow prefetching' do
78
+ @artist.get(:articles)
79
+ end
80
+
81
+ it 'should get articles' do
82
+ @artist.articles[0][:artist].should == 'Anberlin'
83
+ @artist.articles[0][:description].blank?.should_not == true
84
+ end
85
+
86
+ it 'should parse the dates' do
87
+ @artist.articles[0][:date].class.should == Time
88
+ end
89
+ end
90
+
91
+ describe 'photos' do
92
+ before(:each) do
93
+ @data = open(Fixtures + 'artist_photos.json').read
94
+ Idiomag::REST.should_receive(:fetch).with('artist/photos',:artist=>'Anberlin').and_return(JSON.parse(@data))
95
+ end
96
+
97
+ it 'should allow prefetching' do
98
+ @artist.get(:photos)
99
+ end
100
+
101
+ it 'should get photos' do
102
+ @artist.photos[0][:title].should =~ /jpg/
103
+ @artist.photos[0][:url].should =~ /jpg/
104
+ @artist.photos[0][:width].class.should == Fixnum
105
+ @artist.photos[0][:height].class.should == Fixnum
106
+ end
107
+
108
+ it 'should parse the dates' do
109
+ @artist.photos[0][:date].class.should == Time
110
+ end
111
+ end
112
+
113
+ describe 'videos' do
114
+ before(:each) do
115
+ @data = open(Fixtures + 'artist_videos.json').read
116
+ Idiomag::REST.should_receive(:fetch).with('artist/videos',:artist=>'Anberlin').and_return(JSON.parse(@data))
117
+ end
118
+
119
+ it 'should allow prefetching' do
120
+ @artist.get(:videos)
121
+ end
122
+
123
+ it 'should get videos' do
124
+ @artist.videos[0][:location].should =~ /video/
125
+ @artist.videos[0][:info].should =~ /youtube/
126
+ @artist.videos[0][:thumb].should =~ /jpg/
127
+ end
128
+ end
129
+
130
+ describe 'playlist' do
131
+ before(:each) do
132
+ @data = open(Fixtures + 'artist_playlist.json').read
133
+ Idiomag::REST.should_receive(:fetch).with('artist/playlist',:artist=>'Anberlin').and_return(JSON.parse(@data))
134
+ end
135
+
136
+ it 'should allow prefetching' do
137
+ @artist.get(:playlist)
138
+ end
139
+
140
+ it 'should get playlist' do
141
+ @artist.playlist[0][:location] =~ /mp3/
142
+ end
143
+
144
+ it 'should respond to #tracks' do
145
+ @artist.get(:tracks)
146
+ @artist.tracks[0][:location] =~ /mp3/
147
+ end
148
+ end
149
+ end