nov-smartfm 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +5 -1
- data/README +3 -10
- data/lib/smartfm.rb +3 -2
- data/lib/smartfm/core/version.rb +1 -1
- data/lib/smartfm/models.rb +7 -0
- data/lib/smartfm/{model → models}/base.rb +2 -1
- data/lib/smartfm/{model → models}/item.rb +18 -86
- data/lib/smartfm/models/like.rb +20 -0
- data/lib/smartfm/{model → models}/list.rb +35 -64
- data/lib/smartfm/models/notification.rb +26 -0
- data/lib/smartfm/models/sentence.rb +70 -0
- data/lib/smartfm/{model → models}/user.rb +54 -16
- data/lib/smartfm/modules.rb +4 -0
- data/lib/smartfm/modules/acts_as_likable.rb +16 -0
- data/lib/smartfm/modules/media_support.rb +37 -0
- data/lib/smartfm/modules/private_content.rb +25 -0
- data/lib/smartfm/modules/public_content.rb +51 -0
- data/lib/smartfm/rest_clients.rb +9 -0
- data/lib/smartfm/{rest_client → rest_clients}/base.rb +0 -0
- data/lib/smartfm/rest_clients/item.rb +17 -0
- data/lib/smartfm/rest_clients/like.rb +7 -0
- data/lib/smartfm/rest_clients/list.rb +18 -0
- data/lib/smartfm/rest_clients/notification.rb +8 -0
- data/lib/smartfm/rest_clients/sentence.rb +14 -0
- data/lib/smartfm/rest_clients/user.rb +20 -0
- data/spec/smartfm/model/base_spec.rb +5 -3
- data/spec/smartfm/model/item_spec.rb +9 -0
- data/spec/smartfm/model/like_spec.rb +11 -0
- data/spec/smartfm/model/list_spec.rb +39 -3
- data/spec/smartfm/model/notification_spec.rb +7 -0
- data/spec/smartfm/model/user_spec.rb +18 -0
- data/spec/smartfm/rest_client/like_spec.rb +7 -0
- data/spec/smartfm/rest_client/notification_spec.rb +7 -0
- metadata +30 -16
- data/lib/smartfm/model.rb +0 -5
- data/lib/smartfm/model/sentence.rb +0 -118
- data/lib/smartfm/rest_client.rb +0 -8
- data/lib/smartfm/rest_client/item.rb +0 -14
- data/lib/smartfm/rest_client/list.rb +0 -15
- data/lib/smartfm/rest_client/sentence.rb +0 -12
- data/lib/smartfm/rest_client/user.rb +0 -14
data/ChangeLog
CHANGED
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
data/lib/smartfm/core/version.rb
CHANGED
@@ -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],
|
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
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
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
|
-
|
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
|
-
|
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
|
-
:
|
5
|
-
|
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
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
46
|
-
|
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 @
|
47
|
+
if @id and @translation_language
|
60
48
|
common_settings = {:list_id => @id, :lang => @translation_language}
|
61
|
-
@iknow = Application.new(common_settings.merge(:
|
62
|
-
@dictation = Application.new(common_settings.merge(:
|
63
|
-
@brainspeed = Application.new(common_settings.merge(:
|
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
|
75
|
-
@sentences
|
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 =
|
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 =
|
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
|
-
|
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
|
-
|
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
|
-
[
|
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
|
@@ -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 =
|
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 =
|
79
|
+
hash = self.rest_client.matching(params)
|
73
80
|
self.deserialize(hash) || []
|
74
81
|
end
|
75
82
|
|
76
|
-
def self.
|
77
|
-
|
83
|
+
def self.current(auth, params = {})
|
84
|
+
self.rest_client.current(auth, params)
|
78
85
|
end
|
79
86
|
|
80
|
-
def
|
81
|
-
|
82
|
-
|
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 =
|
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 =
|
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
|
96
|
-
hash =
|
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
|
101
|
-
hash =
|
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 =
|
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,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,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,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
|
-
|
14
|
-
|
15
|
-
|
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
|
|
@@ -39,3 +39,12 @@ describe Smartfm::Item, '#sentences' do
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
42
|
+
|
43
|
+
describe Smartfm::Item, '#likes' do
|
44
|
+
it "should return a Array of Smartfm::Like" do
|
45
|
+
smart.likes.should be_a(Array)
|
46
|
+
smart.likes.each do |like|
|
47
|
+
like.should be_a(Smartfm::Like)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
|
2
|
+
|
3
|
+
matake_likes = Smartfm::User.find('matake').likes
|
4
|
+
|
5
|
+
describe Smartfm::Like do
|
6
|
+
it "should respond to attribute methods" do
|
7
|
+
Smartfm::Like::ATTRIBUTES.each do |attr|
|
8
|
+
matake_likes.first.should respond_to(attr)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -1,7 +1,43 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
core_2000 = Smartfm::List.find(705)
|
4
|
+
|
5
|
+
describe Smartfm::List do
|
6
|
+
it "should respond to attribute methods" do
|
7
|
+
Smartfm::List::ATTRIBUTES.each do |attr|
|
8
|
+
core_2000.should respond_to(attr)
|
9
|
+
end
|
10
|
+
Smartfm::List::Application::ATTRIBUTES.each do |attr|
|
11
|
+
core_2000.iknow.should respond_to(attr)
|
12
|
+
core_2000.dictation.should respond_to(attr)
|
13
|
+
core_2000.brainspeed.should respond_to(attr)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe Smartfm::List, '#items' do
|
19
|
+
it "should return a Array of Smartfm::Item" do
|
20
|
+
core_2000.items.should be_a(Array)
|
21
|
+
core_2000.items.each do |item|
|
22
|
+
item.should be_a(Smartfm::Item)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe Smartfm::List, '#sentences' do
|
28
|
+
it "should return a Array of Smartfm::Sentence" do
|
29
|
+
core_2000.sentences.should be_a(Array)
|
30
|
+
core_2000.sentences.each do |sentence|
|
31
|
+
sentence.should be_a(Smartfm::Sentence)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe Smartfm::List, '#likes' do
|
37
|
+
it "should return a Array of Smartfm::Like" do
|
38
|
+
core_2000.likes.should be_a(Array)
|
39
|
+
core_2000.likes.each do |like|
|
40
|
+
like.should be_a(Smartfm::Like)
|
41
|
+
end
|
6
42
|
end
|
7
43
|
end
|
@@ -64,6 +64,24 @@ describe Smartfm::User, '#followers' do
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
+
describe Smartfm::User, '#likes' do
|
68
|
+
it "should return a Array of Smartfm::Like" do
|
69
|
+
matake.likes.should be_a(Array)
|
70
|
+
matake.likes.each do |like|
|
71
|
+
like.should be_a(Smartfm::Like)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe Smartfm::User, '#notifications' do
|
77
|
+
it "should return a Array of Smartfm::Notification" do
|
78
|
+
matake.notifications.should be_a(Array)
|
79
|
+
matake.notifications.each do |notification|
|
80
|
+
notification.should be_a(Smartfm::Notification)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
67
85
|
describe Smartfm::User, '#study' do
|
68
86
|
it "should return a instance of Smartfm::User::Study" do
|
69
87
|
matake.study.should be_a(Smartfm::User::Study)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nov-smartfm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nov
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-04-
|
12
|
+
date: 2009-04-30 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -57,13 +57,17 @@ files:
|
|
57
57
|
- spec/smartfm/model
|
58
58
|
- spec/smartfm/model/base_spec.rb
|
59
59
|
- spec/smartfm/model/item_spec.rb
|
60
|
+
- spec/smartfm/model/like_spec.rb
|
60
61
|
- spec/smartfm/model/list_spec.rb
|
62
|
+
- spec/smartfm/model/notification_spec.rb
|
61
63
|
- spec/smartfm/model/sentence_spec.rb
|
62
64
|
- spec/smartfm/model/user_spec.rb
|
63
65
|
- spec/smartfm/rest_client
|
64
66
|
- spec/smartfm/rest_client/base_spec.rb
|
65
67
|
- spec/smartfm/rest_client/item_spec.rb
|
68
|
+
- spec/smartfm/rest_client/like_spec.rb
|
66
69
|
- spec/smartfm/rest_client/list_spec.rb
|
70
|
+
- spec/smartfm/rest_client/notification_spec.rb
|
67
71
|
- spec/smartfm/rest_client/sentence_spec.rb
|
68
72
|
- spec/smartfm/rest_client/user_spec.rb
|
69
73
|
- spec/spec_helper.rb
|
@@ -75,20 +79,30 @@ files:
|
|
75
79
|
- lib/smartfm/core/config.rb
|
76
80
|
- lib/smartfm/core/version.rb
|
77
81
|
- lib/smartfm/core.rb
|
78
|
-
- lib/smartfm/
|
79
|
-
- lib/smartfm/
|
80
|
-
- lib/smartfm/
|
81
|
-
- lib/smartfm/
|
82
|
-
- lib/smartfm/
|
83
|
-
- lib/smartfm/
|
84
|
-
- lib/smartfm/
|
85
|
-
- lib/smartfm/
|
86
|
-
- lib/smartfm/
|
87
|
-
- lib/smartfm/
|
88
|
-
- lib/smartfm/
|
89
|
-
- lib/smartfm/
|
90
|
-
- lib/smartfm/
|
91
|
-
- lib/smartfm/
|
82
|
+
- lib/smartfm/models
|
83
|
+
- lib/smartfm/models/base.rb
|
84
|
+
- lib/smartfm/models/item.rb
|
85
|
+
- lib/smartfm/models/like.rb
|
86
|
+
- lib/smartfm/models/list.rb
|
87
|
+
- lib/smartfm/models/notification.rb
|
88
|
+
- lib/smartfm/models/sentence.rb
|
89
|
+
- lib/smartfm/models/user.rb
|
90
|
+
- lib/smartfm/models.rb
|
91
|
+
- lib/smartfm/modules
|
92
|
+
- lib/smartfm/modules/acts_as_likable.rb
|
93
|
+
- lib/smartfm/modules/media_support.rb
|
94
|
+
- lib/smartfm/modules/private_content.rb
|
95
|
+
- lib/smartfm/modules/public_content.rb
|
96
|
+
- lib/smartfm/modules.rb
|
97
|
+
- lib/smartfm/rest_clients
|
98
|
+
- lib/smartfm/rest_clients/base.rb
|
99
|
+
- lib/smartfm/rest_clients/item.rb
|
100
|
+
- lib/smartfm/rest_clients/like.rb
|
101
|
+
- lib/smartfm/rest_clients/list.rb
|
102
|
+
- lib/smartfm/rest_clients/notification.rb
|
103
|
+
- lib/smartfm/rest_clients/sentence.rb
|
104
|
+
- lib/smartfm/rest_clients/user.rb
|
105
|
+
- lib/smartfm/rest_clients.rb
|
92
106
|
- lib/smartfm.rb
|
93
107
|
- examples/pure_ruby.rb
|
94
108
|
has_rdoc: true
|
data/lib/smartfm/model.rb
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
class Smartfm::Sentence < Smartfm::Base
|
2
|
-
ATTRIBUTES = [:sound, :image, :square_image, :text, :language, :id, :transliterations, :translations, :item, :list]
|
3
|
-
READONLY_ATTRIBUTES = [:id]
|
4
|
-
attr_accessor *(ATTRIBUTES - READONLY_ATTRIBUTES)
|
5
|
-
attr_reader *READONLY_ATTRIBUTES
|
6
|
-
|
7
|
-
def self.recent(params = {})
|
8
|
-
hash = Smartfm::RestClient::Sentence.recent(params)
|
9
|
-
self.deserialize(hash) || []
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.find(sentence_id, params = {})
|
13
|
-
params[:id] = sentence_id
|
14
|
-
hash = Smartfm::RestClient::Sentence.find(params)
|
15
|
-
self.deserialize(hash)
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.matching(keyword, params = {})
|
19
|
-
params[:keyword] = keyword
|
20
|
-
hash = Smartfm::RestClient::Sentence.matching(params)
|
21
|
-
self.deserialize(hash) || []
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.create(auth, params = {})
|
25
|
-
self.new(params).save(auth)
|
26
|
-
end
|
27
|
-
|
28
|
-
def initialize(params = {})
|
29
|
-
params[:translations] = [params[:translation]] if params[:translation]
|
30
|
-
params[:transliterations] = [params[:transliteration]] if params[:transliteration]
|
31
|
-
@id = params[:id]
|
32
|
-
@item = params[:item]
|
33
|
-
@list = params[:list]
|
34
|
-
@sound = params[:sound]
|
35
|
-
@image = params[:image]
|
36
|
-
@square_image = params[:square_image]
|
37
|
-
@text = params[:text]
|
38
|
-
@language = params[:language]
|
39
|
-
@transliterations = params[:transliterations]
|
40
|
-
@translations = self.deserialize(params[:translations], :as => Smartfm::Sentence)
|
41
|
-
end
|
42
|
-
|
43
|
-
def save(auth)
|
44
|
-
begin
|
45
|
-
sentence_id = Smartfm::RestClient::Sentence.create(auth, self.to_post_data)
|
46
|
-
rescue
|
47
|
-
return false
|
48
|
-
end
|
49
|
-
Smartfm::Sentence.find(sentence_id)
|
50
|
-
end
|
51
|
-
|
52
|
-
def add_image(auth, params)
|
53
|
-
post_params = if params.is_a?(String)
|
54
|
-
{'image[url]' => params}
|
55
|
-
else
|
56
|
-
{'image[url]' => params[:url], 'image[list_id]' => params[:list_id]}.merge(attribution_params(params[:attribution]))
|
57
|
-
end
|
58
|
-
Smartfm::RestClient::Sentence.add_image(auth, post_params.merge(:id => self.id))
|
59
|
-
end
|
60
|
-
|
61
|
-
def add_sound(auth, params)
|
62
|
-
post_params = if params.is_a?(String)
|
63
|
-
{'sound[url]' => params}
|
64
|
-
else
|
65
|
-
{'sound[url]' => params[:url], 'sound[list_id]' => params[:list_id]}.merge(attribution_params(params[:attribution]))
|
66
|
-
end
|
67
|
-
Smartfm::RestClient::Sentence.add_sound(auth, post_params.merge(:id => self.id))
|
68
|
-
end
|
69
|
-
|
70
|
-
protected
|
71
|
-
|
72
|
-
def to_post_data
|
73
|
-
self.validate
|
74
|
-
post_data = {
|
75
|
-
'item_id' => self.item.id,
|
76
|
-
'sentence[text]' => self.text
|
77
|
-
}
|
78
|
-
# Optional attributes
|
79
|
-
if self.list
|
80
|
-
post_data['sentence[list_id]'] = self.list.id
|
81
|
-
end
|
82
|
-
[:language, :transliteration].each do |key|
|
83
|
-
if self.send("#{key}")
|
84
|
-
post_data["sentence[#{key}]"] = self.send("#{key}")
|
85
|
-
end
|
86
|
-
end
|
87
|
-
if self.translation
|
88
|
-
[:text, :language, :transliteration].each do |key|
|
89
|
-
if self.translation.send("#{key}")
|
90
|
-
post_data["translation[#{key}]"] = self.translation.send("#{key}")
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
post_data
|
95
|
-
end
|
96
|
-
|
97
|
-
def validate
|
98
|
-
raise ArgumentError.new("Item is required.") unless self.item
|
99
|
-
raise ArgumentError.new("Sentence text is required.") if self.text.nil? or self.text.empty?
|
100
|
-
end
|
101
|
-
|
102
|
-
def translation
|
103
|
-
self.translations.first rescue nil
|
104
|
-
end
|
105
|
-
|
106
|
-
def transliteration
|
107
|
-
self.transliterations.first rescue nil
|
108
|
-
end
|
109
|
-
|
110
|
-
def attribution_params(attr_params)
|
111
|
-
return {} unless attr_params
|
112
|
-
{'attribution[medias_entity]' => attr_params[:media_entity],
|
113
|
-
'attribution[author]' => attr_params[:author],
|
114
|
-
'attribution[author_url]' => attr_params[:author_url],
|
115
|
-
'attributions[attribution_license_id]' => attr_params[:attribution_license_id] }
|
116
|
-
end
|
117
|
-
|
118
|
-
end
|
data/lib/smartfm/rest_client.rb
DELETED
@@ -1,14 +0,0 @@
|
|
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
|
-
:create => { :path => '/items', :http_method => :post },
|
9
|
-
:add_image => { :path => '/items/__id__/images', :http_method => :post },
|
10
|
-
:add_sound => { :path => '/items/__id__/sounds', :http_method => :post },
|
11
|
-
:add_tags => { :path => '/items/__id__/tags', :http_method => :post }
|
12
|
-
}
|
13
|
-
|
14
|
-
end
|
@@ -1,15 +0,0 @@
|
|
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
|
-
:create => { :path => '/lists', :http_method => :post },
|
10
|
-
:delete => { :path => '/lists/__id__', :http_method => :delete },
|
11
|
-
:add_item => { :path => '/lists/__list_id__/items', :http_method => :post },
|
12
|
-
:delete_item => { :path => '/lists/__list_id__/items/__id__', :http_method => :delete }
|
13
|
-
}
|
14
|
-
|
15
|
-
end
|
@@ -1,12 +0,0 @@
|
|
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
|
-
}
|
11
|
-
|
12
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
class Smartfm::RestClient::User < Smartfm::RestClient::Base
|
2
|
-
|
3
|
-
ACTIONS = {
|
4
|
-
:find => { :path => '/users/__username__' },
|
5
|
-
:lists => { :path => '/users/__username__/lists' },
|
6
|
-
:items => { :path => '/users/__username__/items' },
|
7
|
-
:friends => { :path => '/users/__username__/friends' },
|
8
|
-
:followers => { :path => '/users/__username__/followers' },
|
9
|
-
:study_results => { :path => '/users/__username__/study_results/__application__' },
|
10
|
-
:matching => { :path => '/users/matching/__keyword__' },
|
11
|
-
:username => { :path => '/sessions' }
|
12
|
-
}
|
13
|
-
|
14
|
-
end
|