smartfm 0.4.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/ChangeLog +6 -2
  2. data/README +3 -10
  3. data/lib/smartfm.rb +3 -2
  4. data/lib/smartfm/core/version.rb +2 -2
  5. data/lib/smartfm/models.rb +7 -0
  6. data/lib/smartfm/{model → models}/base.rb +2 -1
  7. data/lib/smartfm/{model → models}/item.rb +18 -86
  8. data/lib/smartfm/models/like.rb +20 -0
  9. data/lib/smartfm/{model → models}/list.rb +35 -64
  10. data/lib/smartfm/models/notification.rb +26 -0
  11. data/lib/smartfm/models/sentence.rb +70 -0
  12. data/lib/smartfm/{model → models}/user.rb +54 -16
  13. data/lib/smartfm/modules.rb +4 -0
  14. data/lib/smartfm/modules/acts_as_likable.rb +16 -0
  15. data/lib/smartfm/modules/media_support.rb +37 -0
  16. data/lib/smartfm/modules/private_content.rb +25 -0
  17. data/lib/smartfm/modules/public_content.rb +51 -0
  18. data/lib/smartfm/rest_clients.rb +9 -0
  19. data/lib/smartfm/{rest_client → rest_clients}/base.rb +0 -0
  20. data/lib/smartfm/rest_clients/item.rb +17 -0
  21. data/lib/smartfm/rest_clients/like.rb +7 -0
  22. data/lib/smartfm/rest_clients/list.rb +18 -0
  23. data/lib/smartfm/rest_clients/notification.rb +8 -0
  24. data/lib/smartfm/rest_clients/sentence.rb +14 -0
  25. data/lib/smartfm/rest_clients/user.rb +20 -0
  26. data/spec/smartfm/{model → models}/base_spec.rb +5 -3
  27. data/spec/smartfm/{model → models}/item_spec.rb +24 -5
  28. data/spec/smartfm/models/like_spec.rb +19 -0
  29. data/spec/smartfm/models/list_spec.rb +70 -0
  30. data/spec/smartfm/models/notification_spec.rb +11 -0
  31. data/spec/smartfm/models/sentence_spec.rb +11 -0
  32. data/spec/smartfm/{model → models}/user_spec.rb +53 -19
  33. data/spec/smartfm/{rest_client → rest_clients}/base_spec.rb +0 -0
  34. data/spec/smartfm/{rest_client → rest_clients}/item_spec.rb +0 -0
  35. data/spec/smartfm/rest_clients/like_spec.rb +7 -0
  36. data/spec/smartfm/{rest_client → rest_clients}/list_spec.rb +0 -0
  37. data/spec/smartfm/rest_clients/notification_spec.rb +7 -0
  38. data/spec/smartfm/{rest_client → rest_clients}/sentence_spec.rb +0 -0
  39. data/spec/smartfm/{rest_client → rest_clients}/user_spec.rb +0 -0
  40. metadata +37 -24
  41. data/lib/smartfm/model.rb +0 -5
  42. data/lib/smartfm/model/sentence.rb +0 -118
  43. data/lib/smartfm/rest_client.rb +0 -8
  44. data/lib/smartfm/rest_client/item.rb +0 -14
  45. data/lib/smartfm/rest_client/list.rb +0 -15
  46. data/lib/smartfm/rest_client/sentence.rb +0 -12
  47. data/lib/smartfm/rest_client/user.rb +0 -14
  48. data/spec/smartfm/model/list_spec.rb +0 -7
  49. data/spec/smartfm/model/sentence_spec.rb +0 -7
@@ -2,6 +2,9 @@ class Smartfm::User < Smartfm::Base
2
2
  ATTRIBUTES = [:username, :profile]
3
3
  attr_reader *ATTRIBUTES
4
4
 
5
+ def self.rest_client; Smartfm::RestClient::User; end
6
+ def rest_client; self.class.rest_client; end
7
+
5
8
  class Profile < Smartfm::Base
6
9
  ATTRIBUTES = [:name, :gender, :birthday, :description, :blog_url, :profile_url, :foaf_url, :icon_url]
7
10
  attr_reader *ATTRIBUTES
@@ -58,54 +61,89 @@ class Smartfm::User < Smartfm::Base
58
61
  @results = self.deserialize(params[:study_results], :as => Smartfm::User::Study::Result)
59
62
  @total_summary = self.deserialize(params[:total_summary], :as => Smartfm::User::Study::TotalSummary)
60
63
  end
64
+ end
61
65
 
66
+ def initialize(params)
67
+ @profile = Profile.new(params[:profile])
68
+ @username = params[:username]
62
69
  end
63
70
 
64
71
  def self.find(username, params = {})
65
72
  params[:username] = username
66
- hash = Smartfm::RestClient::User.find(params)
73
+ hash = self.rest_client.find(params)
67
74
  self.deserialize(hash)
68
75
  end
69
76
 
70
77
  def self.matching(keyword, params = {})
71
78
  params[:keyword] = keyword
72
- hash = Smartfm::RestClient::User.matching(params)
79
+ hash = self.rest_client.matching(params)
73
80
  self.deserialize(hash) || []
74
81
  end
75
82
 
76
- def self.username(auth)
77
- Smartfm::RestClient::User.username(auth, {})
83
+ def self.current(auth, params = {})
84
+ self.rest_client.current(auth, params)
78
85
  end
79
86
 
80
- def initialize(params)
81
- @profile = Profile.new(params[:profile])
82
- @username = params[:username]
87
+ def self.friends(auth, params = {})
88
+ self.rest_client.friends_of_current(auth, params)
89
+ end
90
+
91
+ def self.followers(auth, params = {})
92
+ self.rest_client.followers_of_current(auth, params)
93
+ end
94
+
95
+ def friends(params = {})
96
+ hash = self.rest_client.friends(params.merge(:username => self.username))
97
+ self.deserialize(hash) || []
98
+ end
99
+
100
+ def followers(params = {})
101
+ hash = self.rest_client.followers(params.merge(:username => self.username))
102
+ self.deserialize(hash) || []
103
+ end
104
+
105
+ def friends_of_current(auth, params = {})
106
+ hash = self.rest_client.friends_of_current(auth, params)
107
+ self.deserialize(hash) || []
108
+ end
109
+
110
+ def followers_of_current(auth, params = {})
111
+ hash = self.rest_client.followers_of_current(auth, params)
112
+ self.deserialize(hash) || []
113
+ end
114
+
115
+ def follow!(auth, params = {})
116
+ self.rest_client.follow!(auth, params.merge(:username => self.username))
117
+ end
118
+
119
+ def unfollow!(auth, params = {})
120
+ self.rest_client.unfollow!(auth, params.merge(:username => self.username))
83
121
  end
84
122
 
85
123
  def items(params = {})
86
- hash = Smartfm::RestClient::User.items(params.merge(:username => self.username))
124
+ hash = self.rest_client.items(params.merge(:username => self.username))
87
125
  self.deserialize(hash, :as => Smartfm::Item) || []
88
126
  end
89
127
 
90
128
  def lists(params = {})
91
- hash = Smartfm::RestClient::User.lists(params.merge(:username => self.username))
129
+ hash = self.rest_client.lists(params.merge(:username => self.username))
92
130
  self.deserialize(hash, :as => Smartfm::List) || []
93
131
  end
94
132
 
95
- def friends(params = {})
96
- hash = Smartfm::RestClient::User.friends(params.merge(:username => self.username))
97
- self.deserialize(hash) || []
133
+ def likes(params = {})
134
+ hash = self.rest_client.likes(params.merge(:username => self.username))
135
+ self.deserialize(hash, :as => Smartfm::Like) || []
98
136
  end
99
137
 
100
- def followers(params = {})
101
- hash = Smartfm::RestClient::User.followers(params.merge(:username => self.username))
102
- self.deserialize(hash) || []
138
+ def notifications(params = {})
139
+ hash = self.rest_client.notifications(params.merge(:username => self.username))
140
+ self.deserialize(hash, :as => Smartfm::Notification) || []
103
141
  end
104
142
 
105
143
  def study(params = {})
106
144
  params[:application] ||= 'iknow'
107
145
  return nil unless ['iknow', 'dictation', 'brainspeed', ].include?(params[:application])
108
- hash = Smartfm::RestClient::User.study_results(params.merge(:username => self.username))
146
+ hash = self.rest_client.study_results(params.merge(:username => self.username))
109
147
  self.deserialize(hash, :as => Smartfm::User::Study)
110
148
  end
111
149
 
@@ -0,0 +1,4 @@
1
+ require 'smartfm/modules/acts_as_likable'
2
+ require 'smartfm/modules/private_content'
3
+ require 'smartfm/modules/public_content'
4
+ require 'smartfm/modules/media_support'
@@ -0,0 +1,16 @@
1
+ module Smartfm::ActsAsLikable
2
+
3
+ def likes(params = {})
4
+ hash = self.rest_client.likes(params.merge(:id => self.id))
5
+ self.deserialize(hash, :as => Smartfm::Like) || []
6
+ end
7
+
8
+ def like!(auth, params)
9
+ self.rest_client.like!(auth, params.merge(:id => self.id))
10
+ end
11
+
12
+ def unlike!(auth, params)
13
+ self.rest_client.unlike!(auth, params.merge(:id => self.id))
14
+ end
15
+
16
+ end
@@ -0,0 +1,37 @@
1
+ module Smartfm::MediaSupport
2
+
3
+ def attribution_params(attr_params)
4
+ return {} unless attr_params
5
+ {
6
+ 'attribution[medias_entity]' => attr_params[:media_entity],
7
+ 'attribution[author]' => attr_params[:author],
8
+ 'attribution[author_url]' => attr_params[:author_url],
9
+ 'attributions[attribution_license_id]' => attr_params[:attribution_license_id]
10
+ }
11
+ end
12
+
13
+ def add_image(auth, params)
14
+ post_params = if params.is_a?(String)
15
+ {'image[url]' => params}
16
+ else
17
+ {
18
+ 'image[url]' => params[:url],
19
+ 'image[list_id]' => params[:list_id]
20
+ }.merge(attribution_params(params[:attribution]))
21
+ end
22
+ self.rest_client.add_image(auth, post_params.merge(:id => self.id))
23
+ end
24
+
25
+ def add_sound(auth, params)
26
+ post_params = if params.is_a?(String)
27
+ {'sound[url]' => params}
28
+ else
29
+ {
30
+ 'sound[url]' => params[:url],
31
+ 'sound[list_id]' => params[:list_id]
32
+ }.merge(attribution_params(params[:attribution]))
33
+ end
34
+ self.rest_client.add_sound(auth, post_params.merge(:id => self.id))
35
+ end
36
+
37
+ end
@@ -0,0 +1,25 @@
1
+ module Smartfm::PrivateContent
2
+
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ base.send(:include, InstanceMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def of_current(auth, params = {})
10
+ hash = self.rest_client.of_current(auth, params)
11
+ self.deserialize(hash) || []
12
+ end
13
+
14
+ def create(auth, params = {})
15
+ self.new(params).save
16
+ end
17
+ end
18
+
19
+ module InstanceMethods
20
+ def save(auth)
21
+ self.rest_client.create(auth, self.to_post_data)
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,51 @@
1
+ module Smartfm::PublicContent
2
+
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ base.send(:include, InstanceMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def recent(params = {})
10
+ hash = self.rest_client.recent(params)
11
+ self.deserialize(hash) || []
12
+ end
13
+
14
+ def find(list_id, params = {})
15
+ params[:id] = list_id
16
+ hash = self.rest_client.find(params)
17
+ self.deserialize(hash)
18
+ end
19
+
20
+ def matching(keyword, params = {})
21
+ params[:keyword] = keyword
22
+ hash = self.rest_client.matching(params)
23
+ self.deserialize(hash) || []
24
+ end
25
+
26
+ def create(auth, params = {})
27
+ self.new(params).save(auth)
28
+ end
29
+
30
+ def delete(obj_id)
31
+ self.find(obj_id).delete
32
+ end
33
+ end
34
+
35
+ module InstanceMethods
36
+ def save(auth)
37
+ begin
38
+ obj_id = self.rest_client.create(auth, self.to_post_data)
39
+ rescue
40
+ return false
41
+ end
42
+ self.find(obj_id)
43
+ end
44
+
45
+ def delete(auth)
46
+ self.rest_client.delete(auth, {:id => self.id})
47
+ end
48
+ alias_method :destroy, :delete
49
+ end
50
+
51
+ end
@@ -0,0 +1,9 @@
1
+ module Smartfm::RestClient; end
2
+
3
+ require 'smartfm/rest_clients/base'
4
+ require 'smartfm/rest_clients/user'
5
+ require 'smartfm/rest_clients/list'
6
+ require 'smartfm/rest_clients/item'
7
+ require 'smartfm/rest_clients/sentence'
8
+ require 'smartfm/rest_clients/like'
9
+ require 'smartfm/rest_clients/notification'
File without changes
@@ -0,0 +1,17 @@
1
+ class Smartfm::RestClient::Item < Smartfm::RestClient::Base
2
+
3
+ ACTIONS = {
4
+ :recent => {:path => '/items' },
5
+ :find => {:path => '/items/__id__' },
6
+ :matching => {:path => '/items/matching/__keyword__'},
7
+ :extract => {:path => '/items/extract', },
8
+ :likes => {:path => '/items/__id__/likes' },
9
+ :create => {:path => '/items', :http_method => :post},
10
+ :add_image => {:path => '/items/__id__/images', :http_method => :post},
11
+ :add_sound => {:path => '/items/__id__/sounds', :http_method => :post},
12
+ :add_tags => {:path => '/items/__id__/tags', :http_method => :post},
13
+ :like! => {:path => '/items/__id__/likes', :http_method => :post},
14
+ :unlike! => {:path => '/items/__id__/likes', :http_method => :delete}
15
+ }
16
+
17
+ end
@@ -0,0 +1,7 @@
1
+ class Smartfm::RestClient::Like < Smartfm::RestClient::Base
2
+
3
+ ACTIONS = {
4
+ :of_current => {:path => '/likes'}
5
+ }
6
+
7
+ end
@@ -0,0 +1,18 @@
1
+ class Smartfm::RestClient::List < Smartfm::RestClient::Base
2
+
3
+ ACTIONS = {
4
+ :recent => {:path => '/lists' },
5
+ :find => {:path => '/lists/__id__' },
6
+ :items => {:path => '/lists/__id__/items' },
7
+ :sentences => {:path => '/lists/__id__/sentences' },
8
+ :matching => {:path => '/lists/matching/__keyword__'},
9
+ :likes => {:path => '/lists/__id__/likes' },
10
+ :create => {:path => '/lists', :http_method => :post},
11
+ :add_item => {:path => '/lists/__id__/items', :http_method => :post},
12
+ :like! => {:path => '/lists/__id__/likes', :http_method => :post},
13
+ :delete => {:path => '/lists/__id__', :http_method => :delete},
14
+ :delete_item => {:path => '/lists/__id__/items/__item_id__', :http_method => :delete},
15
+ :unlike! => {:path => '/lists/__id__/likes', :http_method => :delete}
16
+ }
17
+
18
+ end
@@ -0,0 +1,8 @@
1
+ class Smartfm::RestClient::Notification < Smartfm::RestClient::Base
2
+
3
+ ACTIONS = {
4
+ :of_current => {:path => '/notifications'},
5
+ :create => {:path => '/notifications', :http_method => :post},
6
+ }
7
+
8
+ end
@@ -0,0 +1,14 @@
1
+ class Smartfm::RestClient::Sentence < Smartfm::RestClient::Base
2
+
3
+ ACTIONS = {
4
+ :recent => {:path => '/sentences' },
5
+ :find => {:path => '/sentences/__id__' },
6
+ :matching => {:path => '/sentences/matching/__keyword__'},
7
+ :create => {:path => '/sentences', :http_method => :post},
8
+ :add_image => {:path => '/sentences/__id__/images', :http_method => :post},
9
+ :add_sound => {:path => '/sentences/__id__/sounds', :http_method => :post},
10
+ :like! => {:path => '/sentences/__id__/likes', :http_method => :post},
11
+ :unlike! => {:path => '/sentences/__id__/likes', :http_method => :delete}
12
+ }
13
+
14
+ end
@@ -0,0 +1,20 @@
1
+ class Smartfm::RestClient::User < Smartfm::RestClient::Base
2
+
3
+ ACTIONS = {
4
+ :current => {:path => '/users' },
5
+ :find => {:path => '/users/__username__' },
6
+ :lists => {:path => '/users/__username__/lists' },
7
+ :items => {:path => '/users/__username__/items' },
8
+ :friends => {:path => '/users/__username__/friends' },
9
+ :followers => {:path => '/users/__username__/followers' },
10
+ :friends_of_current => {:path => '/friends' },
11
+ :followers_of_current => {:path => '/followers' },
12
+ :likes => {:path => '/users/__username__/likes' },
13
+ :notifications => {:path => '/users/__username__/notifications'},
14
+ :matching => {:path => '/users/matching/__keyword__' },
15
+ :study_results => {:path => '/users/__username__/study_results/__application__'},
16
+ :follow! => {:path => '/users/__username__/friends', :http_method => :post},
17
+ :unfollow! => {:path => '/users/__username__/friends', :http_method => :delete}
18
+ }
19
+
20
+ end
@@ -10,9 +10,11 @@ subclasses.each do |klass|
10
10
  end
11
11
  end
12
12
 
13
- describe klass, '.find' do
14
- it "should return nil if NOT FOUND" do
15
- klass.find(-1).should be_nil
13
+ if klass.respond_to?(:recent)
14
+ describe klass, '.find' do
15
+ it "should return nil if NOT FOUND" do
16
+ klass.find(-1).should be_nil
17
+ end
16
18
  end
17
19
  end
18
20
 
@@ -2,15 +2,25 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
2
2
 
3
3
  smart = Smartfm::Item.find(33158, :include_sentences => true)
4
4
 
5
- describe Smartfm::Item do
6
- it "should respond to attribute methods" do
7
- Smartfm::Item::ATTRIBUTES.each do |attr|
5
+ Smartfm::Item::ATTRIBUTES.each do |attr|
6
+ describe Smartfm::Item, "##{attr}" do
7
+ it "should be accessible" do
8
8
  smart.should respond_to(attr)
9
9
  end
10
- Smartfm::Item::Response::ATTRIBUTES.each do |attr|
10
+ end
11
+ end
12
+
13
+ Smartfm::Item::Response::ATTRIBUTES.each do |attr|
14
+ describe Smartfm::Item::Response, "##{attr}" do
15
+ it "should be accessible" do
11
16
  smart.responses.first.should respond_to(attr)
12
17
  end
13
- Smartfm::Item::Cue::ATTRIBUTES.each do |attr|
18
+ end
19
+ end
20
+
21
+ Smartfm::Item::Cue::ATTRIBUTES.each do |attr|
22
+ describe Smartfm::Item::Cue, "##{attr}" do
23
+ it "should be accessibles" do
14
24
  smart.cue.should respond_to(attr)
15
25
  end
16
26
  end
@@ -39,3 +49,12 @@ describe Smartfm::Item, '#sentences' do
39
49
  end
40
50
  end
41
51
  end
52
+
53
+ describe Smartfm::Item, '#likes' do
54
+ it "should return a Array of Smartfm::Like" do
55
+ smart.likes.should be_a(Array)
56
+ smart.likes.each do |like|
57
+ like.should be_a(Smartfm::Like)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,19 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
2
+
3
+ matake_likes = Smartfm::User.find('matake').likes
4
+
5
+ Smartfm::Like::ATTRIBUTES.each do |attr|
6
+ describe Smartfm::Like, "##{attr}" do
7
+ it "should be accessible" do
8
+ matake_likes.first.should respond_to(attr)
9
+ end
10
+ end
11
+ end
12
+
13
+ Smartfm::Like::ATTRIBUTES.each do |attr|
14
+ describe Smartfm::Like, "##{attr}" do
15
+ it "should not be nil" do
16
+ matake_likes.first.should_not be_nil
17
+ end
18
+ end
19
+ end