hypem 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Hypem Ruby Gem ![travis](https://secure.travis-ci.org/JackCA/hypem.png?branch=master) #
1
+ # Hypem Ruby Gem [![travis](https://secure.travis-ci.org/JackCA/hypem.png?branch=master)](http://travis-ci.org/JackCA/hypem) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/JackCA/hypem)#
2
2
  ## Introduction ##
3
3
  This is an unoffical Ruby gem for the **Hype Machine** read-only API. It currently only supports Ruby `1.9.x`.
4
4
 
@@ -94,6 +94,15 @@ An example track object inspection is as follows:
94
94
 
95
95
  ******
96
96
 
97
+ ### Track Favorites ###
98
+ The people who liked a track can be retrieved via `Hypem::TrackFavorites('trackid')` or `track.favorites` and synced using the `get` method.
99
+
100
+ Their pagination is supplied via the same methods as the `Hypem::Playlist` ones, and they also accept the desired page as the last argument at initialization : `Hypem::TrackFavorites('trackid', page)`
101
+
102
+ The `Hypem::Users` present in each page can be retrieved via the `users` instance method
103
+
104
+ ******
105
+
97
106
  ## TODO ###
98
107
  - Blogs with given tag
99
108
  - Authenticated Requests
@@ -22,8 +22,8 @@ Gem::Specification.new do |s|
22
22
  s.add_development_dependency "vcr", "~> 2.0"
23
23
  s.add_development_dependency "webmock"
24
24
  s.add_development_dependency "rake"
25
+ s.add_development_dependency "timecop"
25
26
  s.add_dependency 'httparty'
26
27
  s.add_dependency 'andand'
27
- s.add_dependency 'multi_json', '~> 1.1'
28
28
 
29
29
  end
@@ -1,12 +1,15 @@
1
1
  require "andand"
2
2
  require "hypem/helper"
3
- require "hypem/response"
4
3
  require "hypem/request"
5
4
  require "hypem/user"
6
5
  require "hypem/track"
7
6
  require "hypem/playlist"
8
7
  require "hypem/blog"
9
- require "exceptions"
8
+ require "hypem/exceptions"
9
+
10
+ require "hypem/track_favorites_request"
11
+ require "hypem/track_favorites"
12
+
10
13
 
11
14
  module Hypem
12
15
 
@@ -7,9 +7,7 @@ module Hypem
7
7
 
8
8
  def update_from_response(raw_hash)
9
9
  raw_hash.each_pair do |key,value|
10
- key = key.to_sym
11
- new_key = self.class.key_conversions[key]
12
- key = new_key unless new_key.nil?
10
+ key = self.class.key_conversions[key.to_sym] || key.to_sym
13
11
 
14
12
  if self.class.datetime_conversions.andand.include? key
15
13
  value = Time.at(value).to_datetime
@@ -37,6 +37,9 @@ module Hypem
37
37
  self
38
38
  end
39
39
 
40
+ def favorites(page=nil)
41
+ @track_favorites ||= TrackFavorites.new(@media_id)
42
+ end
40
43
 
41
44
  end
42
45
  end
@@ -0,0 +1,49 @@
1
+ module Hypem
2
+ class TrackFavorites
3
+ include Helper
4
+
5
+ attr_accessor :users, :media_id
6
+
7
+ LIMIT = 20
8
+
9
+ def initialize(media_id, page=nil)
10
+ page = 1 if page.nil?
11
+ @media_id = media_id
12
+ @page = page
13
+ end
14
+
15
+ def get
16
+ user_ids = TrackFavoritesRequest.get_data(path)
17
+ @users = user_ids.map{|user_id| User.new(user_id)}
18
+ self
19
+ end
20
+
21
+ def next_page
22
+ TrackFavorites.new(@media_id,@page+1).get
23
+ end
24
+
25
+ def prev_page
26
+ TrackFavorites.new(@media_id,@page+1).get
27
+ end
28
+
29
+ def page(num)
30
+ TrackFavorites.new(@media_id,num).get
31
+ end
32
+
33
+ private
34
+
35
+ def path
36
+ timestamp = Time.now.to_i
37
+ items_to_skip = (@page - 1)*LIMIT
38
+
39
+ return "/serve_activity_info.php?" + [
40
+ "type=favorites",
41
+ "id=#{media_id}",
42
+ "skip=#{items_to_skip}",
43
+ "limit=#{LIMIT}",
44
+ "ts=#{timestamp}"
45
+ ].join("&")
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,14 @@
1
+ module Hypem
2
+ class TrackFavoritesRequest < Request
3
+
4
+ base_uri 'http://hypem.com/inc'
5
+ format :html
6
+
7
+ # Extract the response in a user names enumerator
8
+ def self.get_data(path)
9
+ response = get(path)
10
+ return response.body.scan(/<span>(\w*)<\/span>/).flatten
11
+ end
12
+
13
+ end
14
+ end
@@ -14,19 +14,15 @@ module Hypem
14
14
  @name = name
15
15
  end
16
16
 
17
- def get(resource)
18
- Request.get_resource("#{resource}?username=#{@name}")
19
- end
20
-
21
17
  def get_profile
22
- response = get('/get_profile')
18
+ response = get_resource('/get_profile')
23
19
  flattened_response = flatten_response(response)
24
- update_from_response(response)
20
+ update_from_response(flattened_response)
25
21
  self
26
22
  end
27
23
 
28
24
  def favorite_blogs
29
- response = get('/get_favorite_blogs')
25
+ response = get_resource('/get_favorite_blogs')
30
26
 
31
27
  response.map do |r|
32
28
  blog = Hypem::Blog.new(r['siteid'])
@@ -36,7 +32,7 @@ module Hypem
36
32
  end
37
33
 
38
34
  def friends
39
- response = get('/get_friends')
35
+ response = get_resource('/get_friends')
40
36
 
41
37
  response.map do |r|
42
38
  user = User.new(r['username'])
@@ -45,7 +41,6 @@ module Hypem
45
41
  end
46
42
  end
47
43
 
48
-
49
44
  #playlist requests
50
45
 
51
46
  def loved_playlist(page=1)
@@ -78,5 +73,9 @@ module Hypem
78
73
  end
79
74
  response
80
75
  end
76
+
77
+ def get_resource(resource)
78
+ Request.get_resource("#{resource}?username=#{@name}")
79
+ end
81
80
  end
82
81
  end
@@ -1,3 +1,3 @@
1
1
  module Hypem
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,80 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://hypem.com/inc/serve_activity_info.php?id=1jsw9&limit=20&skip=0&ts=1350518333&type=favorites
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - text/html
12
+ response:
13
+ status:
14
+ code: 200
15
+ message: OK
16
+ headers:
17
+ Server:
18
+ - nginx/1.2.3
19
+ Date:
20
+ - Wed, 17 Oct 2012 23:58:53 GMT
21
+ Content-Type:
22
+ - text/html; charset=UTF-8
23
+ Transfer-Encoding:
24
+ - chunked
25
+ Connection:
26
+ - keep-alive
27
+ Keep-Alive:
28
+ - timeout=10
29
+ body:
30
+ encoding: US-ASCII
31
+ string: ! '<div class="favcountlist"><div class="close-this"></div><h4>Loved
32
+ By</h4><ul><li ><a href="/Celia_BL"><img src=''http://faces-s3.hypem.com/1240942225538552_75.png''
33
+ class="avatar-s" width="30" height="30" title=''Celia_BL loved this 4 days
34
+ ago''/> <span>Celia_BL</span></a><li ><a href="/stephronson"><img src=''http://faces-s3.hypem.com/2208821703498672_75.png''
35
+ class="avatar-s" width="30" height="30" title=''stephronson loved this 5 days
36
+ ago''/> <span>stephronson</span></a><li ><a href="/patchouli89"><img src=''http://faces-s3.hypem.com/915584976021997_75.png''
37
+ class="avatar-s" width="30" height="30" title=''patchouli89 loved this last
38
+ week''/> <span>patchouli89</span></a><li ><a href="/DubTasticSemen"><img
39
+ src=''http://faces-s3.hypem.com/4899791450234933_75.png'' class="avatar-s"
40
+ width="30" height="30" title=''DubTasticSemen loved this last week''/> <span>DubTasticSemen</span></a><li
41
+ ><a href="/katiep"><img src=''http://faces-s3.hypem.com/183261887691666_75.png''
42
+ class="avatar-s" width="30" height="30" title=''katiep loved this on Oct 1st''/>
43
+ <span>katiep</span></a><li ><a href="/Darktrain"><img src=''http://faces-s3.hypem.com/129147371270505_75.png''
44
+ class="avatar-s" width="30" height="30" title=''Darktrain loved this on Oct
45
+ 1st''/> <span>Darktrain</span></a><li ><a href="/aubreycamille"><img src=''http://faces-s3.hypem.com/443902290006435_75.png''
46
+ class="avatar-s" width="30" height="30" title=''aubreycamille loved this on
47
+ Sep 30th''/> <span>aubreycamille</span></a><li ><a href="/Makailax"><img
48
+ src=''http://faces-s3.hypem.com/6829031078555215_75.png'' class="avatar-s"
49
+ width="30" height="30" title=''Makailax loved this on Sep 26th''/> <span>Makailax</span></a><li
50
+ ><a href="/picapwel"><img src=''http://faces-s3.hypem.com/861286451970126_75.png''
51
+ class="avatar-s" width="30" height="30" title=''picapwel loved this on Sep
52
+ 26th''/> <span>picapwel</span></a><li ><a href="/SpaghettiArms"><img src=''http://faces-s3.hypem.com/802561691180543_75.png''
53
+ class="avatar-s" width="30" height="30" title=''SpaghettiArms loved this on
54
+ Sep 24th''/> <span>SpaghettiArms</span></a><li ><a href="/vachco"><img src=''http://faces-s3.hypem.com/11657621703909853_75.png''
55
+ class="avatar-s" width="30" height="30" title=''vachco loved this on Sep 21st''/>
56
+ <span>vachco</span></a><li ><a href="/jrodney"><img src=''http://faces-s3.hypem.com/3629451415173087_75.png''
57
+ class="avatar-s" width="30" height="30" title=''jrodney loved this on Sep
58
+ 21st''/> <span>jrodney</span></a><li ><a href="/ciscogoya"><img src=''http://faces-s3.hypem.com/69212976015486_75.png''
59
+ class="avatar-s" width="30" height="30" title=''ciscogoya loved this on Sep
60
+ 21st''/> <span>ciscogoya</span></a><li ><a href="/jillian8"><img src=''http://faces-s3.hypem.com/6808391081805987_75.png''
61
+ class="avatar-s" width="30" height="30" title=''jillian8 loved this on Sep
62
+ 20th''/> <span>jillian8</span></a><li ><a href="/jennagordon"><img src=''http://faces-s3.hypem.com/12569241449517956_75.png''
63
+ class="avatar-s" width="30" height="30" title=''jennagordon loved this on
64
+ Sep 20th''/> <span>jennagordon</span></a><li ><a href="/fnezrob"><img src=''http://faces-s3.hypem.com/1010779881277052_75.png''
65
+ class="avatar-s" width="30" height="30" title=''fnezrob loved this on Sep
66
+ 17th''/> <span>fnezrob</span></a><li ><a href="/Terijan"><img src=''http://faces-s3.hypem.com/7834091852750270_75.png''
67
+ class="avatar-s" width="30" height="30" title=''Terijan loved this on Sep
68
+ 17th''/> <span>Terijan</span></a><li ><a href="/randoo"><img src=''http://faces-s3.hypem.com/9562582079546783_75.png''
69
+ class="avatar-s" width="30" height="30" title=''randoo loved this on Sep 17th''/>
70
+ <span>randoo</span></a><li ><a href="/hhoneymoon"><img src=''http://faces-s3.hypem.com/1279187495880144_75.png''
71
+ class="avatar-s" width="30" height="30" title=''hhoneymoon loved this on Sep
72
+ 17th''/> <span>hhoneymoon</span></a><li ><a href="/Tunch"><img src=''http://faces-s3.hypem.com/3887221963549886_75.png''
73
+ class="avatar-s" width="30" height="30" title=''Tunch loved this on Sep 17th''/>
74
+ <span>Tunch</span></a></ul><a href="" onclick="update_item_activity(''favorites'',
75
+ ''1jsw9'', ''20'', ''20''); return false;" class="fav-paging">Next 20 &raquo;</a><script
76
+ type="text/javascript">jQuery(".avatar-s img[title]").tooltip({ position:
77
+ "top center", offset: [-2,0]});</script></div>'
78
+ http_version:
79
+ recorded_at: Wed, 17 Oct 2012 23:58:53 GMT
80
+ recorded_with: VCR 2.2.5
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hypem::TrackFavoritesRequest do
4
+
5
+ describe "#get_data" do
6
+ # Tested in Hypem::TrackFavorites#get
7
+ end
8
+
9
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+ require 'timecop'
3
+
4
+ describe Hypem::TrackFavorites do
5
+
6
+ let(:media_id) { '1jsw9' }
7
+ let(:track_favorites_from_string) { described_class.new(media_id) }
8
+
9
+ describe "#get" do
10
+ subject do
11
+
12
+ # Fetching the last request date, so the timestamp in the request match,
13
+ # and VCR doesn't try to fetch a new episode
14
+ @last_request_time = VCR::Cassette.new("track_favorites").serializable_hash.values.first.last["recorded_at"]
15
+
16
+ Timecop.freeze(@last_request_time) do
17
+ VCR.use_cassette("track_favorites") do
18
+ track_favorites_from_string.get
19
+ end
20
+ end
21
+ end
22
+
23
+ its(:users) { should be_a Array }
24
+ end
25
+
26
+
27
+
28
+ end
@@ -6,7 +6,7 @@ describe Hypem::Track do
6
6
  Hypem::Request.get('/playlist/latest/all/json/1')["0"]
7
7
  end
8
8
  end
9
-
9
+
10
10
  let(:media_id) { '1jsw9' }
11
11
  let(:track_from_hash) { described_class.new(track_hash) }
12
12
  let(:track_from_string) { described_class.new(media_id) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hypem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-27 00:00:00.000000000 Z
12
+ date: 2012-10-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70284220238720 !ruby/object:Gem::Requirement
16
+ requirement: &70109952730500 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '2.6'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70284220238720
24
+ version_requirements: *70109952730500
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: vcr
27
- requirement: &70284220238260 !ruby/object:Gem::Requirement
27
+ requirement: &70109952730040 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '2.0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70284220238260
35
+ version_requirements: *70109952730040
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: webmock
38
- requirement: &70284220237880 !ruby/object:Gem::Requirement
38
+ requirement: &70109952729660 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70284220237880
46
+ version_requirements: *70109952729660
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &70284220237420 !ruby/object:Gem::Requirement
49
+ requirement: &70109952729200 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,21 +54,21 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70284220237420
57
+ version_requirements: *70109952729200
58
58
  - !ruby/object:Gem::Dependency
59
- name: httparty
60
- requirement: &70284220237000 !ruby/object:Gem::Requirement
59
+ name: timecop
60
+ requirement: &70109952728760 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
64
64
  - !ruby/object:Gem::Version
65
65
  version: '0'
66
- type: :runtime
66
+ type: :development
67
67
  prerelease: false
68
- version_requirements: *70284220237000
68
+ version_requirements: *70109952728760
69
69
  - !ruby/object:Gem::Dependency
70
- name: andand
71
- requirement: &70284220236580 !ruby/object:Gem::Requirement
70
+ name: httparty
71
+ requirement: &70109952728340 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,18 +76,18 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *70284220236580
79
+ version_requirements: *70109952728340
80
80
  - !ruby/object:Gem::Dependency
81
- name: multi_json
82
- requirement: &70284220236080 !ruby/object:Gem::Requirement
81
+ name: andand
82
+ requirement: &70109952727920 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
- - - ~>
85
+ - - ! '>='
86
86
  - !ruby/object:Gem::Version
87
- version: '1.1'
87
+ version: '0'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *70284220236080
90
+ version_requirements: *70109952727920
91
91
  description:
92
92
  email:
93
93
  - jackcanderson@gmail.com
@@ -102,14 +102,15 @@ files:
102
102
  - README.md
103
103
  - Rakefile
104
104
  - hypem.gemspec
105
- - lib/exceptions.rb
106
105
  - lib/hypem.rb
107
106
  - lib/hypem/blog.rb
107
+ - lib/hypem/exceptions.rb
108
108
  - lib/hypem/helper.rb
109
109
  - lib/hypem/playlist.rb
110
110
  - lib/hypem/request.rb
111
- - lib/hypem/response.rb
112
111
  - lib/hypem/track.rb
112
+ - lib/hypem/track_favorites.rb
113
+ - lib/hypem/track_favorites_request.rb
113
114
  - lib/hypem/user.rb
114
115
  - lib/hypem/version.rb
115
116
  - spec/blog_spec.rb
@@ -117,6 +118,7 @@ files:
117
118
  - spec/fixtures/vcr_cassettes/blog_all.yml
118
119
  - spec/fixtures/vcr_cassettes/latest_playlist.yml
119
120
  - spec/fixtures/vcr_cassettes/single_track.yml
121
+ - spec/fixtures/vcr_cassettes/track_favorites.yml
120
122
  - spec/fixtures/vcr_cassettes/user_favorite_blogs.yml
121
123
  - spec/fixtures/vcr_cassettes/user_friends.yml
122
124
  - spec/fixtures/vcr_cassettes/user_profile.yml
@@ -124,6 +126,8 @@ files:
124
126
  - spec/playlist_spec.rb
125
127
  - spec/request_spec.rb
126
128
  - spec/spec_helper.rb
129
+ - spec/track_favorites_request_spec.rb
130
+ - spec/track_favorites_spec.rb
127
131
  - spec/track_spec.rb
128
132
  - spec/user_spec.rb
129
133
  homepage: https://github.com/JackCA/hypem
@@ -146,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
150
  version: '0'
147
151
  segments:
148
152
  - 0
149
- hash: -2570760148147230779
153
+ hash: -3140532029222442359
150
154
  requirements: []
151
155
  rubyforge_project: hypem
152
156
  rubygems_version: 1.8.10
@@ -159,6 +163,7 @@ test_files:
159
163
  - spec/fixtures/vcr_cassettes/blog_all.yml
160
164
  - spec/fixtures/vcr_cassettes/latest_playlist.yml
161
165
  - spec/fixtures/vcr_cassettes/single_track.yml
166
+ - spec/fixtures/vcr_cassettes/track_favorites.yml
162
167
  - spec/fixtures/vcr_cassettes/user_favorite_blogs.yml
163
168
  - spec/fixtures/vcr_cassettes/user_friends.yml
164
169
  - spec/fixtures/vcr_cassettes/user_profile.yml
@@ -166,5 +171,7 @@ test_files:
166
171
  - spec/playlist_spec.rb
167
172
  - spec/request_spec.rb
168
173
  - spec/spec_helper.rb
174
+ - spec/track_favorites_request_spec.rb
175
+ - spec/track_favorites_spec.rb
169
176
  - spec/track_spec.rb
170
177
  - spec/user_spec.rb
@@ -1,14 +0,0 @@
1
- require 'multi_json'
2
-
3
- module Hypem
4
- class Response
5
- attr_accessor :body
6
-
7
- def initialize(raw_response)
8
- raise RequestError if raw_response.body == 'null'
9
-
10
- @body = MultiJson.decode raw_response.body
11
- end
12
-
13
- end
14
- end