mixcloud 0.0.3 → 1.0.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.
- data/lib/mixcloud.rb +1 -1
- data/lib/mixcloud/category.rb +1 -1
- data/lib/mixcloud/cloudcast.rb +10 -5
- data/lib/mixcloud/error.rb +7 -0
- data/lib/mixcloud/popular_new_hot.rb +7 -3
- data/lib/mixcloud/resource.rb +26 -7
- data/lib/mixcloud/search.rb +2 -1
- data/lib/mixcloud/url_helper.rb +26 -29
- metadata +45 -14
data/lib/mixcloud.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rest_client'
|
3
3
|
require 'json'
|
4
|
-
require 'rspec'
|
5
4
|
|
6
5
|
require 'mixcloud/popular_new_hot'
|
7
6
|
require 'mixcloud/url_helper'
|
@@ -14,3 +13,4 @@ require 'mixcloud/tag'
|
|
14
13
|
require 'mixcloud/track'
|
15
14
|
require 'mixcloud/user'
|
16
15
|
require 'mixcloud/search'
|
16
|
+
require 'mixcloud/error'
|
data/lib/mixcloud/category.rb
CHANGED
@@ -20,7 +20,7 @@ module Mixcloud
|
|
20
20
|
|
21
21
|
['userpick_users', 'userpick_cloudcasts', 'users', 'cloudcasts' ].each do | connection |
|
22
22
|
define_method "#{connection}_url" do
|
23
|
-
|
23
|
+
turn_www_to_api(@public_url).concat "#{connection.gsub("_", "-")}/"
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
data/lib/mixcloud/cloudcast.rb
CHANGED
@@ -30,7 +30,7 @@ module Mixcloud
|
|
30
30
|
|
31
31
|
['listeners', 'similar', 'favorites', 'comments'].each do | connection |
|
32
32
|
define_method "#{connection}_url" do
|
33
|
-
|
33
|
+
turn_www_to_api(@public_url) + "#{connection.gsub("_", "-")}/?metadata=1"
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
@@ -38,21 +38,26 @@ module Mixcloud
|
|
38
38
|
convert_hash_data_into_array_of('sections')
|
39
39
|
end
|
40
40
|
|
41
|
-
def
|
41
|
+
def tags
|
42
42
|
convert_hash_data_into_array_of('tags')
|
43
43
|
end
|
44
|
+
|
45
|
+
def tag_urls
|
46
|
+
tags.map(&:api_url)
|
47
|
+
end
|
44
48
|
|
45
49
|
#######################################################################
|
46
50
|
private
|
47
51
|
def convert_hash_data_into_array_of(type)
|
48
|
-
raise
|
52
|
+
raise Mixcloud::Error.new("type should be 'sections' or 'tags', but you provided '#{type}'") unless ['sections', 'tags'].include?(type)
|
49
53
|
elements_hash = JSON.parse(RestClient.get @api_url)[type]
|
50
54
|
objects_array = []
|
51
55
|
elements_hash.each do | element |
|
52
56
|
if type == 'sections'
|
53
|
-
objects_array << Mixcloud::Section.new(
|
57
|
+
objects_array << Mixcloud::Section.new(turn_www_to_api(element['track']['url'] + "?metadata=1"),
|
58
|
+
element['position'], element['start_time'], element['section_type'])
|
54
59
|
elsif type == 'tags'
|
55
|
-
objects_array <<
|
60
|
+
objects_array << Mixcloud::Tag.new(turn_www_to_api(element['url']).concat('?metadata=1'))
|
56
61
|
end
|
57
62
|
end
|
58
63
|
objects_array
|
@@ -1,15 +1,19 @@
|
|
1
1
|
module Mixcloud
|
2
2
|
module PopularNewHot
|
3
|
+
def self.included(base)
|
4
|
+
base.class_eval{include UrlHelper} unless base.include?(UrlHelper)
|
5
|
+
end
|
6
|
+
|
3
7
|
def popular_url
|
4
|
-
|
8
|
+
turn_www_to_api(@public_url).concat 'popular/'
|
5
9
|
end
|
6
10
|
|
7
11
|
def new_url
|
8
|
-
|
12
|
+
turn_www_to_api(@public_url).concat 'new/'
|
9
13
|
end
|
10
14
|
|
11
15
|
def hot_url
|
12
|
-
|
16
|
+
turn_www_to_api(@public_url).concat 'hot/'
|
13
17
|
end
|
14
18
|
end
|
15
19
|
end
|
data/lib/mixcloud/resource.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
module Mixcloud
|
2
2
|
class Resource
|
3
3
|
|
4
|
+
include UrlHelper
|
4
5
|
def initialize(url)
|
5
|
-
|
6
|
-
url_with_metadata =
|
7
|
-
data_hash =
|
8
|
-
klass =
|
6
|
+
validate_mixcloud_url(url)
|
7
|
+
url_with_metadata = concat_with_metadata(url)
|
8
|
+
data_hash = grab_data_from_mixcloud(url_with_metadata)
|
9
|
+
klass = determine_class_by_mixcloud_data(data_hash)
|
9
10
|
prevent_url_and_class_mismatch(klass)
|
10
11
|
map_to_resource_attributes(data_hash)
|
11
12
|
end
|
@@ -14,9 +15,26 @@ module Mixcloud
|
|
14
15
|
###########################################
|
15
16
|
private
|
16
17
|
|
18
|
+
def grab_data_from_mixcloud(url_with_metadata)
|
19
|
+
begin
|
20
|
+
JSON.parse RestClient.get(url_with_metadata)
|
21
|
+
rescue => e
|
22
|
+
e.message.concat(". Are you sure you have connection with Mixcloud at the moment?")
|
23
|
+
raise e
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def determine_class_by_mixcloud_data(data_hash)
|
28
|
+
begin
|
29
|
+
Mixcloud.const_get(data_hash['type'].capitalize) # it would take a change of Mixcloud API to get this line to fail
|
30
|
+
rescue => e
|
31
|
+
e.message.concat(". We can't find the key 'type'. Could it be that Mixcloud has changed its API?")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
17
35
|
def prevent_url_and_class_mismatch(klass)
|
18
36
|
if klass != self.class
|
19
|
-
raise "You tried to create an object of #{self.class} with an URL of object type #{klass}"
|
37
|
+
raise Mixcloud::Error.new("You tried to create an object of #{self.class} with an URL of object type #{klass}")
|
20
38
|
end
|
21
39
|
end
|
22
40
|
|
@@ -38,14 +56,15 @@ module Mixcloud
|
|
38
56
|
|
39
57
|
def set_public_and_api_urls(url_string)
|
40
58
|
send("public_url=", url_string)
|
41
|
-
send("api_url=",
|
59
|
+
send("api_url=", turn_www_to_api(url_string)).concat('?metadata=1')
|
42
60
|
end
|
43
61
|
|
44
62
|
def set_associated_object_urls(key, value)
|
45
63
|
variable_name = key + "_url"
|
46
|
-
object_url =
|
64
|
+
object_url = turn_www_to_api(value['url']).concat('?metadata=1')
|
47
65
|
[ variable_name, object_url ]
|
48
66
|
end
|
67
|
+
|
49
68
|
############################################
|
50
69
|
end
|
51
70
|
end
|
data/lib/mixcloud/search.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module Mixcloud
|
2
2
|
class Search
|
3
|
+
extend UrlHelper
|
3
4
|
|
4
5
|
# The codeblock below defines the following class methods:
|
5
6
|
# Mixcloud::Search.find_artist
|
@@ -25,7 +26,7 @@ module Mixcloud
|
|
25
26
|
def self.turn_data_into_mixcloud_resources(results, klass)
|
26
27
|
search_results = []
|
27
28
|
results.each do |result|
|
28
|
-
resource_api_url =
|
29
|
+
resource_api_url = turn_www_to_api result['url']
|
29
30
|
resource = klass.new(resource_api_url)
|
30
31
|
search_results << resource
|
31
32
|
end
|
data/lib/mixcloud/url_helper.rb
CHANGED
@@ -1,38 +1,35 @@
|
|
1
1
|
module Mixcloud
|
2
|
-
class UrlHelper
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
def concat_with_metadata(url)
|
12
|
-
return url if ends_with_metadata?(url)
|
13
|
-
return url.chop if ends_with_metadata_slash?(url)
|
14
|
-
url.concat('/') unless ends_with_slash?(url)
|
15
|
-
url.concat('?metadata=1')
|
16
|
-
end
|
3
|
+
module UrlHelper
|
4
|
+
##################################
|
5
|
+
public
|
6
|
+
def validate_mixcloud_url(url)
|
7
|
+
raise Mixcloud::Error.new("You provided an invalid Mixcloud-API url. It must start with http://api.mixcloud.com/") unless url =~ /\Ahttp:\/\/api.mixcloud.com\//
|
8
|
+
end
|
17
9
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
true if url =~ /\?metadata=1$/
|
25
|
-
end
|
10
|
+
def concat_with_metadata(url)
|
11
|
+
return url if ends_with_metadata?(url)
|
12
|
+
return url.chop if ends_with_metadata_slash?(url)
|
13
|
+
url.concat('/') unless ends_with_slash?(url)
|
14
|
+
url.concat('?metadata=1')
|
15
|
+
end
|
26
16
|
|
27
|
-
|
28
|
-
|
29
|
-
|
17
|
+
def turn_www_to_api(url)
|
18
|
+
url.strip.sub('http://www.', 'http://api.' ) if url.strip =~ /\Ahttp:\/\/www./
|
19
|
+
end
|
20
|
+
##################################
|
21
|
+
private
|
22
|
+
def ends_with_metadata?(url)
|
23
|
+
true if url =~ /\?metadata=1$/
|
24
|
+
end
|
30
25
|
|
31
|
-
|
32
|
-
|
33
|
-
end
|
34
|
-
##################################
|
26
|
+
def ends_with_metadata_slash?(url)
|
27
|
+
true if url =~ /\?metadata=1\/$/
|
35
28
|
end
|
36
29
|
|
30
|
+
def ends_with_slash?(url)
|
31
|
+
url =~ /\/\z/
|
32
|
+
end
|
33
|
+
##################################
|
37
34
|
end
|
38
35
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mixcloud
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,41 +9,72 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rest-client
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: 1.6.7
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.6.7
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: json
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
|
-
- -
|
35
|
+
- - ! '>='
|
31
36
|
- !ruby/object:Gem::Version
|
32
37
|
version: 1.6.1
|
33
38
|
type: :runtime
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.6.1
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: rspec
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
|
-
- -
|
51
|
+
- - ! '>='
|
42
52
|
- !ruby/object:Gem::Version
|
43
53
|
version: 2.7.0
|
44
|
-
type: :
|
54
|
+
type: :development
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.7.0
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: fakeweb
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.3.0
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.3.0
|
47
78
|
description: Ruby wrapper of the Mixcloud API. Visit http://www.mixcloud.com/developers/documentation/
|
48
79
|
for more info
|
49
80
|
email: actfong@gmail.com
|
@@ -55,6 +86,7 @@ files:
|
|
55
86
|
- lib/mixcloud/artist.rb
|
56
87
|
- lib/mixcloud/category.rb
|
57
88
|
- lib/mixcloud/cloudcast.rb
|
89
|
+
- lib/mixcloud/error.rb
|
58
90
|
- lib/mixcloud/popular_new_hot.rb
|
59
91
|
- lib/mixcloud/resource.rb
|
60
92
|
- lib/mixcloud/search.rb
|
@@ -83,10 +115,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
115
|
version: '0'
|
84
116
|
requirements: []
|
85
117
|
rubyforge_project:
|
86
|
-
rubygems_version: 1.8.
|
118
|
+
rubygems_version: 1.8.21
|
87
119
|
signing_key:
|
88
120
|
specification_version: 3
|
89
121
|
summary: Ruby wrapper of the Mixcloud API. It allows you to builld Mixcloud resources
|
90
122
|
as Ruby objects and provides search functionality
|
91
123
|
test_files: []
|
92
|
-
has_rdoc:
|