iknow 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ChangeLog +12 -0
- data/README +64 -0
- data/Rakefile +147 -0
- data/examples/iknow_on_rails/app/controllers/application.rb +15 -0
- data/examples/iknow_on_rails/app/controllers/iknow_oauth_controller.rb +24 -0
- data/examples/iknow_on_rails/app/controllers/users_controller.rb +10 -0
- data/examples/iknow_on_rails/app/helpers/application_helper.rb +3 -0
- data/examples/iknow_on_rails/app/helpers/iknow_oauth_helper.rb +3 -0
- data/examples/iknow_on_rails/app/helpers/users_helper.rb +2 -0
- data/examples/iknow_on_rails/app/models/iknow_oauth_token.rb +37 -0
- data/examples/iknow_on_rails/config/boot.rb +109 -0
- data/examples/iknow_on_rails/config/environment.rb +76 -0
- data/examples/iknow_on_rails/config/environments/development.rb +17 -0
- data/examples/iknow_on_rails/config/environments/production.rb +22 -0
- data/examples/iknow_on_rails/config/environments/test.rb +22 -0
- data/examples/iknow_on_rails/config/initializers/inflections.rb +10 -0
- data/examples/iknow_on_rails/config/initializers/mime_types.rb +5 -0
- data/examples/iknow_on_rails/config/initializers/new_rails_defaults.rb +17 -0
- data/examples/iknow_on_rails/config/routes.rb +44 -0
- data/examples/iknow_on_rails/db/migrate/20081017012212_create_iknow_oauth_tokens.rb +15 -0
- data/examples/iknow_on_rails/db/schema.rb +23 -0
- data/examples/iknow_on_rails/lib/iknow_oauth_system.rb +26 -0
- data/examples/iknow_on_rails/public/dispatch.rb +10 -0
- data/examples/iknow_on_rails/test/functional/iknow_oauth_controller_test.rb +7 -0
- data/examples/iknow_on_rails/test/functional/users_controller_test.rb +8 -0
- data/examples/iknow_on_rails/test/test_helper.rb +38 -0
- data/examples/iknow_on_rails/test/unit/iknow_oauth_token_test.rb +7 -0
- data/examples/pure_ruby.rb +54 -0
- data/generators/iknow_oauth/USAGE +12 -0
- data/generators/iknow_oauth/iknow_oauth_generator.rb +22 -0
- data/generators/iknow_oauth/templates/iknow_oauth_controller.rb +24 -0
- data/generators/iknow_oauth/templates/iknow_oauth_controller_test.rb +7 -0
- data/generators/iknow_oauth/templates/iknow_oauth_helper.rb +3 -0
- data/generators/iknow_oauth/templates/iknow_oauth_system.rb +26 -0
- data/generators/iknow_oauth/templates/iknow_oauth_token.rb +37 -0
- data/generators/iknow_oauth/templates/iknow_oauth_token_test.rb +7 -0
- data/generators/iknow_oauth/templates/iknow_oauth_tokens_migration.rb +15 -0
- data/generators/iknow_oauth/templates/index.rhtml +4 -0
- data/lib/ext/hash.rb +23 -0
- data/lib/iknow/core/config.rb +44 -0
- data/lib/iknow/core/version.rb +14 -0
- data/lib/iknow/core.rb +2 -0
- data/lib/iknow/model/base.rb +29 -0
- data/lib/iknow/model/item.rb +60 -0
- data/lib/iknow/model/list.rb +133 -0
- data/lib/iknow/model/sentence.rb +34 -0
- data/lib/iknow/model/user.rb +102 -0
- data/lib/iknow/model.rb +5 -0
- data/lib/iknow/rest_client/base.rb +114 -0
- data/lib/iknow/rest_client/item.rb +12 -0
- data/lib/iknow/rest_client/list.rb +13 -0
- data/lib/iknow/rest_client/sentence.rb +12 -0
- data/lib/iknow/rest_client/user.rb +12 -0
- data/lib/iknow/rest_client.rb +8 -0
- data/lib/iknow.rb +14 -0
- data/test/iknow_test.rb +5 -0
- data/test/test_helper.rb +3 -0
- metadata +157 -0
@@ -0,0 +1,23 @@
|
|
1
|
+
# This file is auto-generated from the current state of the database. Instead of editing this file,
|
2
|
+
# please use the migrations feature of Active Record to incrementally modify your database, and
|
3
|
+
# then regenerate this schema definition.
|
4
|
+
#
|
5
|
+
# Note that this schema.rb definition is the authoritative source for your database schema. If you need
|
6
|
+
# to create the application database on another system, you should be using db:schema:load, not running
|
7
|
+
# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
8
|
+
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
9
|
+
#
|
10
|
+
# It's strongly recommended to check this file into your version control system.
|
11
|
+
|
12
|
+
ActiveRecord::Schema.define(:version => 20081016145649) do
|
13
|
+
|
14
|
+
create_table "iknow_oauth_tokens", :force => true do |t|
|
15
|
+
t.string "iknow_username", :null => false
|
16
|
+
t.string "token"
|
17
|
+
t.string "secret"
|
18
|
+
end
|
19
|
+
|
20
|
+
add_index "iknow_oauth_tokens", ["iknow_username"], :name => "index_iknow_oauth_tokens_on_iknow_username"
|
21
|
+
add_index "iknow_oauth_tokens", ["token"], :name => "index_iknow_oauth_tokens_on_token", :unique => true
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module IknowOauthSystem
|
2
|
+
|
3
|
+
protected
|
4
|
+
|
5
|
+
def require_iknow_oauth
|
6
|
+
unless iknow_username_exists?
|
7
|
+
redirect_to(:controller => 'iknow_oauth', :action => 'index')
|
8
|
+
return false
|
9
|
+
end
|
10
|
+
|
11
|
+
iknow_oauth_token = IknowOauthToken.find_by_username(session[:iknow_username])
|
12
|
+
unless iknow_oauth_token
|
13
|
+
redirect_to(:controller => 'iknow_oauth', :action => 'new_request')
|
14
|
+
return false
|
15
|
+
end
|
16
|
+
|
17
|
+
true
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def iknow_username_exists?
|
23
|
+
!session[:iknow_username].blank?
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/local/ruby//bin/ruby
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
|
4
|
+
|
5
|
+
# If you're using RubyGems and mod_ruby, this require should be changed to an absolute path one, like:
|
6
|
+
# "/usr/local/lib/ruby/gems/1.8/gems/rails-0.8.0/lib/dispatcher" -- otherwise performance is severely impaired
|
7
|
+
require "dispatcher"
|
8
|
+
|
9
|
+
ADDITIONAL_LOAD_PATHS.reverse.each { |dir| $:.unshift(dir) if File.directory?(dir) } if defined?(Apache::RubyRun)
|
10
|
+
Dispatcher.dispatch
|
@@ -0,0 +1,38 @@
|
|
1
|
+
ENV["RAILS_ENV"] = "test"
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
|
3
|
+
require 'test_help'
|
4
|
+
|
5
|
+
class Test::Unit::TestCase
|
6
|
+
# Transactional fixtures accelerate your tests by wrapping each test method
|
7
|
+
# in a transaction that's rolled back on completion. This ensures that the
|
8
|
+
# test database remains unchanged so your fixtures don't have to be reloaded
|
9
|
+
# between every test method. Fewer database queries means faster tests.
|
10
|
+
#
|
11
|
+
# Read Mike Clark's excellent walkthrough at
|
12
|
+
# http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
|
13
|
+
#
|
14
|
+
# Every Active Record database supports transactions except MyISAM tables
|
15
|
+
# in MySQL. Turn off transactional fixtures in this case; however, if you
|
16
|
+
# don't care one way or the other, switching from MyISAM to InnoDB tables
|
17
|
+
# is recommended.
|
18
|
+
#
|
19
|
+
# The only drawback to using transactional fixtures is when you actually
|
20
|
+
# need to test transactions. Since your test is bracketed by a transaction,
|
21
|
+
# any transactions started in your code will be automatically rolled back.
|
22
|
+
self.use_transactional_fixtures = true
|
23
|
+
|
24
|
+
# Instantiated fixtures are slow, but give you @david where otherwise you
|
25
|
+
# would need people(:david). If you don't want to migrate your existing
|
26
|
+
# test cases which use the @david style and don't mind the speed hit (each
|
27
|
+
# instantiated fixtures translates to a database query per test method),
|
28
|
+
# then set this back to true.
|
29
|
+
self.use_instantiated_fixtures = false
|
30
|
+
|
31
|
+
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
|
32
|
+
#
|
33
|
+
# Note: You'll currently still have to declare fixtures explicitly in integration tests
|
34
|
+
# -- they do not yet inherit this setting
|
35
|
+
fixtures :all
|
36
|
+
|
37
|
+
# Add more helper methods to be used by all tests here...
|
38
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'iknow'
|
3
|
+
|
4
|
+
Iknow::Config.init do |conf|
|
5
|
+
conf.api_key = 'SET_YOUR_API_KEY'
|
6
|
+
conf.oauth_consumer_key = 'SET_YOUR_OAUTH_CONSUMER_KEY'
|
7
|
+
conf.oauth_consumer_secret = 'SET_YOUR_OAUTH_CONSUMER_SECRET'
|
8
|
+
end
|
9
|
+
|
10
|
+
please_get_api_key =<<EOS
|
11
|
+
This example needs your own iKnow! API key.
|
12
|
+
(for only Iknow::Item.extract example)
|
13
|
+
|
14
|
+
You can get iKnow! API key at iKnow! Developers.
|
15
|
+
iKnow! Developers (http://developer.iknow.co.jp/)
|
16
|
+
|
17
|
+
Thanks!
|
18
|
+
EOS
|
19
|
+
|
20
|
+
if Iknow::Config.api_key == ''# or
|
21
|
+
# Iknow::Config.oauth_consumer_key == '' or
|
22
|
+
# Iknow::Config.oauth_consumer_secret == ''
|
23
|
+
raise ArgumentError.new(please_get_api_key)
|
24
|
+
end
|
25
|
+
|
26
|
+
# ## User API
|
27
|
+
# @user = Iknow::User.find('kirk')
|
28
|
+
# @user.items(:include_sentences => true)
|
29
|
+
# @user.lists
|
30
|
+
# @user.friends
|
31
|
+
# @user.study.results
|
32
|
+
# @user.study.total_results
|
33
|
+
# @matched_users = Iknow::User.matching('matake')
|
34
|
+
#
|
35
|
+
# ## List API
|
36
|
+
# @recent_lists = Iknow::List.recent
|
37
|
+
# @list = Iknow::List.find(31509, :include_sentences => true, :include_items => true)
|
38
|
+
# @list.items
|
39
|
+
# @list.sentences
|
40
|
+
# @matched_lists = Iknow::List.matching("イタリア語であいさつ")
|
41
|
+
#
|
42
|
+
puts Iknow::List.find(31509, :include_sentences => true, :include_items => true).inspect
|
43
|
+
#
|
44
|
+
# ## Item API
|
45
|
+
# @recent_items = Iknow::Item.recent(:include_sentences => true)
|
46
|
+
# @item = Iknow::Item.find(437525)
|
47
|
+
# @matched_items = Iknow::Item.matching('record', :include_sentences => true)
|
48
|
+
# @items = Iknow::Item.extract("sometimes, often, electrical")
|
49
|
+
# @items.first.sentences
|
50
|
+
#
|
51
|
+
# ## Sentence API
|
52
|
+
# @recent_sentences = Iknow::Sentence.recent
|
53
|
+
# @sentence = Iknow::Sentence.find(312271)
|
54
|
+
# @matched_sentences = Iknow::Sentence.matching('record')
|
@@ -0,0 +1,12 @@
|
|
1
|
+
NAME
|
2
|
+
iknow_oauth - creates a functional iknow oauth consumer
|
3
|
+
|
4
|
+
SYNOPSIS
|
5
|
+
nothing
|
6
|
+
|
7
|
+
Description:
|
8
|
+
Generates a model, controller, helper, view and library for making a OAuth consumer of iKnow! API.
|
9
|
+
No arguments are needed.
|
10
|
+
|
11
|
+
Example:
|
12
|
+
./script/generate iknow_oauth
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rails_generator/base'
|
2
|
+
|
3
|
+
class IknowOauthGenerator < Rails::Generator::Base
|
4
|
+
def manifest
|
5
|
+
record do |m|
|
6
|
+
m.class_collisions 'IknowOauthController', 'IknowOauthControllerTest', 'IknowOauthHelper',
|
7
|
+
'IknowOauthSystem', 'IknowOauthToken', 'IknowOauthTokenTest'
|
8
|
+
|
9
|
+
m.migration_template 'iknow_oauth_tokens_migration.rb', 'db/migrate', :migration_file_name => 'create_iknow_oauth_tokens'
|
10
|
+
|
11
|
+
m.template 'iknow_oauth_controller.rb', File.join('app/controllers', 'iknow_oauth_controller.rb' )
|
12
|
+
m.template 'iknow_oauth_controller_test.rb', File.join('test/functional', 'iknow_oauth_controller_test.rb')
|
13
|
+
m.template 'iknow_oauth_helper.rb', File.join('app/helpers', 'iknow_oauth_helper.rb' )
|
14
|
+
m.template 'iknow_oauth_system.rb', File.join('lib', 'iknow_oauth_system.rb' )
|
15
|
+
m.template 'iknow_oauth_token.rb', File.join('app/models', 'iknow_oauth_token.rb' )
|
16
|
+
m.template 'iknow_oauth_token_test.rb', File.join('test/unit', 'iknow_oauth_token_test.rb' )
|
17
|
+
|
18
|
+
m.directory File.join('app/views', 'iknow_oauth')
|
19
|
+
m.template 'index.rhtml', File.join('app/views', 'iknow_oauth', 'index.rhtml')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class IknowOauthController < ApplicationController
|
2
|
+
|
3
|
+
def index; end
|
4
|
+
|
5
|
+
def new_request
|
6
|
+
request_token = IknowOauthToken.new_request_token
|
7
|
+
|
8
|
+
session[:iknow_username] = params[:iknow_username]
|
9
|
+
session[:request_token] = request_token
|
10
|
+
session[:redirect_url_after_auth] = params[:redirect_url_after_auth] || request.referer
|
11
|
+
|
12
|
+
if request_token
|
13
|
+
redirect_to(request_token.authorize_url)
|
14
|
+
else
|
15
|
+
raise RuntimeError.new("Failed to get a iKnow! Request Token")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def callback
|
20
|
+
AuthToken.establish_auth_token(session[:iknow_username], session[:request_token])
|
21
|
+
redirect_to(session[:redirect_url_after_auth])
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module IknowOauthSystem
|
2
|
+
|
3
|
+
protected
|
4
|
+
|
5
|
+
def require_iknow_oauth
|
6
|
+
unless iknow_username_exists?
|
7
|
+
redirect_to(:controller => 'iknow_oauth', :action => 'index')
|
8
|
+
return false
|
9
|
+
end
|
10
|
+
|
11
|
+
iknow_oauth_token = IknowOauthToken.find_by_username(session[:iknow_username])
|
12
|
+
unless iknow_oauth_token
|
13
|
+
redirect_to(:controller => 'iknow_oauth', :action => 'new_request')
|
14
|
+
return false
|
15
|
+
end
|
16
|
+
|
17
|
+
true
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def iknow_username_exists?
|
23
|
+
!session[:iknow_username].blank?
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'oauth/consumer'
|
2
|
+
|
3
|
+
class IknowOauthToken < ActiveRecord::Base
|
4
|
+
|
5
|
+
def self.consumer
|
6
|
+
@@consumer ||= OAuth::Consumer.new(
|
7
|
+
Iknow::Config.oauth_consumer_key,
|
8
|
+
Iknow::Config.oauth_consumer_secret,
|
9
|
+
:site => Iknow::Config.iknow_api_base_url,
|
10
|
+
:authorize_url => "#{Iknow::Config.iknow_base_url}/oauth/authorize"
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.new_request_token
|
15
|
+
begin
|
16
|
+
self.consumer.get_request_token
|
17
|
+
rescue Exception => e
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.establish_auth_token(iknow_username, request_token)
|
23
|
+
access_token = request_token.get_access_token
|
24
|
+
|
25
|
+
auth_token = IknowAuthToken.new
|
26
|
+
auth_token.username = iknow_username
|
27
|
+
auth_token.token = access_token.token
|
28
|
+
auth_token.secret = access_token.secret
|
29
|
+
auth_token.save!
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_access_token
|
33
|
+
OAuth::AccessToken.new(self.class.consumer, self.token, self.secret)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class CreateIknowOauthTokens < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :iknow_oauth_tokens do |t|
|
4
|
+
t.column :iknow_username, :string, :null => false
|
5
|
+
t.column :token, :string, :unique => true
|
6
|
+
t.column :secret, :string
|
7
|
+
end
|
8
|
+
add_index :iknow_oauth_tokens, :token, :unique => true
|
9
|
+
add_index :iknow_oauth_tokens, :iknow_username
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.down
|
13
|
+
drop_table :iknow_oauth_tokens
|
14
|
+
end
|
15
|
+
end
|
data/lib/ext/hash.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
class Hash
|
2
|
+
def to_http_str
|
3
|
+
result = ''
|
4
|
+
return result if self.empty?
|
5
|
+
self.each do |key, val|
|
6
|
+
result << "#{key}=#{URI.encode(val.to_s)}&"
|
7
|
+
end
|
8
|
+
result.chop
|
9
|
+
end
|
10
|
+
def symbolize_keys!
|
11
|
+
self.each do |key, value|
|
12
|
+
unless self.delete(key.to_s).nil?
|
13
|
+
if value.is_a?(Hash)
|
14
|
+
value.symbolize_keys!
|
15
|
+
elsif value.is_a?(Array)
|
16
|
+
value.map!{ |v| v.symbolize_keys! if v.is_a?(Hash) }
|
17
|
+
end
|
18
|
+
self[key.to_sym] = value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
self
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
class Iknow::Config
|
4
|
+
include Singleton
|
5
|
+
attr_accessor :protocol, :host, :port, :api_protocol, :api_host, :api_port,
|
6
|
+
:api_key, :oauth_consumer_key, :oauth_consumer_secret,
|
7
|
+
:user_agent, :application_name, :application_version, :application_url, :source
|
8
|
+
|
9
|
+
def self.init(&block)
|
10
|
+
conf = Iknow::Config.instance
|
11
|
+
{ :protocol => 'http',
|
12
|
+
:host => 'www.iknow.co.jp',
|
13
|
+
:port => 80,
|
14
|
+
:api_protocol => 'http',
|
15
|
+
:api_host => 'api.iknow.co.jp',
|
16
|
+
:api_port => 80,
|
17
|
+
:api_key => '',
|
18
|
+
:oauth_consumer_key => '',
|
19
|
+
:oauth_consumer_secret => '',
|
20
|
+
:user_agent => 'default',
|
21
|
+
:application_name => 'A rubygem for iKnow! API',
|
22
|
+
:application_version => Iknow::Version.to_version,
|
23
|
+
:application_url => 'http://github.com/nov/iknow_oauth_generator',
|
24
|
+
:source => 'iknow'
|
25
|
+
}.each do |key, value| conf.send("#{key}=", value) end
|
26
|
+
yield conf if block_given?
|
27
|
+
conf
|
28
|
+
end
|
29
|
+
|
30
|
+
def iknow_base_url
|
31
|
+
port = self.port==80 ? nil : ":#{self.port}"
|
32
|
+
"#{self.protocol}://#{self.host}#{port}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def iknow_api_base_url
|
36
|
+
port = self.api_port==80 ? nil : ":#{self.api_port}"
|
37
|
+
"#{self.api_protocol}://#{self.api_host}#{port}"
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.method_missing(method, *args)
|
41
|
+
self.instance.send(method, *args)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/lib/iknow/core.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
class Iknow::Base
|
2
|
+
|
3
|
+
def self.attributes; self::ATTRIBUTES end
|
4
|
+
|
5
|
+
def attributes; self.class.attributes end
|
6
|
+
|
7
|
+
def self.deserialize(response, params = {})
|
8
|
+
return nil if response.nil? or
|
9
|
+
(response.is_a?(Hash) and
|
10
|
+
!response['error'].nil? and
|
11
|
+
response['error']['code'].to_i == 404)
|
12
|
+
|
13
|
+
klass = params[:as] ? params[:as] : self
|
14
|
+
if response.is_a?(Array)
|
15
|
+
response.inject([]) { |results, hash|
|
16
|
+
hash.symbolize_keys!
|
17
|
+
results << klass.new(hash)
|
18
|
+
}
|
19
|
+
else
|
20
|
+
response.symbolize_keys!
|
21
|
+
klass.new(response)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def deserialize(response, params = {})
|
26
|
+
self.class.deserialize(response, params)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
class Iknow::Item < Iknow::Base
|
2
|
+
ATTRIBUTES = [:sentences, :response, :cue, :id]
|
3
|
+
attr_reader *ATTRIBUTES
|
4
|
+
|
5
|
+
class Response
|
6
|
+
ATTRIBUTES = [:text, :type, :language]
|
7
|
+
attr_reader *ATTRIBUTES
|
8
|
+
|
9
|
+
def initialize(params = {})
|
10
|
+
@text = params[:text]
|
11
|
+
@type = params[:type]
|
12
|
+
@language = params[:language]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Cue
|
17
|
+
ATTRIBUTES = [:text, :sound, :part_of_speech, :language]
|
18
|
+
NOT_WRITABLE_ATTRIBUTES = [:text]
|
19
|
+
attr_accessor *(ATTRIBUTES - NOT_WRITABLE_ATTRIBUTES)
|
20
|
+
attr_reader *NOT_WRITABLE_ATTRIBUTES
|
21
|
+
|
22
|
+
def initialize(params = {})
|
23
|
+
@text = params[:text]
|
24
|
+
@sound = params[:sound]
|
25
|
+
@part_of_speech = params[:part_of_speech]
|
26
|
+
@language = params[:language]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.recent(params = {})
|
31
|
+
response = Iknow::RestClient::Item.recent(params)
|
32
|
+
self.deserialize(response) || []
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.find(item_id, params = {})
|
36
|
+
params[:id] = item_id
|
37
|
+
response = Iknow::RestClient::Item.find(params)
|
38
|
+
self.deserialize(response)
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.matching(keyword, params = {})
|
42
|
+
params[:keyword] = keyword
|
43
|
+
response = Iknow::RestClient::Item.matching(params)
|
44
|
+
self.deserialize(response) || []
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.extract(text, params = {})
|
48
|
+
params[:text] = text
|
49
|
+
response = Iknow::RestClient::Item.extract(params)
|
50
|
+
self.deserialize(response) || []
|
51
|
+
end
|
52
|
+
|
53
|
+
def initialize(params = {})
|
54
|
+
@id = params[:id].to_i
|
55
|
+
@cue = self.deserialize(params[:cue], :as => Iknow::Item::Cue)
|
56
|
+
@responses = self.deserialize(params[:responses], :as => Iknow::Item::Response)
|
57
|
+
@sentences = self.deserialize(params[:sentences], :as => Iknow::Sentence)
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
# "iknow": true,
|
2
|
+
# "description": "hogehoge",
|
3
|
+
# "translation_language": "ja",
|
4
|
+
# "dictation": true,
|
5
|
+
# "icon": "http://www.iknow.co.jp/assets/courses/m4.jpg",
|
6
|
+
# "brainspeed": true,
|
7
|
+
# "language": "en",
|
8
|
+
# "item_count": 117,
|
9
|
+
# "author_id": "Cerego",
|
10
|
+
# "user_count": 45056,
|
11
|
+
# "author": "Team iKnow!",
|
12
|
+
# "title": "\u65c5\u306b\u51fa\u3088\u3046\uff01\uff08\u51fa\u56fd\uff06\u8857\u3092\u6b69\u304f\uff09",
|
13
|
+
# "id": 708,
|
14
|
+
# "author_url": "http://www.iknow.co.jp/user/Cerego"
|
15
|
+
|
16
|
+
class Iknow::List < Iknow::Base
|
17
|
+
ATTRIBUTES = [:list_id, :title, :description, :icon, :item_count, :user_count, :iknow, :dictation, :brainspeed,
|
18
|
+
:language, :translation_language, :list_type, :transcript, :embed,
|
19
|
+
:tags, :media_entry, :author, :author_id, :author_url, :attribution_license_id,
|
20
|
+
:items, :sentences]
|
21
|
+
NOT_WRITABLE_ATTRIBUTES = [:list_id, :icon, :item_count, :user_count, :iknow, :dictation, :brainspeed]
|
22
|
+
attr_accessor *(ATTRIBUTES - NOT_WRITABLE_ATTRIBUTES)
|
23
|
+
attr_reader *NOT_WRITABLE_ATTRIBUTES
|
24
|
+
|
25
|
+
class Application
|
26
|
+
attr_reader :application, :list_id, :lang
|
27
|
+
def initialize(params = {})
|
28
|
+
@application = params[:application]
|
29
|
+
@list_id = params[:list_id]
|
30
|
+
@lang = params[:lang]
|
31
|
+
end
|
32
|
+
def url
|
33
|
+
"http://www.iknow.co.jp/flash?swf=#{self.name}&course_id=#{self.list_id}&lang=#{self.lang}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.recent(params = {})
|
38
|
+
response = Iknow::RestClient::List.recent(params)
|
39
|
+
self.deserialize(response) || []
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.find(list_id, params = {})
|
43
|
+
params[:id] = list_id
|
44
|
+
response = Iknow::RestClient::List.find(params)
|
45
|
+
self.deserialize(response)
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.matching(keyword, params = {})
|
49
|
+
params[:keyword] = keyword
|
50
|
+
response = Iknow::RestClient::List.matching(params)
|
51
|
+
self.deserialize(response) || []
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.create(params = {})
|
55
|
+
new_list = self.new(params)
|
56
|
+
new_list.save!
|
57
|
+
end
|
58
|
+
|
59
|
+
def initialize(params = {})
|
60
|
+
@list_id = (params[:id].to_i rescue nil)
|
61
|
+
@title = params[:title]
|
62
|
+
@description = params[:description]
|
63
|
+
@icon = params[:icon]
|
64
|
+
@item_count = (params[:item_count].to_i rescue nil)
|
65
|
+
@user_count = (params[:user_count].to_i rescue nil)
|
66
|
+
@language = params[:language]
|
67
|
+
@translation_language = params[:translation_language]
|
68
|
+
if @list_id and @translation_language
|
69
|
+
common_settings = {:list_id => @list_id, :lang => @translation_language}
|
70
|
+
@iknow = Application.new(common_settings.merge(:application => 'iknow')) if params[:iknow]
|
71
|
+
@dictation = Application.new(common_settings.merge(:application => 'dictation')) if params[:dictation]
|
72
|
+
@brainspeed = Application.new(common_settings.merge(:application => 'brainspeed')) if params[:brainspeed]
|
73
|
+
end
|
74
|
+
@author = params[:author] # display_name or username
|
75
|
+
@author_id = params[:author_id] # username
|
76
|
+
@author_url = params[:author_url]
|
77
|
+
@list_type = params[:list_type] # for list creation
|
78
|
+
@transcript = params[:transcript] # for list creation
|
79
|
+
@embed = params[:embed] # for list creation
|
80
|
+
@tags = params[:tags] # for list creation
|
81
|
+
@media_entry = params[:media_entry] # for list creation
|
82
|
+
@attribution_license_id = params[:attribution_license_id] # for list creation
|
83
|
+
@items = self.deserialize(params[:items], :as => Iknow::Item)
|
84
|
+
@sentences = self.deserialize(params[:sentences], :as => Iknow::Sentence)
|
85
|
+
end
|
86
|
+
|
87
|
+
def items(params = {})
|
88
|
+
response = Iknow::RestClient::List.items(params.merge(:id => self.list_id))
|
89
|
+
self.deserialize(response, :as => Iknow::Item) || []
|
90
|
+
end
|
91
|
+
|
92
|
+
def sentences(params = {})
|
93
|
+
response = Iknow::RestClient::List.sentences(params.merge(:id => self.list_id))
|
94
|
+
self.deserialize(response, :as => Iknow::Sentence) || []
|
95
|
+
end
|
96
|
+
|
97
|
+
def save!
|
98
|
+
Iknow::RestClient::List.create(self.to_post_data)
|
99
|
+
end
|
100
|
+
|
101
|
+
def save
|
102
|
+
self.save!
|
103
|
+
true
|
104
|
+
rescue
|
105
|
+
false
|
106
|
+
end
|
107
|
+
|
108
|
+
protected
|
109
|
+
|
110
|
+
def to_post_data
|
111
|
+
raise ArgumentError.new("List title is needed.") if self.title.nil? or self.title.empty?
|
112
|
+
|
113
|
+
post_data = {
|
114
|
+
'list[name]' => self.title,
|
115
|
+
'list[description]' => self.description,
|
116
|
+
'list[language]' => self.language || 'en',
|
117
|
+
'list[translation_language]' => self.translation_language || 'ja'
|
118
|
+
}
|
119
|
+
# Object#type should not be used
|
120
|
+
if self.list_type
|
121
|
+
post_data['list[type]'] = self.list_type
|
122
|
+
end
|
123
|
+
# Optional attributes
|
124
|
+
[ :transcript, :embed, :tags, :media_entry,
|
125
|
+
:author, :author_url, :attribution_license_id ].each do |key|
|
126
|
+
if self.send("#{key}")
|
127
|
+
post_data["list[#{key}]"] = self.send("#{key}")
|
128
|
+
end
|
129
|
+
end
|
130
|
+
post_data
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|