officialfm 0.1.2 → 0.2.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.
@@ -0,0 +1,65 @@
1
+ module OfficialFM
2
+ module Projects
3
+
4
+ # Search for a project
5
+ def projects(search_param, options={})
6
+ response = connection.get '/projects/search', options.merge(:q => search_param)
7
+
8
+ response.body.projects.map! do |p|
9
+ # remove the unnecessary root
10
+ actual_project = p.project
11
+
12
+ extend_response(actual_project, ProjectMethods)
13
+
14
+ actual_project
15
+ end
16
+
17
+ response.body
18
+ end
19
+
20
+ # Retrieve information about a specific project
21
+ def project(id, options={})
22
+ url = resource_url(id, parent: 'projects')
23
+
24
+ response = connection.get(url, options).body.project
25
+ extend_response(response, ProjectMethods)
26
+ end
27
+
28
+ def project_playlists(id, options={})
29
+ url = resource_url(id, { parent: 'projects', child: 'playlists' })
30
+
31
+ response = connection.get url, options
32
+
33
+ playlists = response.body.playlists.map do |p|
34
+ # remove the unnecessary root
35
+ p.playlist
36
+ end
37
+
38
+ playlists
39
+ end
40
+
41
+ def project_tracks(id, options={})
42
+ url = resource_url(id, { parent: 'projects', child: 'tracks' })
43
+
44
+ response = connection.get url, options
45
+
46
+ tracks = response.body.tracks.map do |t|
47
+ # remove the unnecessary root
48
+ t.track
49
+ end
50
+
51
+ tracks
52
+ end
53
+
54
+ module ProjectMethods
55
+ def playlists
56
+ obj.project_playlists(self.src)
57
+ end
58
+
59
+ def tracks
60
+ obj.project_tracks(self.src)
61
+ end
62
+ end
63
+
64
+ end
65
+ end
@@ -1,206 +1,24 @@
1
- require 'cgi'
2
-
3
1
  module OfficialFM
4
2
  module Tracks
5
-
3
+
6
4
  # Search for tracks
7
- #
8
- # @param [String] search_param: a search parameter (eg. name of the track)
9
- # @param [Integer] limit (50) limit per page (optional)
10
- # @return [Hashie::Mash] Track list
11
5
  def tracks(search_param, options={})
12
- response = connection.get do |req|
13
- req.url "/search/tracks/#{CGI::escape(search_param)}", simple_params(options)
14
- end
15
- response.body
16
- end
17
-
18
- # Retrieve information about a specific track
19
- #
20
- # Note: http://official.fm/developers/simple_api#track_show
21
- # says that api_max_responses is a valid parameter. Why escapes me.
22
- #
23
- # @param [String] track_id: id
24
- # @param [Bool] embed (false) should embed codes be included in the response
25
- # @return [Hashie::Mash] Track
26
- def track(track_id, options={})
27
- response = connection.get do |req|
28
- req.url "/track/#{track_id}", simple_params(options)
29
- end
30
- response.body[0]
31
- end
32
-
33
- # Retrieve users that have voted for this track
34
- #
35
- # @param [String] track_id: id
36
- # @param [Integer] limit (50) limit per page
37
- # @return [Hashie::Mash] User list
38
- def track_votes(track_id, options={})
39
- response = connection.get do |req|
40
- req.url "/track/#{track_id}/votes", simple_params(options)
41
- end
42
- response.body
43
- end
44
-
45
- # Retrieve 200 tracks of selected chart
46
- #
47
- # @param [String] charting: :today, :week, :month, :year or :all_time
48
- # @param [String] genre: Genre string (Electronic, Rock, Jazz, ...) (optional)
49
- # @param [String] country: ISO country id (CH, FR, UK) (optional)
50
- # @param [Bool] embed (false) should embed codes be included in the response (optional)
51
- # @param [Integer] limit (200) limit per page (optional)
52
- # @return [Hashie::Mash] Track list
53
- def charts(charting, options={})
54
- response = connection.get do |req|
55
- req.url "/tracks/charts", simple_params(options)
56
- end
57
- response.body
58
- end
59
-
60
- # Retrieve 200 latest tracks
61
- #
62
- # @param [String] genre: Genre string (Electronic, Rock, Jazz, ...) (optional)
63
- # @param [String] country: ISO country id (CH, FR, UK) (optional)
64
- # @param [Bool] embed (false) should embed codes be included in the response (optional)
65
- # @param [Integer] limit (200) limit per page (optional)
66
- # @return [Hashie::Mash] Track list
67
- def latest(options={})
68
- response = connection.get do |req|
69
- req.url "/tracks/latest", simple_params(options)
70
- end
71
- response.body
72
- end
73
-
74
- ####################################################################
75
- ######################### Advanced API methods #####################
76
- ####################################################################
6
+ response = connection.get '/tracks/search', options.merge(:q => search_param)
77
7
 
78
- # Update information of a given track
79
- #
80
- # @param [String] track_id: id
81
- # @param [String] artist_name (optional)
82
- # @param [String] buy_url (optional)
83
- # @param [String] country_id (optional)
84
- # @param [String] downloadable (optional)
85
- # @param [String] derived_by (optional)
86
- # @param [String] derived_type (optional)
87
- # @param [String] description (optional)
88
- # @param [String] isrc (optional)
89
- # @param [String] label_name (optional)
90
- # @param [String] label_none (optional)
91
- # @param [String] license_type (optional)
92
- # @param [String] lyrics (optional)
93
- # @param [String] genre_string (optional)
94
- # @param [String] original_track_id (optional)
95
- # @param [String] password (optional)
96
- # @param [String] pr_url (optional)
97
- # @param [String] private (optional)
98
- # @param [String] require_valid_email (optional)
99
- # @param [String] tag_string (optional)
100
- # @param [String] title (optional)
101
- # @param [String] url_1_name (optional)
102
- # @param [String] url_1 (optional)
103
- # @param [String] url_2_name (optional)
104
- # @param [String] url_2 (optional)
105
- # @param [String] web_url (optional)
106
- # @return [Hashie::Mash] Track
107
- def update_track! (track_id, data = {})
108
- check_auth :update_track
109
-
110
- response = connection.put do |req|
111
- req.url "/track/update/#{track_id}", data
112
- req.body = { :format => @format }
113
- end
114
- response
115
- end
116
-
117
- # Upload a picture for a given track
118
- #
119
- # @param [String] track_id: id
120
- # @param [String] path: path to a picture
121
- # @param [String] mime: the mime-type of the picture (e.g. image/jpeg, image/png, etc.)
122
- # @return [Hashie::Mash] Success or error message
123
- def track_picture! (track_id, path, mime)
124
- check_auth :track_picture
125
-
126
- response = connection.post do |req|
127
- req.url "/track/picture/#{track_id}"
128
- req.body = { :file => Faraday::UploadIO.new(path, mime), :format => @format }
129
- end
130
- response
131
- end
132
-
133
- # Vote for a track
134
- #
135
- # @param [String] track_id: id
136
- # @return [Hashie::Mash] Success or error message
137
- def track_vote! (track_id)
138
- check_auth :track_vote
139
-
140
- response = connection.post do |req|
141
- req.url "/track/vote/#{track_id}"
142
- req.body = { :format => @format }
8
+ response.body.tracks.map! do |t|
9
+ # remove useless root
10
+ t.track
143
11
  end
144
- response
145
- end
146
12
 
147
- # Remove vote for a track
148
- #
149
- # @param [String] track_id: id
150
- # @return [Hashie::Mash] Success or error message
151
- def track_unvote! (track_id)
152
- check_auth :track_unvote
153
-
154
- response = connection.post do |req|
155
- req.url "/track/unvote/#{track_id}"
156
- req.body = { :format => @format }
157
- end
158
- response
159
- end
160
-
161
- # Add a track to a playlist
162
- #
163
- # @param [String] track_id: id
164
- # @param [String] playlist_id: id
165
- # @return [Hashie::Mash] Success or error message
166
- def add_track_to_playlist! (track_id, playlist_id)
167
- check_auth :add_track_to_playlist!
168
-
169
- response = connection.post do |req|
170
- req.url "/track/#{track_id}/addto/#{playlist_id}"
171
- req.body = { :format => @format }
172
- end
173
- response
13
+ response.body
174
14
  end
175
15
 
176
- # Remove a track from a playlist
177
- #
178
- # @param [String] track_id: id
179
- # @param [String] playlist_id: id
180
- # @return [Hashie::Mash] Success or error message
181
- def remove_track_from_playlist! (track_id, playlist_id)
182
- check_auth :remove_track_from_playlist!
183
-
184
- response = connection.delete do |req|
185
- req.url "/track/#{track_id}/removefrom/#{playlist_id}"
186
- req.body = { :format => @format }
187
- end
188
- response
189
- end
190
-
191
- # Upload a new track to Official.fm
192
- #
193
- # @param [String] path: path to an mp3, 44.1Khz file
194
- # @param [String] mime: the mime-type of the file (e.g. audio/mpeg, etc.)
195
- # @return [Hashie::Mash] Track
196
- def upload! (path, mime)
197
- check_auth :upload
198
-
199
- response = connection.post do |req|
200
- req.url "/track/upload"
201
- req.body = { :file => Faraday::UploadIO.new(path, mime), :format => @format }
202
- end
203
- response
16
+ # Retrieve information about a specific track
17
+ def track(id, options={})
18
+ url = resource_url(id, parent: 'tracks')
19
+
20
+ response = connection.get url, options
21
+ response.body.track
204
22
  end
205
23
 
206
24
  end
@@ -1,3 +1,3 @@
1
1
  module OfficialFM
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/officialfm.rb CHANGED
@@ -7,24 +7,18 @@ module OfficialFM
7
7
 
8
8
  class << self
9
9
  attr_accessor :api_key
10
- attr_accessor :api_secret
11
- attr_accessor :test_mode
12
10
 
13
11
  # Configures default credentials easily
14
- # @yield [api_key, username, password]
12
+ # @yield api_key
15
13
  def configure
16
14
  yield self
17
15
  true
18
16
  end
19
-
20
- def test_mode?
21
- !!self.test_mode
22
- end
23
17
  end
24
18
 
25
- require 'officialfm/users'
26
19
  require 'officialfm/tracks'
27
20
  require 'officialfm/playlists'
21
+ require 'officialfm/projects'
28
22
  require 'officialfm/client'
29
23
 
30
24
  end
data/officialfm.gemspec CHANGED
@@ -4,34 +4,28 @@ require File.expand_path('../lib/officialfm/version', __FILE__)
4
4
  Gem::Specification.new do |gem|
5
5
  gem.name = 'officialfm'
6
6
  gem.version = OfficialFM::VERSION
7
- gem.authors = ["Amos Wenger"]
8
- gem.email = ['amos@official.fm']
9
- gem.summary = %q{Official Ruby bindings for the official.fm API}
10
- gem.description = %q{Official Ruby bindings for the official.fm API}
7
+ gem.authors = ["Dimiter Petrov"]
8
+ gem.email = ['dimiter@official.fm']
9
+ gem.summary = %q{Official Ruby bindings for the official.fm v2 API}
10
+ gem.description = %q{Official Ruby bindings for the official.fm v2 API}
11
11
  gem.post_install_message =<<eos
12
12
  ********************************************************************************
13
13
 
14
14
  Follow @ofmdev on Twitter for announcements, updates, and news.
15
15
  https://twitter.com/ofmdev
16
16
 
17
- Don't forget to get your API key here:
18
- http://official.fm/developers/manager
19
-
20
17
  ********************************************************************************
21
18
  eos
22
- gem.homepage = 'http://github.com/officialfm/officialfm-ruby'
23
-
24
- gem.add_runtime_dependency 'faraday', '~> 0.6'
25
- gem.add_runtime_dependency 'faraday_middleware', '~> 0.6'
26
- gem.add_runtime_dependency 'hashie', '~> 1.0'
27
- gem.add_runtime_dependency 'simple_oauth', '~> 0.1.5'
19
+ gem.homepage = 'http://github.com/officialfm/officialfm-v2-ruby'
28
20
 
21
+ gem.add_runtime_dependency 'faraday', '~> 0.8.1'
22
+ gem.add_runtime_dependency 'faraday_middleware', '~> 0.8.8'
23
+ gem.add_runtime_dependency 'hashie', '~> 1.2'
24
+
29
25
  gem.add_development_dependency 'bundler', '~> 1.0'
30
- gem.add_development_dependency 'fakeweb', '~> 1.3'
31
- gem.add_development_dependency 'jnunemaker-matchy', '~> 0.4'
32
- gem.add_development_dependency 'json_pure', '~> 1.5'
33
26
  gem.add_development_dependency 'rake', '~> 0.8'
34
27
  gem.add_development_dependency 'shoulda', '~> 2.11'
28
+ gem.add_development_dependency 'jnunemaker-matchy', '~> 0.4'
35
29
  gem.add_development_dependency 'test-unit', '~> 2.1'
36
30
 
37
31
  gem.required_rubygems_version = Gem::Requirement.new('>= 1.3.6') if gem.respond_to? :required_rubygems_version=
@@ -0,0 +1,97 @@
1
+ require 'helper'
2
+
3
+ class ClientTest < Test::Unit::TestCase
4
+
5
+ context "OfficialFM::Client" do
6
+
7
+ should "use the configuration set with OfficialFM.configure by default" do
8
+ API_KEY = "test key"
9
+
10
+ OfficialFM.configure do |s|
11
+ s.api_key = API_KEY
12
+ end
13
+
14
+ OfficialFM.api_key.should == API_KEY
15
+ OfficialFM::Client.new.api_key.should == API_KEY
16
+ end
17
+
18
+
19
+ should "be able to override the default configuration" do
20
+ DEFAULT_API_KEY = "test key"
21
+ CUSTOM_API_KEY = "other test key"
22
+
23
+ OfficialFM.configure do |s|
24
+ s.api_key = DEFAULT_API_KEY
25
+ end
26
+
27
+ client = OfficialFM::Client.new(:api_key => CUSTOM_API_KEY)
28
+ client.api_key.should == CUSTOM_API_KEY
29
+
30
+ end
31
+
32
+ context "#resource_url" do
33
+ setup do
34
+ @base_url = 'http://example.com'
35
+ @client = OfficialFM::Client.new
36
+ end
37
+
38
+ should "not change the base paths of complete URLs" do
39
+ options = {
40
+ parent: 'foo',
41
+ child: 'bar'
42
+ }
43
+
44
+ @client.send(:resource_url, @base_url, options).start_with?(@base_url).should be(true)
45
+ end
46
+
47
+ should "build resource url from a base url" do
48
+ options = {
49
+ parent: 'foo',
50
+ child: 'bar'
51
+ }
52
+
53
+ @client.send(:resource_url, @base_url, options).should == 'http://example.com/bar'
54
+ end
55
+
56
+ should "build resource url from an id" do
57
+ options = {
58
+ parent: 'foo',
59
+ child: 'bar'
60
+ }
61
+ @client.send(:resource_url, '42', options).should == '/foo/42/bar'
62
+ end
63
+
64
+ should "build url for resource without child" do
65
+ @client.send(:resource_url, '42', parent: 'foo').should == '/foo/42'
66
+ end
67
+
68
+ should "build url for resource without parent" do
69
+ @client.send(:resource_url, 'user', child: 'profile').should == '/user/profile'
70
+ end
71
+
72
+ should "build url for resource without parent or child" do
73
+ @client.send(:resource_url, 'greeting').should == '/greeting'
74
+ end
75
+
76
+ should "raise an error when resource id is nil" do
77
+ lambda {@client.send(:resource_url, nil)}.should raise_error
78
+ end
79
+ end
80
+
81
+ context "#extend_response" do
82
+ setup do
83
+ @client = OfficialFM::Client.new
84
+ end
85
+
86
+ should "add module methods to a client response object" do
87
+ response = Hashie::Mash.new
88
+ module CustomMethods; def foo; end; end
89
+
90
+ @client.send(:extend_response, response, CustomMethods).should respond_to? :foo
91
+ end
92
+
93
+ end
94
+
95
+ end
96
+
97
+ end
data/test/helper.rb CHANGED
@@ -1,42 +1,16 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
- require 'shoulda'
4
- require 'matchy'
5
- require 'fakeweb'
6
- begin require 'redgreen'; rescue LoadError; end
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ require 'simplecov-gem-adapter'
7
4
 
8
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
- $LOAD_PATH.unshift(File.dirname(__FILE__))
10
- require 'officialfm'
11
-
12
- FakeWeb.allow_net_connect = false
13
-
14
- def officialfm_test_client
15
- OfficialFM::Client.new(:api_key => 'GNXbH3zYb25F1I7KVEEN')
5
+ SimpleCov.start 'gem'
16
6
  end
17
7
 
18
- def fixture_file(filename)
19
- return '' if filename == ''
20
- file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename)
21
- File.read(file_path)
22
- end
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
23
10
 
24
- def officialfm_url(url)
25
- url =~ /^http/ ? url : "http://api.official.fm#{url}"
26
- end
11
+ require 'officialfm'
27
12
 
28
- def stub_get(url, filename, options={})
29
- opts = {
30
- :body => fixture_file(filename),
31
- :content_type => 'application/json; charset=utf-8'
32
- }.merge(options)
33
- FakeWeb.register_uri(:get, officialfm_url(url), opts)
34
- end
13
+ require 'test/unit'
14
+ require 'shoulda'
15
+ require 'matchy'
35
16
 
36
- def stub_post(url, filename, options={})
37
- opts = {
38
- :body => fixture_file(filename),
39
- :content_type => 'application/json; charset=utf-8'
40
- }.merge(options)
41
- FakeWeb.register_uri(:post, officialfm_url(url), opts)
42
- end
data/test/tracks_test.rb CHANGED
@@ -1,48 +1,27 @@
1
1
  require 'helper'
2
2
 
3
3
  class TracksTest < Test::Unit::TestCase
4
+ context "OfficialFM::Client" do
4
5
 
5
- context "When using the official.fm API and working with Tracks" do
6
6
  setup do
7
- @client = officialfm_test_client
8
- end
9
-
10
- should "search for 5 tracks with the 'Quelque Chose' search term" do
11
- stub_get('http://api.official.fm/search/tracks/Quelque+Chose?key=GNXbH3zYb25F1I7KVEEN&format=json&api_max_responses=5', 'tracks.json')
12
- tracks = @client.tracks('Quelque Chose', :limit => 5)
13
- tracks.length.should == 5
14
- tracks[0].artist_string.should == 'Radio Sympa'
15
- tracks[4].title.should == 'Manhattan'
16
- # interestingly, the search turns up nothing about QuelqueChoseD'HasBeen :/
17
- end
18
-
19
- should "look up informations about my favorite SomethingALaMode mix" do
20
- stub_get('http://api.official.fm/track/60526?key=GNXbH3zYb25F1I7KVEEN&format=json&api_embed_codes=', 'track.json')
21
- track = @client.track(60526)
22
- # yes, for some reason there's a double-escaped backslash in there. Don't ask, don't tell.
23
- track.title.should == "QuelqueChosed\'HasBeen"
24
- track.artist_string.should == 'Various Artist'
25
- end
26
-
27
- should "retrieve a list of 3 users who voted for QuelqueChoseD'HasBeen" do
28
- stub_get('http://api.official.fm/track/60526/votes?key=GNXbH3zYb25F1I7KVEEN&format=json&api_max_responses=3', 'track_votes.json')
29
- users = @client.track_votes(60526, :limit => 3)
30
- users[0].city.should == "Geneva"
31
- users[1].profile.should == "co-founder @official.fm"
32
- users[2].id.should == 7615
7
+ @client = OfficialFM::Client.new
8
+
9
+ @stubs = Faraday::Adapter::Test::Stubs.new
10
+
11
+ @client.connection.builder.adapter :test, @stubs
33
12
  end
34
-
35
- should "retrieve 50 latest tracks from the 'electronic' charts" do
36
- stub_get('http://api.official.fm/tracks/charts?key=GNXbH3zYb25F1I7KVEEN&format=json&charting=month&genre=&country=&api_embed_codes=&api_max_responses=', 'charts.json')
37
- tracks = @client.charts('month')
38
- tracks.length.should == 50 # the API's default
13
+
14
+ context "#tracks" do
39
15
  end
40
-
41
- should "retrieve 50 last posted tracks" do
42
- stub_get('http://api.official.fm/tracks/latest?key=GNXbH3zYb25F1I7KVEEN&format=json&genre=&country=&api_embed_codes=&api_max_responses=', 'charts.json')
43
- tracks = @client.latest()
44
- tracks.length.should == 50 # the API's default
16
+
17
+ context "#track" do
18
+ should "return a track" do
19
+ id = 42
20
+ @stubs.get("/tracks/#{id}") {[200, {}, 'test']}
21
+ raise "Implement"
22
+ @client.track(id)
23
+
24
+ end
45
25
  end
46
26
  end
47
-
48
27
  end