hypem 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/hypem.gemspec +3 -1
- data/lib/hypem.rb +2 -0
- data/lib/hypem/blog.rb +9 -22
- data/lib/hypem/helper.rb +37 -0
- data/lib/hypem/playlist.rb +12 -16
- data/lib/hypem/request.rb +14 -18
- data/lib/hypem/track.rb +19 -28
- data/lib/hypem/user.rb +26 -35
- data/lib/hypem/version.rb +1 -1
- data/spec/blog_spec.rb +5 -11
- data/spec/fixtures/vcr_cassettes/blog.yml +44 -0
- data/{fixtures → spec/fixtures}/vcr_cassettes/blog_all.yml +98 -143
- data/spec/fixtures/vcr_cassettes/latest_playlist.yml +238 -0
- data/spec/fixtures/vcr_cassettes/single_track.yml +48 -0
- data/spec/fixtures/vcr_cassettes/user_favorite_blogs.yml +68 -0
- data/spec/fixtures/vcr_cassettes/user_friends.yml +46 -0
- data/spec/fixtures/vcr_cassettes/user_profile.yml +46 -0
- data/spec/playlist_spec.rb +2 -8
- data/spec/request_spec.rb +0 -28
- data/spec/spec_helper.rb +3 -3
- data/spec/track_spec.rb +2 -5
- data/spec/user_spec.rb +5 -5
- metadata +53 -33
- data/fixtures/vcr_cassettes/blog.yml +0 -42
- data/fixtures/vcr_cassettes/blog_playlist.yml +0 -156
- data/fixtures/vcr_cassettes/feed_playlist.yml +0 -150
- data/fixtures/vcr_cassettes/fresh_playlist.yml +0 -230
- data/fixtures/vcr_cassettes/friends_history.yml +0 -144
- data/fixtures/vcr_cassettes/friends_playlist.yml +0 -150
- data/fixtures/vcr_cassettes/latest_playlist.yml +0 -216
- data/fixtures/vcr_cassettes/loved_playlist.yml +0 -146
- data/fixtures/vcr_cassettes/obsessed_playlist.yml +0 -151
- data/fixtures/vcr_cassettes/popular_playlist.yml +0 -167
- data/fixtures/vcr_cassettes/single_track.yml +0 -47
- data/fixtures/vcr_cassettes/user_favorite_blogs.yml +0 -61
- data/fixtures/vcr_cassettes/user_friends.yml +0 -44
- data/fixtures/vcr_cassettes/user_profile.yml +0 -44
- data/spec/response_spec.rb +0 -36
data/hypem.gemspec
CHANGED
@@ -20,8 +20,10 @@ Gem::Specification.new do |s|
|
|
20
20
|
|
21
21
|
s.add_development_dependency "rspec", "~> 2.6"
|
22
22
|
s.add_development_dependency "vcr", "~> 2.0"
|
23
|
+
s.add_development_dependency "webmock"
|
23
24
|
s.add_development_dependency "rake"
|
24
|
-
s.add_dependency '
|
25
|
+
s.add_dependency 'httparty'
|
26
|
+
s.add_dependency 'andand'
|
25
27
|
s.add_dependency 'multi_json', '~> 1.1'
|
26
28
|
|
27
29
|
end
|
data/lib/hypem.rb
CHANGED
data/lib/hypem/blog.rb
CHANGED
@@ -1,9 +1,14 @@
|
|
1
1
|
module Hypem
|
2
2
|
class Blog
|
3
|
+
include Helper
|
4
|
+
|
3
5
|
attr_reader :blog_image, :blog_image_small, :first_posted,
|
4
6
|
:followers, :last_posted, :site_name, :site_url,
|
5
7
|
:total_tracks, :id
|
6
8
|
|
9
|
+
convert_keys(siteid: :id, sitename: :site_name, siteurl: :site_url)
|
10
|
+
convert_datetimes(:first_posted, :last_posted)
|
11
|
+
|
7
12
|
def initialize(*args)
|
8
13
|
if args.count == 1
|
9
14
|
@id = args.first.is_a?(Integer) ? args.first : args.first.to_i
|
@@ -11,31 +16,13 @@ module Hypem
|
|
11
16
|
end
|
12
17
|
|
13
18
|
def get_info
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
@has_info = true
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def update_from_response(rsp)
|
22
|
-
@id ||= rsp[:site_id]
|
23
|
-
@blog_image ||= rsp['blog_image']
|
24
|
-
@blog_image_small ||= rsp['blog_image_small']
|
25
|
-
@followers ||= rsp['followers']
|
26
|
-
@id ||= rsp['siteid']
|
27
|
-
@site_name ||= rsp['sitename']
|
28
|
-
@site_url ||= rsp['siteurl']
|
29
|
-
@total_tracks ||= rsp['total_tracks']
|
30
|
-
|
31
|
-
# only from get_info
|
32
|
-
@first_posted ||= Time.at(rsp['first_posted']) unless rsp['first_posted'].nil?
|
33
|
-
@last_posted ||= Time.at(rsp['last_posted']) unless rsp['last_posted'].nil?
|
34
|
-
|
19
|
+
response = Request.get_resource("/get_site_info?siteid=#{id}")
|
20
|
+
update_from_response(response)
|
21
|
+
self
|
35
22
|
end
|
36
23
|
|
37
24
|
def self.all
|
38
|
-
response = Request.
|
25
|
+
response = Request.get_resource("/get_all_blogs")
|
39
26
|
response.map { |b| self.new.tap {|blog| blog.update_from_response(b) } }
|
40
27
|
end
|
41
28
|
end
|
data/lib/hypem/helper.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module Hypem
|
2
|
+
module Helper
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
def update_from_response(raw_hash)
|
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?
|
13
|
+
|
14
|
+
if self.class.datetime_conversions.andand.include? key
|
15
|
+
value = Time.at(value).to_datetime
|
16
|
+
end
|
17
|
+
|
18
|
+
instance_variable_set("@#{key}",value)
|
19
|
+
|
20
|
+
self.class.class_eval { attr_reader key }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
attr_reader :key_conversions, :datetime_conversions
|
26
|
+
|
27
|
+
def convert_keys(values)
|
28
|
+
@key_conversions = values
|
29
|
+
end
|
30
|
+
|
31
|
+
def convert_datetimes(*values)
|
32
|
+
@datetime_conversions = values
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
data/lib/hypem/playlist.rb
CHANGED
@@ -9,30 +9,26 @@ module Hypem
|
|
9
9
|
@type = type
|
10
10
|
@arg = arg
|
11
11
|
@page = page
|
12
|
-
@path = "/playlist/#{@type}/#{@arg}
|
12
|
+
@path = "/playlist/#{@type}/#{@arg}"
|
13
13
|
end
|
14
14
|
|
15
15
|
def get
|
16
|
-
response = Request.
|
17
|
-
@tracks
|
18
|
-
response.
|
19
|
-
|
16
|
+
response = Request.get_data(path)
|
17
|
+
@tracks ||= []
|
18
|
+
response.each{|v| tracks << Track.new(v)}
|
19
|
+
self
|
20
20
|
end
|
21
21
|
|
22
22
|
def next_page
|
23
|
-
Playlist.new(@type,@arg,@page+1).
|
23
|
+
Playlist.new(@type,@arg,@page+1).get
|
24
24
|
end
|
25
25
|
|
26
26
|
def prev_page
|
27
|
-
Playlist.new(@type,@arg,@page-1).
|
27
|
+
Playlist.new(@type,@arg,@page-1).get
|
28
28
|
end
|
29
29
|
|
30
30
|
def page(num)
|
31
|
-
Playlist.new(@type,@arg,num).
|
32
|
-
end
|
33
|
-
|
34
|
-
def url
|
35
|
-
Hypem::ROOT_PATH + path
|
31
|
+
Playlist.new(@type,@arg,num).get
|
36
32
|
end
|
37
33
|
|
38
34
|
def self.new_from_tracks(tracks)
|
@@ -42,7 +38,7 @@ module Hypem
|
|
42
38
|
|
43
39
|
def self.latest(filter=:all,page=nil)
|
44
40
|
raise ArgumentError unless [:all, :noremix, :remix, :fresh].include? filter
|
45
|
-
Playlist.new(:latest,filter,page).
|
41
|
+
Playlist.new(:latest,filter,page).get
|
46
42
|
end
|
47
43
|
|
48
44
|
def self.popular(arg=POPULAR_ARGS.first,page=nil)
|
@@ -52,11 +48,11 @@ module Hypem
|
|
52
48
|
end
|
53
49
|
|
54
50
|
def self.friends_history(user,page=nil)
|
55
|
-
Playlist.new(:people_history,user,page)
|
51
|
+
Playlist.new(:people_history,user,page)
|
56
52
|
end
|
57
53
|
|
58
54
|
def self.friends_favorites(user,page=nil)
|
59
|
-
Playlist.new(:people,user,page)
|
55
|
+
Playlist.new(:people,user,page)
|
60
56
|
end
|
61
57
|
|
62
58
|
def self.tags(list,page)
|
@@ -69,7 +65,7 @@ module Hypem
|
|
69
65
|
# meta method definitions for generic playlists
|
70
66
|
GENERIC_METHODS.each do |method|
|
71
67
|
define_singleton_method(method) do |arg,page=nil|
|
72
|
-
Playlist.new(method,arg,page)
|
68
|
+
Playlist.new(method,arg,page)
|
73
69
|
end
|
74
70
|
end
|
75
71
|
|
data/lib/hypem/request.rb
CHANGED
@@ -1,29 +1,25 @@
|
|
1
|
-
require '
|
1
|
+
require 'httparty'
|
2
2
|
|
3
3
|
module Hypem
|
4
|
-
ROOT_PATH = 'http://hypem.com'
|
5
4
|
class Request
|
6
|
-
|
5
|
+
include HTTParty
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
debug_output
|
8
|
+
base_uri 'http://api.hypem.com'
|
9
|
+
headers 'Accept' => 'text/html'
|
10
|
+
format :json
|
11
11
|
|
12
|
-
def
|
13
|
-
|
12
|
+
def self.get_resource(path, query=nil)
|
13
|
+
get('/api' + path, query: query)
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
17
|
-
|
16
|
+
def self.get_data(path, page=1)
|
17
|
+
url = [path, 'json', page].join('/')
|
18
|
+
response = get(url)
|
19
|
+
response.delete("version")
|
20
|
+
values = response.values
|
21
|
+
values.size == 1 ? values.first : values
|
18
22
|
end
|
19
23
|
|
20
|
-
private
|
21
|
-
|
22
|
-
def connection
|
23
|
-
@@conn ||= Faraday.new(url: ROOT_PATH) do |builder|
|
24
|
-
builder.request :url_encoded
|
25
|
-
builder.adapter :net_http
|
26
|
-
end
|
27
|
-
end
|
28
24
|
end
|
29
25
|
end
|
data/lib/hypem/track.rb
CHANGED
@@ -1,22 +1,10 @@
|
|
1
1
|
module Hypem
|
2
2
|
class Track
|
3
|
-
|
3
|
+
include Helper
|
4
4
|
|
5
|
-
|
6
|
-
if arg.is_a? Hash
|
7
|
-
keys_to_attributes arg
|
8
|
-
elsif arg.is_a? String
|
9
|
-
@media_id = arg
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def get
|
14
|
-
request = Request.new("/playlist/item/#{media_id}/json/1").tap(&:get)
|
15
|
-
raw_hash = request.response.body.tap(&:shift)["0"]
|
16
|
-
keys_to_attributes raw_hash
|
17
|
-
end
|
5
|
+
attr_accessor :media_id
|
18
6
|
|
19
|
-
|
7
|
+
convert_keys(
|
20
8
|
mediaid: :media_id,
|
21
9
|
dateposted: :dated_posted,
|
22
10
|
siteid: :site_id,
|
@@ -29,23 +17,26 @@ module Hypem
|
|
29
17
|
sitename: :site_name,
|
30
18
|
dateposted: :date_posted,
|
31
19
|
sitename_first: :site_name_first
|
32
|
-
|
20
|
+
)
|
33
21
|
|
34
|
-
|
22
|
+
convert_datetimes :date_posted, :date_posted_first
|
35
23
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
value = Time.at(value).to_datetime if DATETIME_CONVERSIONS.include? key
|
44
|
-
instance_variable_set("@#{key}",value)
|
45
|
-
|
46
|
-
self.class.class_eval { attr_reader key }
|
24
|
+
def initialize(arg)
|
25
|
+
if arg.is_a? Hash
|
26
|
+
update_from_response arg
|
27
|
+
elsif arg.is_a? String
|
28
|
+
@media_id = arg
|
29
|
+
else
|
30
|
+
raise
|
47
31
|
end
|
48
32
|
end
|
49
33
|
|
34
|
+
def get
|
35
|
+
response = Request.get_data("/playlist/item/#{media_id}")
|
36
|
+
update_from_response response
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
40
|
+
|
50
41
|
end
|
51
42
|
end
|
data/lib/hypem/user.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
module Hypem
|
2
2
|
class User
|
3
|
+
include Helper
|
4
|
+
|
5
|
+
convert_keys fullname: :full_name, userpic: :image_url, joined_ts: :joined_at
|
6
|
+
convert_datetimes :joined_at
|
7
|
+
|
3
8
|
attr_reader :name, :full_name, :joined_at, :location, :twitter_username, :image_url,
|
4
9
|
:followed_users_count, :followed_items_count, :followed_sites_count,
|
5
10
|
:followed_queries_count, :friends
|
@@ -9,23 +14,19 @@ module Hypem
|
|
9
14
|
@name = name
|
10
15
|
end
|
11
16
|
|
12
|
-
def
|
13
|
-
|
14
|
-
request.get
|
15
|
-
request.response.body
|
17
|
+
def get(resource)
|
18
|
+
Request.get_resource("#{resource}?username=#{@name}")
|
16
19
|
end
|
17
20
|
|
18
21
|
def get_profile
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
24
|
-
return self
|
22
|
+
response = get('/get_profile')
|
23
|
+
flattened_response = flatten_response(response)
|
24
|
+
update_from_response(response)
|
25
|
+
self
|
25
26
|
end
|
26
27
|
|
27
|
-
def
|
28
|
-
response =
|
28
|
+
def favorite_blogs
|
29
|
+
response = get('/get_favorite_blogs')
|
29
30
|
|
30
31
|
response.map do |r|
|
31
32
|
blog = Hypem::Blog.new(r['siteid'])
|
@@ -34,12 +35,8 @@ module Hypem
|
|
34
35
|
end
|
35
36
|
end
|
36
37
|
|
37
|
-
def
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
def get_friends
|
42
|
-
response = api_request('get_friends')
|
38
|
+
def friends
|
39
|
+
response = get('/get_friends')
|
43
40
|
|
44
41
|
response.map do |r|
|
45
42
|
user = User.new(r['username'])
|
@@ -48,23 +45,6 @@ module Hypem
|
|
48
45
|
end
|
49
46
|
end
|
50
47
|
|
51
|
-
def friends
|
52
|
-
@friends ||= get_friends
|
53
|
-
end
|
54
|
-
|
55
|
-
def update_from_response(response)
|
56
|
-
@full_name ||= response['fullname']
|
57
|
-
@location ||= response['location']
|
58
|
-
@image_url ||= response['userpic']
|
59
|
-
@followed_users_count ||= response['favorites_count']['user']
|
60
|
-
@followed_items_count ||= response['favorites_count']['item']
|
61
|
-
@followed_sites_count ||= response['favorites_count']['site']
|
62
|
-
@followed_queries_count ||= response['favorites_count']['query']
|
63
|
-
|
64
|
-
# only returned on get_profile
|
65
|
-
@joined_at ||= Time.at(response['joined_ts']) unless response['joined_ts'].nil?
|
66
|
-
@twitter_username ||= response['twitter_username']
|
67
|
-
end
|
68
48
|
|
69
49
|
#playlist requests
|
70
50
|
|
@@ -87,5 +67,16 @@ module Hypem
|
|
87
67
|
def friends_history_playlist(page=1)
|
88
68
|
@f_h_p ||= Playlist.friends_history(@name,page)
|
89
69
|
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def flatten_response(response)
|
74
|
+
count_hash = response.delete('favorites_count')
|
75
|
+
count_hash.each do |k,v|
|
76
|
+
pluralized_key = k == 'query' ? 'queries' : k + 's'
|
77
|
+
response["followed_#{pluralized_key}_count"] = v
|
78
|
+
end
|
79
|
+
response
|
80
|
+
end
|
90
81
|
end
|
91
82
|
end
|
data/lib/hypem/version.rb
CHANGED
data/spec/blog_spec.rb
CHANGED
@@ -14,23 +14,17 @@ describe Hypem::Blog do
|
|
14
14
|
|
15
15
|
describe "#get_info" do
|
16
16
|
subject do
|
17
|
-
VCR.use_cassette('blog') {blog.
|
17
|
+
VCR.use_cassette('blog') {blog.get_info}
|
18
18
|
end
|
19
19
|
specify {subject.should be_a Hypem::Blog}
|
20
20
|
specify {subject.site_name.should == 'Pasta Primavera'}
|
21
21
|
specify {subject.blog_image.should == 'http://static-ak.hypem.net/images/blog_images/4632.jpg'}
|
22
22
|
specify {subject.blog_image_small.should == 'http://static-ak.hypem.net/images/blog_images/small/4632.jpg'}
|
23
23
|
specify {subject.site_url.should == 'http://pastaprima.net'}
|
24
|
-
specify {subject.total_tracks.should
|
25
|
-
specify {subject.first_posted.should be_a
|
26
|
-
specify {subject.last_posted.should be_a
|
27
|
-
specify {subject.followers.should
|
28
|
-
|
29
|
-
it "caches its response" do
|
30
|
-
subject
|
31
|
-
Hypem::Request.should_not_receive :new
|
32
|
-
VCR.use_cassette('blog') { blog.get_info }
|
33
|
-
end
|
24
|
+
specify {subject.total_tracks.should be_an Integer}
|
25
|
+
specify {subject.first_posted.should be_a DateTime}
|
26
|
+
specify {subject.last_posted.should be_a DateTime}
|
27
|
+
specify {subject.followers.should be_an Integer}
|
34
28
|
|
35
29
|
end
|
36
30
|
|
@@ -0,0 +1,44 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: http://api.hypem.com/api/get_site_info?siteid=4632
|
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.0.12
|
19
|
+
Date:
|
20
|
+
- Tue, 26 Jun 2012 02:52:09 GMT
|
21
|
+
Content-Type:
|
22
|
+
- text/html
|
23
|
+
Transfer-Encoding:
|
24
|
+
- chunked
|
25
|
+
Connection:
|
26
|
+
- keep-alive
|
27
|
+
Keep-Alive:
|
28
|
+
- timeout=10
|
29
|
+
Set-Cookie:
|
30
|
+
- AUTH=03%3Af1682d84385b013d5d5ddb49ad4b6aba%3A1340679128%3A1276002699%3ACA-US;
|
31
|
+
expires=Thu, 22-Jun-2028 02:52:08 GMT; path=/; domain=hypem.com
|
32
|
+
X-Hacker:
|
33
|
+
- Hey, if you're reading this, you should drop us an email at hypem.com/contact,
|
34
|
+
maybe we can work together!
|
35
|
+
Access-Control-Allow-Origin:
|
36
|
+
- ! '*'
|
37
|
+
Access-Control-Allow-Headers:
|
38
|
+
- ! '*'
|
39
|
+
body:
|
40
|
+
encoding: US-ASCII
|
41
|
+
string: ! '{"siteid":"4632","sitename":"Pasta Primavera","siteurl":"http:\/\/pastaprima.net","followers":1359,"is_favorite":null,"total_tracks":4104,"last_posted":1340641909,"first_posted":1192804200,"blog_image":"http:\/\/static-ak.hypem.net\/images\/blog_images\/4632.jpg","blog_image_small":"http:\/\/static-ak.hypem.net\/images\/blog_images\/small\/4632.jpg"}'
|
42
|
+
http_version:
|
43
|
+
recorded_at: Tue, 26 Jun 2012 02:52:38 GMT
|
44
|
+
recorded_with: VCR 2.2.0
|