mixcloud 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'rest_client'
3
+ require 'json'
4
+ require 'rspec'
5
+
6
+ require 'mixcloud/popular_new_hot'
7
+ require 'mixcloud/url_helper'
8
+ require 'mixcloud/resource'
9
+ require 'mixcloud/artist'
10
+ require 'mixcloud/category'
11
+ require 'mixcloud/cloudcast'
12
+ require 'mixcloud/section'
13
+ require 'mixcloud/tag'
14
+ require 'mixcloud/track'
15
+ require 'mixcloud/user'
16
+ require 'mixcloud/search'
@@ -0,0 +1,14 @@
1
+ module Mixcloud
2
+ class Artist < Resource
3
+ include Mixcloud::PopularNewHot
4
+ attr_accessor :name,
5
+ :key,
6
+ :slug,
7
+ :public_url,
8
+ :api_url
9
+ # This class contains the following instance methods
10
+ # #popular_url
11
+ # #new_url
12
+ # #hot_url
13
+ end
14
+ end
@@ -0,0 +1,27 @@
1
+ module Mixcloud
2
+ class Category < Mixcloud::Resource
3
+ attr_accessor :name,
4
+ :format,
5
+ :public_url,
6
+ :api_url,
7
+ :key,
8
+ :slug
9
+
10
+ # This class contains the following instance methods:
11
+ # #small_picture_url
12
+ # #large_picture_url
13
+ # #medium_picture_url
14
+ # #thumbnail_picture_url
15
+ # #medium_mobile_picture_url
16
+ # #userpick_users_url
17
+ # #userpick_cloudcasts_url
18
+ # #users_url
19
+ # #cloudcasts_url
20
+
21
+ ['userpick_users', 'userpick_cloudcasts', 'users', 'cloudcasts' ].each do | connection |
22
+ define_method "#{connection}_url" do
23
+ UrlHelper.turn_www_to_api(@public_url).concat "#{connection.gsub("_", "-")}/"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,62 @@
1
+ module Mixcloud
2
+ class Cloudcast < Mixcloud::Resource
3
+ attr_accessor :listener_count,
4
+ :name,
5
+ :public_url,
6
+ :api_url,
7
+ :description,
8
+ :updated_time,
9
+ :play_count,
10
+ :comment_count,
11
+ :percentage_music,
12
+ :key,
13
+ :created_time,
14
+ :audio_length,
15
+ :slug,
16
+ :favorite_count,
17
+ :user_url
18
+
19
+ # This class contains the following instance methods
20
+ # #medium_picture_url
21
+ # #extra_large_picture_url
22
+ # #large_picture_url
23
+ # #small_url
24
+ # #medium_mobile_picture_url
25
+ # #thumbnail_picture_url
26
+ # #listeners_url
27
+ # #similar_url
28
+ # #favorites_url
29
+ # #comments_url
30
+
31
+ ['listeners', 'similar', 'favorites', 'comments'].each do | connection |
32
+ define_method "#{connection}_url" do
33
+ UrlHelper.turn_www_to_api(@public_url) + "#{connection.gsub("_", "-")}/?metadata=1"
34
+ end
35
+ end
36
+
37
+ def sections
38
+ convert_hash_data_into_array_of('sections')
39
+ end
40
+
41
+ def tag_urls
42
+ convert_hash_data_into_array_of('tags')
43
+ end
44
+
45
+ #######################################################################
46
+ private
47
+ def convert_hash_data_into_array_of(type)
48
+ raise 'I only take sections of tags, dude' unless ['sections', 'tags'].include?(type)
49
+ elements_hash = JSON.parse(RestClient.get @api_url)[type]
50
+ objects_array = []
51
+ elements_hash.each do | element |
52
+ if type == 'sections'
53
+ objects_array << Mixcloud::Section.new(UrlHelper.turn_www_to_api(element['track']['url']), element['position'], element['start_type'], element['section_type'] )
54
+ elsif type == 'tags'
55
+ objects_array << UrlHelper.turn_www_to_api(element['url']).concat('?metadata=1')
56
+ end
57
+ end
58
+ objects_array
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,15 @@
1
+ module Mixcloud
2
+ module PopularNewHot
3
+ def popular_url
4
+ UrlHelper.turn_www_to_api(@public_url).concat 'popular/'
5
+ end
6
+
7
+ def new_url
8
+ UrlHelper.turn_www_to_api(@public_url).concat 'new/'
9
+ end
10
+
11
+ def hot_url
12
+ UrlHelper.turn_www_to_api(@public_url).concat 'hot/'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,51 @@
1
+ module Mixcloud
2
+ class Resource
3
+
4
+ def initialize(url)
5
+ UrlHelper.validate_mixcloud_url(url)
6
+ url_with_metadata = UrlHelper.concat_with_metadata(url)
7
+ data_hash = JSON.parse RestClient.get(url_with_metadata)
8
+ klass = Mixcloud.const_get(data_hash['type'].capitalize)
9
+ prevent_url_and_class_mismatch(klass)
10
+ map_to_resource_attributes(data_hash)
11
+ end
12
+
13
+
14
+ ###########################################
15
+ private
16
+
17
+ def prevent_url_and_class_mismatch(klass)
18
+ if klass != self.class
19
+ raise "You tried to create an object of #{self.class} with an URL of object type #{klass}"
20
+ end
21
+ end
22
+
23
+ def map_to_resource_attributes(data_hash)
24
+ data_hash.each_pair do | key, value |
25
+ next if ['metadata', 'sections', 'tags', 'type'].include?(key)
26
+ create_picture_url_methods(value) and next if key == 'pictures'
27
+ set_public_and_api_urls(value) and next if key == 'url'
28
+ key,value = set_associated_object_urls(key, value) if Mixcloud.const_defined?(key.capitalize)
29
+ send("#{key}=", value)
30
+ end
31
+ end
32
+
33
+ def create_picture_url_methods(picture_hash)
34
+ picture_hash.each_pair do |format, picture_url|
35
+ self.class.send(:define_method, "#{format}_picture_url") { picture_url }
36
+ end
37
+ end
38
+
39
+ def set_public_and_api_urls(url_string)
40
+ send("public_url=", url_string)
41
+ send("api_url=", UrlHelper.turn_www_to_api(url_string)).concat('?metadata=1')
42
+ end
43
+
44
+ def set_associated_object_urls(key, value)
45
+ variable_name = key + "_url"
46
+ object_url = UrlHelper.turn_www_to_api(value['url']).concat('?metadata=1')
47
+ [ variable_name, object_url ]
48
+ end
49
+ ############################################
50
+ end
51
+ end
@@ -0,0 +1,36 @@
1
+ module Mixcloud
2
+ class Search
3
+
4
+ # The codeblock below defines the following class methods:
5
+ # Mixcloud::Search.find_artist
6
+ # Mixcloud::Search.find_cloudcast
7
+ # Mixcloud::Search.find_user
8
+ # Mixcloud::Search.find_tag
9
+ SEARCH_TYPES = ['artist', 'cloudcast', 'user', 'tag']
10
+ SEARCH_TYPES.each do |type|
11
+ define_singleton_method "find_#{type}" do | name |
12
+ search_url = create_search_url(name, type)
13
+ data_returned = JSON.parse(RestClient.get search_url)['data']
14
+ search_results = turn_data_into_mixcloud_resources(data_returned, Mixcloud.const_get(type.capitalize))
15
+ search_results
16
+ end
17
+ end
18
+
19
+ private
20
+ def self.create_search_url(name, type)
21
+ name.gsub! " ", "+"
22
+ "http://api.mixcloud.com/search/?q=#{name}&type=#{type}"
23
+ end
24
+
25
+ def self.turn_data_into_mixcloud_resources(results, klass)
26
+ search_results = []
27
+ results.each do |result|
28
+ resource_api_url = UrlHelper.turn_www_to_api result['url']
29
+ resource = klass.new(resource_api_url)
30
+ search_results << resource
31
+ end
32
+ search_results
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,12 @@
1
+ module Mixcloud
2
+ class Section
3
+ attr_accessor :track_url, :position, :start_time, :section_type
4
+
5
+ def initialize(track_url, position, start_time, section_type)
6
+ @track_url = track_url
7
+ @position = position
8
+ @start_time = start_time
9
+ @section_type = section_type
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module Mixcloud
2
+ class Tag < Mixcloud::Resource
3
+ include Mixcloud::PopularNewHot
4
+ attr_accessor :public_url,
5
+ :api_url,
6
+ :name,
7
+ :key
8
+ # This class contains the following instance methods
9
+ # #popular_url
10
+ # #new_url
11
+ # #hot_url
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module Mixcloud
2
+ class Track < Mixcloud::Resource
3
+ include PopularNewHot
4
+ attr_accessor :public_url,
5
+ :api_url,
6
+ :name,
7
+ :key,
8
+ :slug,
9
+ :artist_url
10
+ # This class contains the following instance methods
11
+ # #popular_url
12
+ # #new_url
13
+ # #hot_url
14
+ end
15
+ end
@@ -0,0 +1,38 @@
1
+ module Mixcloud
2
+ class UrlHelper
3
+
4
+ class << self
5
+ ##################################
6
+ public
7
+ def validate_mixcloud_url(url)
8
+ raise "You provided an invalid Mixcloud-API url. It must start with http://api.mixcloud.com/" unless url =~ /\Ahttp:\/\/api.mixcloud.com\//
9
+ end
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
17
+
18
+ def turn_www_to_api(url)
19
+ url.strip.sub('http://www.', 'http://api.' ) if url.strip =~ /\Ahttp:\/\/www./
20
+ end
21
+ ##################################
22
+ private
23
+ def ends_with_metadata?(url)
24
+ true if url =~ /\?metadata=1$/
25
+ end
26
+
27
+ def ends_with_metadata_slash?(url)
28
+ true if url =~ /\?metadata=1\/$/
29
+ end
30
+
31
+ def ends_with_slash?(url)
32
+ url =~ /\/\z/
33
+ end
34
+ ##################################
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,42 @@
1
+ module Mixcloud
2
+ class User < Mixcloud::Resource
3
+ attr_accessor :username,
4
+ :name,
5
+ :cloudcast_count,
6
+ :following_count,
7
+ :public_url,
8
+ :api_url,
9
+ :listen_count,
10
+ :key,
11
+ :follower_count,
12
+ :favorite_count,
13
+ :updated_time,
14
+ :created_time,
15
+ :pictures,
16
+ :city,
17
+ :biog,
18
+ :country
19
+
20
+ # This class contains the following instance methods
21
+ # #medium_picture_url
22
+ # #extra_large_picture_url
23
+ # #large_picture_url
24
+ # #small_url
25
+ # #medium_mobile_picture_url
26
+ # #thumbnail_picture_url
27
+ # #feed_url
28
+ # #playlists_url
29
+ # #comments_url
30
+ # #followers_url
31
+ # #favorites_url
32
+ # #following_url
33
+ # #cloudcasts_url
34
+ # #listens_url
35
+ ['feed', 'playlists', 'comments', 'followers', 'favorites', 'following', 'cloudcasts', 'listens'].each do |connection|
36
+ define_method "#{connection}_url" do
37
+ "http://api.mixcloud.com#{@key}#{connection}/"
38
+ end
39
+ end
40
+
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mixcloud
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex Fong
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-17 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: &2161429600 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.6.7
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2161429600
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ requirement: &2161429080 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.6.1
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2161429080
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &2161428580 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 2.7.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2161428580
47
+ description: Ruby wrapper of the Mixcloud API. Visit http://www.mixcloud.com/developers/documentation/
48
+ for more info
49
+ email: actfong@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - lib/mixcloud.rb
55
+ - lib/mixcloud/artist.rb
56
+ - lib/mixcloud/category.rb
57
+ - lib/mixcloud/cloudcast.rb
58
+ - lib/mixcloud/popular_new_hot.rb
59
+ - lib/mixcloud/resource.rb
60
+ - lib/mixcloud/search.rb
61
+ - lib/mixcloud/section.rb
62
+ - lib/mixcloud/tag.rb
63
+ - lib/mixcloud/track.rb
64
+ - lib/mixcloud/url_helper.rb
65
+ - lib/mixcloud/user.rb
66
+ homepage: http://www.github.com/actfong/mixcloud
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.15
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Ruby wrapper of the Mixcloud API. It allows you to builld Mixcloud resources
90
+ as Ruby objects and provides search functionality
91
+ test_files: []
92
+ has_rdoc: