Permalink
Browse files

likes calls first commit

fixed some bugs
refactored some models
  • Loading branch information...
1 parent 9b8c5af commit a307a4336b72b135f446c3a3ae99333d6a97be0f @nov committed Apr 30, 2009
View
@@ -2,4 +2,5 @@
require 'smartfm/model/user'
require 'smartfm/model/list'
require 'smartfm/model/item'
-require 'smartfm/model/sentence'
+require 'smartfm/model/sentence'
+require 'smartfm/model/like'
View
@@ -23,4 +23,14 @@ def deserialize(hash, params = {})
self.class.deserialize(hash, params)
end
+ def attribution_params(attr_params)
+ return {} unless attr_params
+ {
+ 'attribution[medias_entity]' => attr_params[:media_entity],
+ 'attribution[author]' => attr_params[:author],
+ 'attribution[author_url]' => attr_params[:author_url],
+ 'attributions[attribution_license_id]' => attr_params[:attribution_license_id]
+ }
+ end
+
end
View
@@ -1,6 +1,6 @@
class Smartfm::Item < Smartfm::Base
- ATTRIBUTES = [:sentences, :responses, :cue, :id, :list]
- READONLY_ATTRIBUTES = [:sentences, :responses, :cue, :id]
+ ATTRIBUTES = [:sentences, :responses, :cue, :id, :list, :user]
+ READONLY_ATTRIBUTES = [:sentences, :responses, :cue, :id, :user]
attr_accessor *(ATTRIBUTES - READONLY_ATTRIBUTES)
attr_reader *READONLY_ATTRIBUTES
@@ -72,6 +72,12 @@ def initialize(params = {})
@cue = self.deserialize(params[:cue], :as => Smartfm::Item::Cue)
@responses = self.deserialize(params[:responses], :as => Smartfm::Item::Response)
@sentences = self.deserialize(params[:sentences], :as => Smartfm::Sentence)
+ @user = self.deserialize(params[:user], :as => Smartfm::User)
+ end
+
+ def likes(params = {})
+ hash = Smartfm::RestClient::Item.likes(params.merge(:id => self.id))
+ self.deserialize(hash, :as => Smartfm::Like) || []
end
def save(auth)
@@ -85,46 +91,18 @@ def save(auth)
def add_image(auth, params)
post_params = if params.is_a?(String)
- { 'image[url]' => params }
+ {'image[url]' => params}
else
- image_params = {
- 'image[url]' => params[:url],
- 'image[list_id]' => params[:list_id]
- }
- if params[:attribution]
- attribution_params = {
- 'attribution[media_entity]' => params[:attribution][:media_entity],
- 'attribution[author]' => params[:attribution][:media_entity],
- 'attribution[author_url]' => params[:attribution][:media_entity],
- 'attribution[attribution_license_id]' => params[:attribution][:media_entity]
- }
- image_params.merge(attribution_params)
- else
- image_params
- end
+ {'image[url]' => params[:url], 'image[list_id]' => params[:list_id]}.merge(attribution_params(params[:attribution]))
end
Smartfm::RestClient::Item.add_image(auth, post_params.merge(:id => self.id))
end
def add_sound(auth, params)
post_params = if params.is_a?(String)
- { 'sound[url]' => params }
+ {'sound[url]' => params}
else
- sound_params = {
- 'sound[url]' => params[:url],
- 'sound[list_id]' => params[:list_id]
- }
- if params[:attribution]
- attribution_params = {
- 'attribution[media_entity]' => params[:attribution][:media_entity],
- 'attribution[author]' => params[:attribution][:media_entity],
- 'attribution[author_url]' => params[:attribution][:media_entity],
- 'attribution[attribution_license_id]' => params[:attribution][:media_entity]
- }
- sound_params.merge(attribution_params)
- else
- sound_params
- end
+ {'sound[url]' => params[:url], 'sound[list_id]' => params[:list_id]}.merge(attribution_params(params[:attribution]))
end
Smartfm::RestClient::Item.add_sound(auth, post_params.merge(:id => self.id))
end
@@ -142,6 +120,14 @@ def add_tags(auth, *tags)
Smartfm::RestClient::Item.add_tags(auth, post_params.merge(:id => self.id))
end
+ def like!(auth, params)
+ Smartfm::RestClient::Item.like!(auth, params.merge(:id => self.id))
+ end
+
+ def unlike!(auth, params)
+ Smartfm::RestClient::Item.unlike!(auth, params.merge(:id => self.id))
+ end
+
protected
def to_post_data
View
@@ -1,9 +1,8 @@
class Smartfm::List < Smartfm::Base
ATTRIBUTES = [:id, :title, :description, :icon, :square_icon, :item_count, :user_count, :iknow, :dictation, :brainspeed,
- :language, :translation_language, :list_type, :transcript, :embed,
- :tags, :media_entry, :author, :author_id, :author_url, :attribution_license_id,
- :items, :sentences]
- READONLY_ATTRIBUTES = [:id, :icon, :item_count, :user_count, :iknow, :dictation, :brainspeed]
+ :language, :translation_language, :list_type, :transcript, :embed, :tags, :media_entry,
+ :attribution_license_id, :items, :sentences, :user]
+ READONLY_ATTRIBUTES = [:id, :icon, :item_count, :user_count, :iknow, :dictation, :brainspeed, :user]
attr_accessor *(ATTRIBUTES - READONLY_ATTRIBUTES)
attr_reader *READONLY_ATTRIBUTES
@@ -62,17 +61,15 @@ def initialize(params = {})
@dictation = Application.new(common_settings.merge(:application => 'dictation')) if params[:dictation]
@brainspeed = Application.new(common_settings.merge(:application => 'brainspeed')) if params[:brainspeed]
end
- @author = params[:author] # display_name or username
- @author_id = params[:author_id] # username
- @author_url = params[:author_url]
@list_type = params[:list_type] # for list creation
@transcript = params[:transcript] # for list creation
@embed = params[:embed] # for list creation
@tags = params[:tags] # for list creation
@media_entry = params[:media_entry] # for list creation
@attribution_license_id = params[:attribution_license_id] # for list creation
- @items = self.deserialize(params[:items], :as => Smartfm::Item)
- @sentences = self.deserialize(params[:sentences], :as => Smartfm::Sentence)
+ @items = self.deserialize(params[:items], :as => Smartfm::Item)
+ @sentences = self.deserialize(params[:sentences], :as => Smartfm::Sentence)
+ @user = self.deserialize(params[:user], :as => Smartfm::User)
end
def items(params = {})
@@ -85,6 +82,11 @@ def sentences(params = {})
self.deserialize(hash, :as => Smartfm::Sentence) || []
end
+ def likes(params = {})
+ hash = Smartfm::RestClient::List.likes(params.merge(:id => self.id))
+ self.deserialize(hash, :as => Smartfm::Like) || []
+ end
+
def save(auth)
begin
list_id = Smartfm::RestClient::List.create(auth, self.to_post_data)
@@ -100,11 +102,19 @@ def delete(auth)
alias_method :destroy, :delete
def add_item(auth, item)
- Smartfm::RestClient::List.add_item(auth, {:list_id => self.id, :id => item.id})
+ Smartfm::RestClient::List.add_item(auth, {:id => self.id, :item_id => item.id})
end
def delete_item(auth, item)
- Smartfm::RestClient::List.delete_item(auth, {:list_id => self.id, :id => item.id})
+ Smartfm::RestClient::List.delete_item(auth, {:id => self.id, :item_id => item.id})
+ end
+
+ def like!(auth, params)
+ Smartfm::RestClient::List.like!(auth, params.merge(:id => self.id))
+ end
+
+ def unlike!(auth, params)
+ Smartfm::RestClient::List.unlike!(auth, params.merge(:id => self.id))
end
protected
@@ -121,8 +131,7 @@ def to_post_data
if self.list_type
post_data['list[type]'] = self.list_type
end
- [ :transcript, :embed, :tags, :media_entry,
- :author, :author_url, :attribution_license_id ].each do |key|
+ [:transcript, :embed, :tags, :media_entry, :author, :author_url, :attribution_license_id ].each do |key|
if self.send("#{key}")
post_data["list[#{key}]"] = self.send("#{key}")
end
@@ -1,6 +1,6 @@
class Smartfm::Sentence < Smartfm::Base
- ATTRIBUTES = [:sound, :image, :square_image, :text, :language, :id, :transliterations, :translations, :item, :list]
- READONLY_ATTRIBUTES = [:id]
+ ATTRIBUTES = [:sound, :image, :square_image, :text, :language, :id, :transliterations, :translations, :item, :list, :user]
+ READONLY_ATTRIBUTES = [:id, :user]
attr_accessor *(ATTRIBUTES - READONLY_ATTRIBUTES)
attr_reader *READONLY_ATTRIBUTES
@@ -38,6 +38,12 @@ def initialize(params = {})
@language = params[:language]
@transliterations = params[:transliterations]
@translations = self.deserialize(params[:translations], :as => Smartfm::Sentence)
+ @user = self.deserialize(params[:user], :as => Smartfm::User)
+ end
+
+ def likes(params = {})
+ hash = Smartfm::RestClient::Sentence.likes(params.merge(:id => self.id))
+ self.deserialize(hash, :as => Smartfm::Like) || []
end
def save(auth)
@@ -67,6 +73,14 @@ def add_sound(auth, params)
Smartfm::RestClient::Sentence.add_sound(auth, post_params.merge(:id => self.id))
end
+ def like!(auth, params)
+ Smartfm::RestClient::Sentence.like!(auth, params.merge(:id => self.id))
+ end
+
+ def unlike!(auth, params)
+ Smartfm::RestClient::Sentence.unlike!(auth, params.merge(:id => self.id))
+ end
+
protected
def to_post_data
@@ -107,12 +121,4 @@ def transliteration
self.transliterations.first rescue nil
end
- def attribution_params(attr_params)
- return {} unless attr_params
- {'attribution[medias_entity]' => attr_params[:media_entity],
- 'attribution[author]' => attr_params[:author],
- 'attribution[author_url]' => attr_params[:author_url],
- 'attributions[attribution_license_id]' => attr_params[:attribution_license_id] }
- end
-
end
View
@@ -73,8 +73,8 @@ def self.matching(keyword, params = {})
self.deserialize(hash) || []
end
- def self.username(auth)
- Smartfm::RestClient::User.username(auth, {})
+ def self.logging_in(auth)
+ Smartfm::RestClient::User.logging_in(auth, {})
end
def initialize(params)
@@ -92,6 +92,11 @@ def lists(params = {})
self.deserialize(hash, :as => Smartfm::List) || []
end
+ def likes(params = {})
+ hash = Smartfm::RestClient::User.likes(params.merge(:username => self.username))
+ self.deserialize(hash, :as => Smartfm::Like) || []
+ end
+
def friends(params = {})
hash = Smartfm::RestClient::User.friends(params.merge(:username => self.username))
self.deserialize(hash) || []
@@ -109,4 +114,12 @@ def study(params = {})
self.deserialize(hash, :as => Smartfm::User::Study)
end
+ def follow!(auth, params = {})
+ Smartfm::RestClient::User.follow!(auth, params.merge(:username => self.username))
+ end
+
+ def unfollow!(auth, params = {})
+ Smartfm::RestClient::User.unfollow!(auth, params.merge(:username => self.username))
+ end
+
end
@@ -1,14 +1,17 @@
class Smartfm::RestClient::Item < Smartfm::RestClient::Base
ACTIONS = {
- :recent => { :path => '/items' },
- :find => { :path => '/items/__id__' },
- :matching => { :path => '/items/matching/__keyword__' },
- :extract => { :path => '/items/extract', },
- :create => { :path => '/items', :http_method => :post },
- :add_image => { :path => '/items/__id__/images', :http_method => :post },
- :add_sound => { :path => '/items/__id__/sounds', :http_method => :post },
- :add_tags => { :path => '/items/__id__/tags', :http_method => :post }
+ :recent => {:path => '/items' },
+ :find => {:path => '/items/__id__' },
+ :matching => {:path => '/items/matching/__keyword__'},
+ :extract => {:path => '/items/extract', },
+ :likes => {:path => '/items/__id__/likes' },
+ :create => {:path => '/items', :http_method => :post},
+ :add_image => {:path => '/items/__id__/images', :http_method => :post},
+ :add_sound => {:path => '/items/__id__/sounds', :http_method => :post},
+ :add_tags => {:path => '/items/__id__/tags', :http_method => :post},
+ :like! => {:path => '/items/__id__/likes', :http_method => :post},
+ :unlike! => {:path => '/items/__id__/likes', :http_method => :delete}
}
end
@@ -1,15 +1,18 @@
class Smartfm::RestClient::List < Smartfm::RestClient::Base
ACTIONS = {
- :recent => { :path => '/lists' },
- :find => { :path => '/lists/__id__' },
- :items => { :path => '/lists/__id__/items' },
- :sentences => { :path => '/lists/__id__/sentences' },
- :matching => { :path => '/lists/matching/__keyword__' },
- :create => { :path => '/lists', :http_method => :post },
- :delete => { :path => '/lists/__id__', :http_method => :delete },
- :add_item => { :path => '/lists/__list_id__/items', :http_method => :post },
- :delete_item => { :path => '/lists/__list_id__/items/__id__', :http_method => :delete }
+ :recent => {:path => '/lists' },
+ :find => {:path => '/lists/__id__' },
+ :items => {:path => '/lists/__id__/items' },
+ :sentences => {:path => '/lists/__id__/sentences' },
+ :matching => {:path => '/lists/matching/__keyword__'},
+ :likes => {:path => '/lists/__id__/likes' },
+ :create => {:path => '/lists', :http_method => :post},
+ :add_item => {:path => '/lists/__id__/items', :http_method => :post},
+ :like! => {:path => '/lists/__id__/likes', :http_method => :post},
+ :delete => {:path => '/lists/__id__', :http_method => :delete},
+ :delete_item => {:path => '/lists/__id__/items/__item_id__', :http_method => :delete},
+ :unlike! => {:path => '/lists/__id__/likes', :http_method => :delete}
}
end
@@ -1,12 +1,14 @@
class Smartfm::RestClient::Sentence < Smartfm::RestClient::Base
ACTIONS = {
- :recent => { :path => '/sentences' },
- :find => { :path => '/sentences/__id__' },
- :matching => { :path => '/sentences/matching/__keyword__' },
- :create => { :path => '/sentences', :http_method => :post },
- :add_image => { :path => '/sentences/__id__/images', :http_method => :post },
- :add_sound => { :path => '/sentences/__id__/sounds', :http_method => :post }
- }
+ :recent => {:path => '/sentences' },
+ :find => {:path => '/sentences/__id__' },
+ :matching => {:path => '/sentences/matching/__keyword__'},
+ :create => {:path => '/sentences', :http_method => :post},
+ :add_image => {:path => '/sentences/__id__/images', :http_method => :post},
+ :add_sound => {:path => '/sentences/__id__/sounds', :http_method => :post},
+ :like! => {:path => '/sentences/__id__/likes', :http_method => :post},
+ :unlike! => {:path => '/sentences/__id__/likes', :http_method => :delete}
+ }
end
@@ -1,14 +1,17 @@
class Smartfm::RestClient::User < Smartfm::RestClient::Base
ACTIONS = {
- :find => { :path => '/users/__username__' },
- :lists => { :path => '/users/__username__/lists' },
- :items => { :path => '/users/__username__/items' },
- :friends => { :path => '/users/__username__/friends' },
- :followers => { :path => '/users/__username__/followers' },
- :study_results => { :path => '/users/__username__/study_results/__application__' },
- :matching => { :path => '/users/matching/__keyword__' },
- :username => { :path => '/sessions' }
- }
+ :find => {:path => '/users/__username__' },
+ :lists => {:path => '/users/__username__/lists' },
+ :items => {:path => '/users/__username__/items' },
+ :friends => {:path => '/users/__username__/friends' },
+ :followers => {:path => '/users/__username__/followers'},
+ :likes => {:path => '/users/__username__/likes' },
+ :study_results => {:path => '/users/__username__/study_results/__application__'},
+ :matching => {:path => '/users/matching/__keyword__' },
+ :logging_in => {:path => '/users' },
+ :follow! => {:path => '/users/__username__/friends', :http_method => :post},
+ :unfollow! => {:path => '/users/__username__/friends', :http_method => :delete}
+ }
end

0 comments on commit a307a43

Please sign in to comment.