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
data/ChangeLog CHANGED
@@ -39,9 +39,13 @@
39
39
 
40
40
  == 0.3.1
41
41
 
42
- * add sessions API support
42
+ * add sessions API calls
43
43
  * fixed extract API bugs
44
44
 
45
45
  == 0.4.0
46
46
 
47
- * OAuth DELETE calls works fine now.
47
+ * OAuth DELETE calls works fine now.
48
+
49
+ == 1.0.0
50
+
51
+ * add friends, followers, likes and notifications API calls
data/README CHANGED
@@ -7,8 +7,8 @@
7
7
  This rubygem is a wrapper of smart.fm API.
8
8
  You can get pure-ruby example at examples/pure_ruby.rb.
9
9
  http://github.com/nov/smartfm/tree/master/examples/pure_ruby.rb
10
-
11
- It shows all API calls you can use with this gem.
10
+
11
+ It shows almost all API calls you can use with this gem.
12
12
 
13
13
  == Installation
14
14
 
@@ -26,15 +26,8 @@
26
26
 
27
27
  == Features/Problems
28
28
 
29
- Test! Test!! Test!!!
30
-
31
- Create/Add/Delete APIs are not implemented.
32
- They will be implemented in a few weeks.
33
-
34
- smart.fm OAuth is still pre-alpha.
35
-
36
29
  == Synopsis
37
-
30
+
38
31
  See examples and smart.fm Developers, please.
39
32
  smart.fm Developers (http://developer.smart.fm)
40
33
 
data/lib/smartfm.rb CHANGED
@@ -10,7 +10,8 @@ require 'json'
10
10
 
11
11
  require 'ext/hash'
12
12
  require 'smartfm/core'
13
- require 'smartfm/rest_client'
14
- require 'smartfm/model'
13
+ require 'smartfm/rest_clients'
14
+ require 'smartfm/modules'
15
+ require 'smartfm/models'
15
16
 
16
17
  Smartfm::Config.init
@@ -1,6 +1,6 @@
1
1
  module Smartfm::Version
2
- MAJOR = 0
3
- MINOR = 4
2
+ MAJOR = 1
3
+ MINOR = 0
4
4
  REVISION = 0
5
5
  class << self
6
6
  def to_version
@@ -0,0 +1,7 @@
1
+ require 'smartfm/models/base'
2
+ require 'smartfm/models/user'
3
+ require 'smartfm/models/list'
4
+ require 'smartfm/models/item'
5
+ require 'smartfm/models/sentence'
6
+ require 'smartfm/models/like'
7
+ require 'smartfm/models/notification'
@@ -1,9 +1,10 @@
1
1
  class Smartfm::Base
2
2
 
3
3
  def self.attributes; self::ATTRIBUTES end
4
-
5
4
  def attributes; self.class.attributes end
6
5
 
6
+ protected
7
+
7
8
  def self.deserialize(hash, params = {})
8
9
  return nil if hash.nil?
9
10
 
@@ -1,9 +1,16 @@
1
1
  class Smartfm::Item < Smartfm::Base
2
- ATTRIBUTES = [:sentences, :responses, :cue, :id, :list]
3
- READONLY_ATTRIBUTES = [:sentences, :responses, :cue, :id]
2
+ ATTRIBUTES = [:sentences, :responses, :cue, :id, :list, :user]
3
+ READONLY_ATTRIBUTES = [:sentences, :responses, :cue, :id, :user]
4
4
  attr_accessor *(ATTRIBUTES - READONLY_ATTRIBUTES)
5
5
  attr_reader *READONLY_ATTRIBUTES
6
6
 
7
+ include Smartfm::PublicContent
8
+ include Smartfm::MediaSupport
9
+ include Smartfm::ActsAsLikable
10
+
11
+ def self.rest_client; Smartfm::RestClient::Item; end
12
+ def rest_client; self.class.rest_client; end
13
+
7
14
  class Response < Smartfm::Base
8
15
  ATTRIBUTES = [:text, :text_with_character, :type, :language]
9
16
  READONLY_ATTRIBUTES = [:type]
@@ -34,99 +41,24 @@ class Smartfm::Item < Smartfm::Base
34
41
  end
35
42
  end
36
43
 
37
- def self.recent(params = {})
38
- hash = Smartfm::RestClient::Item.recent(params)
39
- self.deserialize(hash) || []
40
- end
41
-
42
- def self.find(item_id, params = {})
43
- params[:id] = item_id
44
- hash = Smartfm::RestClient::Item.find(params)
45
- self.deserialize(hash)
46
- end
47
-
48
- def self.matching(keyword, params = {})
49
- params[:keyword] = keyword
50
- hash = Smartfm::RestClient::Item.matching(params)
51
- self.deserialize(hash) || []
52
- end
53
-
54
- def self.extract(text, params = {})
55
- params[:text] = text
56
- hash = Smartfm::RestClient::Item.extract(params)
57
- if params[:words_only] == false
58
- self.deserialize(hash) || []
59
- else
60
- hash
61
- end
62
- end
63
-
64
- def self.create(auth, params = {})
65
- self.new(params).save(auth)
66
- end
67
-
68
44
  def initialize(params = {})
69
45
  params[:responses] = [params[:response]] if params[:response]
70
46
  @id = params[:id].to_i
71
47
  @list = params[:list]
72
- @cue = self.deserialize(params[:cue], :as => Smartfm::Item::Cue)
48
+ @cue = self.deserialize(params[:cue], :as => Smartfm::Item::Cue)
73
49
  @responses = self.deserialize(params[:responses], :as => Smartfm::Item::Response)
74
50
  @sentences = self.deserialize(params[:sentences], :as => Smartfm::Sentence)
51
+ @user = self.deserialize(params[:user], :as => Smartfm::User)
75
52
  end
76
53
 
77
- def save(auth)
78
- begin
79
- item_id = Smartfm::RestClient::Item.create(auth, self.to_post_data)
80
- rescue
81
- return false
82
- end
83
- Smartfm::Item.find(item_id)
84
- end
85
-
86
- def add_image(auth, params)
87
- post_params = if params.is_a?(String)
88
- { 'image[url]' => params }
89
- else
90
- image_params = {
91
- 'image[url]' => params[:url],
92
- 'image[list_id]' => params[:list_id]
93
- }
94
- if params[:attribution]
95
- attribution_params = {
96
- 'attribution[media_entity]' => params[:attribution][:media_entity],
97
- 'attribution[author]' => params[:attribution][:media_entity],
98
- 'attribution[author_url]' => params[:attribution][:media_entity],
99
- 'attribution[attribution_license_id]' => params[:attribution][:media_entity]
100
- }
101
- image_params.merge(attribution_params)
102
- else
103
- image_params
104
- end
105
- end
106
- Smartfm::RestClient::Item.add_image(auth, post_params.merge(:id => self.id))
107
- end
108
-
109
- def add_sound(auth, params)
110
- post_params = if params.is_a?(String)
111
- { 'sound[url]' => params }
54
+ def self.extract(text, params = {})
55
+ params[:text] = text
56
+ hash = self.rest_client.extract(params)
57
+ if params[:words_only] == false
58
+ self.deserialize(hash) || []
112
59
  else
113
- sound_params = {
114
- 'sound[url]' => params[:url],
115
- 'sound[list_id]' => params[:list_id]
116
- }
117
- if params[:attribution]
118
- attribution_params = {
119
- 'attribution[media_entity]' => params[:attribution][:media_entity],
120
- 'attribution[author]' => params[:attribution][:media_entity],
121
- 'attribution[author_url]' => params[:attribution][:media_entity],
122
- 'attribution[attribution_license_id]' => params[:attribution][:media_entity]
123
- }
124
- sound_params.merge(attribution_params)
125
- else
126
- sound_params
127
- end
60
+ hash
128
61
  end
129
- Smartfm::RestClient::Item.add_sound(auth, post_params.merge(:id => self.id))
130
62
  end
131
63
 
132
64
  def add_tags(auth, *tags)
@@ -139,7 +71,7 @@ class Smartfm::Item < Smartfm::Base
139
71
  post_params["semantic_tags[#{idx}][disambiguation]"] = tag[:disambiguation]
140
72
  end
141
73
  end
142
- Smartfm::RestClient::Item.add_tags(auth, post_params.merge(:id => self.id))
74
+ self.rest_client.add_tags(auth, post_params.merge(:id => self.id))
143
75
  end
144
76
 
145
77
  protected
@@ -0,0 +1,20 @@
1
+ class Smartfm::Like < Smartfm::Base
2
+ ATTRIBUTES = [:id, :type, :title, :description, :href, :favorite, :user]
3
+ attr_reader *ATTRIBUTES
4
+
5
+ include Smartfm::PrivateContent
6
+
7
+ def self.rest_client; Smartfm::RestClient::Like; end
8
+ def rest_client; self.class.rest_client; end
9
+
10
+ def initialize(params)
11
+ @id = params[:id]
12
+ @type = params[:user]
13
+ @title = params[:title]
14
+ @description = params[:description]
15
+ @href = params[:href]
16
+ @favorite = params[:favorite]
17
+ @user = self.deserialize(params[:user], :as => Smartfm::User)
18
+ end
19
+
20
+ end
@@ -1,49 +1,37 @@
1
1
  class Smartfm::List < Smartfm::Base
2
2
  ATTRIBUTES = [:id, :title, :description, :icon, :square_icon, :item_count, :user_count, :iknow, :dictation, :brainspeed,
3
- :language, :translation_language, :list_type, :transcript, :embed,
4
- :tags, :media_entry, :author, :author_id, :author_url, :attribution_license_id,
5
- :items, :sentences]
6
- READONLY_ATTRIBUTES = [:id, :icon, :item_count, :user_count, :iknow, :dictation, :brainspeed]
3
+ :language, :translation_language, :list_type, :transcript, :embed, :tags, :media_entry,
4
+ :attribution_license_id, :items, :sentences, :user]
5
+ READONLY_ATTRIBUTES = [:id, :icon, :item_count, :user_count, :iknow, :dictation, :brainspeed, :user]
7
6
  attr_accessor *(ATTRIBUTES - READONLY_ATTRIBUTES)
8
7
  attr_reader *READONLY_ATTRIBUTES
9
8
 
9
+ include Smartfm::PublicContent
10
+ include Smartfm::ActsAsLikable
11
+
12
+ def self.rest_client; Smartfm::RestClient::List; end
13
+ def rest_client; self.class.rest_client; end
14
+
10
15
  class Application < Smartfm::Base
11
- ATTRIBUTES = [:application, :list_id, :lang]
16
+ ATTRIBUTES = [:application, :available, :progress, :list_id, :lang]
12
17
  attr_reader *ATTRIBUTES
13
18
 
14
19
  def initialize(params = {})
15
- @application = params[:application]
16
- @list_id = params[:list_id]
17
- @lang = params[:lang]
18
- end
19
- def url
20
- "http://smart.fm/flash?swf=#{self.name}&course_id=#{self.list_id}&lang=#{self.lang}"
20
+ @application = case
21
+ when params[:iknow] then :iknow
22
+ when params[:dictation] then :dictation
23
+ when params[:brainspeed] then :brainspeed
24
+ end
25
+ @available = params[self.application][:available]
26
+ @progress = params[self.application][:progress]
27
+ @href = params[self.application][:href]
28
+ @list_id = params[:list_id]
29
+ @lang = params[:lang]
21
30
  end
22
- end
23
-
24
- def self.recent(params = {})
25
- hash = Smartfm::RestClient::List.recent(params)
26
- self.deserialize(hash) || []
27
- end
28
-
29
- def self.find(list_id, params = {})
30
- params[:id] = list_id
31
- hash = Smartfm::RestClient::List.find(params)
32
- self.deserialize(hash)
33
- end
34
-
35
- def self.matching(keyword, params = {})
36
- params[:keyword] = keyword
37
- hash = Smartfm::RestClient::List.matching(params)
38
- self.deserialize(hash) || []
39
- end
40
-
41
- def self.create(auth, params = {})
42
- self.new(params).save(auth)
43
- end
44
31
 
45
- def self.delete(list_id)
46
- self.find(list_id).delete
32
+ def available?
33
+ self.available
34
+ end
47
35
  end
48
36
 
49
37
  def initialize(params = {})
@@ -56,55 +44,39 @@ class Smartfm::List < Smartfm::Base
56
44
  @user_count = (params[:user_count].to_i rescue nil)
57
45
  @language = params[:language]
58
46
  @translation_language = params[:translation_language]
59
- if @list_id and @translation_language
47
+ if @id and @translation_language
60
48
  common_settings = {:list_id => @id, :lang => @translation_language}
61
- @iknow = Application.new(common_settings.merge(:application => 'iknow')) if params[:iknow]
62
- @dictation = Application.new(common_settings.merge(:application => 'dictation')) if params[:dictation]
63
- @brainspeed = Application.new(common_settings.merge(:application => 'brainspeed')) if params[:brainspeed]
49
+ @iknow = Application.new(common_settings.merge(:iknow => params[:iknow]))
50
+ @dictation = Application.new(common_settings.merge(:dictation => params[:dictation]))
51
+ @brainspeed = Application.new(common_settings.merge(:brainspeed => params[:brainspeed]))
64
52
  end
65
- @author = params[:author] # display_name or username
66
- @author_id = params[:author_id] # username
67
- @author_url = params[:author_url]
68
53
  @list_type = params[:list_type] # for list creation
69
54
  @transcript = params[:transcript] # for list creation
70
55
  @embed = params[:embed] # for list creation
71
56
  @tags = params[:tags] # for list creation
72
57
  @media_entry = params[:media_entry] # for list creation
73
58
  @attribution_license_id = params[:attribution_license_id] # for list creation
74
- @items = self.deserialize(params[:items], :as => Smartfm::Item)
75
- @sentences = self.deserialize(params[:sentences], :as => Smartfm::Sentence)
59
+ @items = self.deserialize(params[:items], :as => Smartfm::Item)
60
+ @sentences = self.deserialize(params[:sentences], :as => Smartfm::Sentence)
61
+ @user = self.deserialize(params[:user], :as => Smartfm::User)
76
62
  end
77
63
 
78
64
  def items(params = {})
79
- hash = Smartfm::RestClient::List.items(params.merge(:id => self.id))
65
+ hash = self.rest_client.items(params.merge(:id => self.id))
80
66
  self.deserialize(hash, :as => Smartfm::Item) || []
81
67
  end
82
68
 
83
69
  def sentences(params = {})
84
- hash = Smartfm::RestClient::List.sentences(params.merge(:id => self.id))
70
+ hash = self.rest_client.sentences(params.merge(:id => self.id))
85
71
  self.deserialize(hash, :as => Smartfm::Sentence) || []
86
72
  end
87
73
 
88
- def save(auth)
89
- begin
90
- list_id = Smartfm::RestClient::List.create(auth, self.to_post_data)
91
- rescue
92
- return false
93
- end
94
- Smartfm::List.find(list_id)
95
- end
96
-
97
- def delete(auth)
98
- Smartfm::RestClient::List.delete(auth, {:id => self.id})
99
- end
100
- alias_method :destroy, :delete
101
-
102
74
  def add_item(auth, item)
103
- Smartfm::RestClient::List.add_item(auth, {:list_id => self.id, :id => item.id})
75
+ self.rest_client.add_item(auth, {:id => self.id, :item_id => item.id})
104
76
  end
105
77
 
106
78
  def delete_item(auth, item)
107
- Smartfm::RestClient::List.delete_item(auth, {:list_id => self.id, :id => item.id})
79
+ self.rest_client.delete_item(auth, {:id => self.id, :item_id => item.id})
108
80
  end
109
81
 
110
82
  protected
@@ -121,8 +93,7 @@ class Smartfm::List < Smartfm::Base
121
93
  if self.list_type
122
94
  post_data['list[type]'] = self.list_type
123
95
  end
124
- [ :transcript, :embed, :tags, :media_entry,
125
- :author, :author_url, :attribution_license_id ].each do |key|
96
+ [:transcript, :embed, :tags, :media_entry, :author, :author_url, :attribution_license_id ].each do |key|
126
97
  if self.send("#{key}")
127
98
  post_data["list[#{key}]"] = self.send("#{key}")
128
99
  end
@@ -0,0 +1,26 @@
1
+ class Smartfm::Notification < Smartfm::Base
2
+ ATTRIBUTES = [:type, :message, :recipient, :sender, :context]
3
+ READONLY_ATTRIBUTES = [:type, :recipient, :sender, :context]
4
+ attr_accessor *(ATTRIBUTES - READONLY_ATTRIBUTES)
5
+ attr_reader *READONLY_ATTRIBUTES
6
+
7
+ include Smartfm::PrivateContent
8
+
9
+ def self.rest_client; Smartfm::RestClient::Notification; end
10
+ def rest_client; self.class.rest_client; end
11
+
12
+ def initialize(params)
13
+ @type = params[:type]
14
+ @message = params[:message]
15
+ @recipient = params[:recipient]
16
+ @sender = params[:sender]
17
+ @context = params[:context]
18
+ end
19
+
20
+ private
21
+
22
+ def to_post_data
23
+ {:message => self.message}
24
+ end
25
+
26
+ end
@@ -0,0 +1,70 @@
1
+ class Smartfm::Sentence < Smartfm::Base
2
+ ATTRIBUTES = [:sound, :image, :square_image, :text, :language, :id, :transliterations, :translations, :item, :list, :user]
3
+ READONLY_ATTRIBUTES = [:id, :user]
4
+ attr_accessor *(ATTRIBUTES - READONLY_ATTRIBUTES)
5
+ attr_reader *READONLY_ATTRIBUTES
6
+
7
+ include Smartfm::PublicContent
8
+ include Smartfm::MediaSupport
9
+ include Smartfm::ActsAsLikable
10
+
11
+ def self.rest_client; Smartfm::RestClient::Sentence; end
12
+ def rest_client; self.class.rest_client; end
13
+
14
+ def initialize(params = {})
15
+ params[:translations] = [params[:translation]] if params[:translation]
16
+ params[:transliterations] = [params[:transliteration]] if params[:transliteration]
17
+ @id = params[:id]
18
+ @item = params[:item]
19
+ @list = params[:list]
20
+ @sound = params[:sound]
21
+ @image = params[:image]
22
+ @square_image = params[:square_image]
23
+ @text = params[:text]
24
+ @language = params[:language]
25
+ @transliterations = params[:transliterations]
26
+ @translations = self.deserialize(params[:translations], :as => Smartfm::Sentence)
27
+ @user = self.deserialize(params[:user], :as => Smartfm::User)
28
+ end
29
+
30
+ protected
31
+
32
+ def to_post_data
33
+ self.validate
34
+ post_data = {
35
+ 'item_id' => self.item.id,
36
+ 'sentence[text]' => self.text
37
+ }
38
+ # Optional attributes
39
+ if self.list
40
+ post_data['sentence[list_id]'] = self.list.id
41
+ end
42
+ [:language, :transliteration].each do |key|
43
+ if self.send("#{key}")
44
+ post_data["sentence[#{key}]"] = self.send("#{key}")
45
+ end
46
+ end
47
+ if self.translation
48
+ [:text, :language, :transliteration].each do |key|
49
+ if self.translation.send("#{key}")
50
+ post_data["translation[#{key}]"] = self.translation.send("#{key}")
51
+ end
52
+ end
53
+ end
54
+ post_data
55
+ end
56
+
57
+ def validate
58
+ raise ArgumentError.new("Item is required.") unless self.item
59
+ raise ArgumentError.new("Sentence text is required.") if self.text.nil? or self.text.empty?
60
+ end
61
+
62
+ def translation
63
+ self.translations.first rescue nil
64
+ end
65
+
66
+ def transliteration
67
+ self.transliterations.first rescue nil
68
+ end
69
+
70
+ end