nov-iknow 0.1.1 → 0.2.0

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.
@@ -7,6 +7,7 @@ Iknow::Config.init do |conf|
7
7
  conf.api_key = '' # 'SET_YOUR_API_KEY'
8
8
  conf.oauth_consumer_key = '' # 'SET_YOUR_OAUTH_CONSUMER_KEY'
9
9
  conf.oauth_consumer_secret = '' # 'SET_YOUR_OAUTH_CONSUMER_SECRET'
10
+ conf.timeout = 15
10
11
  end
11
12
 
12
13
  # Edit here
@@ -36,7 +37,10 @@ end
36
37
  ## WITHOUT AUTHORIZATION ##
37
38
  ###########################
38
39
 
40
+ puts "WITHOUT AUTHORIZATION"
41
+
39
42
  ## User API
43
+ puts "# User API Calls"
40
44
  @user = Iknow::User.find('kirk')
41
45
  @user.items(:include_sentences => true)
42
46
  @user.lists
@@ -46,6 +50,7 @@ end
46
50
  @matched_users = Iknow::User.matching('matake')
47
51
 
48
52
  ## List API
53
+ puts "# List API Calls"
49
54
  @recent_lists = Iknow::List.recent
50
55
  @list = Iknow::List.find(31509, :include_sentences => true, :include_items => true)
51
56
  @list.items
@@ -53,6 +58,7 @@ end
53
58
  @matched_lists = Iknow::List.matching("イタリア語であいさつ")
54
59
 
55
60
  ## Item API
61
+ puts "# Item API Calls"
56
62
  @recent_items = Iknow::Item.recent(:include_sentences => true)
57
63
  @item = Iknow::Item.find(437525)
58
64
  @matched_items = Iknow::Item.matching('record', :include_sentences => true)
@@ -60,6 +66,7 @@ end
60
66
  @items.first.sentences
61
67
 
62
68
  ## Sentence API
69
+ puts "# Sentence API Calls"
63
70
  @recent_sentences = Iknow::Sentence.recent
64
71
  @sentence = Iknow::Sentence.find(312271)
65
72
  @matched_sentences = Iknow::Sentence.matching('record')
@@ -84,15 +91,26 @@ unless iknow_auth
84
91
  puts "Skip calls which require authentication"
85
92
  exit
86
93
  else
87
- puts "Authenticate Mode :: #{iknow_auth.mode}"
94
+ puts "## WITH AUTHORIZATION :: #{iknow_auth.mode}"
88
95
  end
89
96
 
90
97
  ## List API
91
-
98
+ puts "# List API"
92
99
  @list = Iknow::List.create(iknow_auth, :title => 'iKnow! gem test', :description => 'A list for iKnow! gem test')
93
100
  @list.add_item(iknow_auth, Iknow::Item.find(437525))
94
- unless OAUTH_ACCESS_TOKEN.empty?
95
- # A kind of weird, only with basic auth...
96
- @list.delete_item(iknow_auth, @list.items.first)
97
- end
98
- @list.delete(iknow_auth)
101
+ @list.delete_item(iknow_auth, @list.items.first)
102
+ @list.delete(iknow_auth)
103
+
104
+ ## Item API
105
+ puts "# Item API"
106
+ @item = Iknow::Item.create(iknow_auth, :cue => {:text => 'hello world! 2', :language => 'en', :part_of_speech => 'E'},
107
+ :response => {:text => 'ハローワールド!', :language => 'ja'})
108
+ @item.add_image(iknow_auth, 'http://farm4.static.flickr.com/3276/3102381796_a33c1ffdf1.jpg')
109
+ @item.add_sound(iknow_auth, 'http://matake.jp/download/hello_world.mp3')
110
+ @item.add_tags(iknow_auth, 'sample', 'programming')
111
+
112
+ ## Sentence API
113
+ puts "# Sentence API"
114
+ @sentence = Iknow::Sentence.create(iknow_auth, :text => 'Hello World!', :item => Iknow::Item.matching('hello world').first)
115
+ @sentence.add_image(iknow_auth, 'http://farm4.static.flickr.com/3276/3102381796_a33c1ffdf1.jpg')
116
+ @sentence.add_sound(iknow_auth, 'http://matake.jp/download/hello_world.mp3')
@@ -14,7 +14,7 @@ class Iknow::Config
14
14
  :api_protocol => 'http',
15
15
  :api_host => 'api.iknow.co.jp',
16
16
  :api_port => 80,
17
- :timeout => 5,
17
+ :timeout => 30,
18
18
  :api_key => '',
19
19
  :oauth_consumer_key => '',
20
20
  :oauth_consumer_secret => '',
@@ -1,7 +1,7 @@
1
1
  module Iknow::Version
2
2
  MAJOR = 0
3
- MINOR = 1
4
- REVISION = 1
3
+ MINOR = 2
4
+ REVISION = 0
5
5
  class << self
6
6
  def to_version
7
7
  "#{MAJOR}.#{MINOR}.#{REVISION}"
@@ -1,10 +1,14 @@
1
1
  class Iknow::Item < Iknow::Base
2
- ATTRIBUTES = [:sentences, :hash, :cue, :id]
3
- attr_reader *ATTRIBUTES
2
+ ATTRIBUTES = [:sentences, :responses, :cue, :id, :list]
3
+ READONLY_ATTRIBUTES = [:sentences, :responses, :cue, :id]
4
+ attr_accessor *(ATTRIBUTES - READONLY_ATTRIBUTES)
5
+ attr_reader *READONLY_ATTRIBUTES
4
6
 
5
7
  class Response
6
- ATTRIBUTES = [:text, :type, :language]
7
- attr_reader *ATTRIBUTES
8
+ ATTRIBUTES = [:text, :text_with_character, :type, :language]
9
+ READONLY_ATTRIBUTES = [:type]
10
+ attr_accessor *(ATTRIBUTES - READONLY_ATTRIBUTES)
11
+ attr_reader *READONLY_ATTRIBUTES
8
12
 
9
13
  def initialize(params = {})
10
14
  @text = params[:text]
@@ -15,9 +19,9 @@ class Iknow::Item < Iknow::Base
15
19
 
16
20
  class Cue
17
21
  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
22
+ READONLY_ATTRIBUTES = [:sound]
23
+ attr_accessor *(ATTRIBUTES - READONLY_ATTRIBUTES)
24
+ attr_reader *READONLY_ATTRIBUTES
21
25
 
22
26
  def initialize(params = {})
23
27
  @text = params[:text]
@@ -50,11 +54,87 @@ class Iknow::Item < Iknow::Base
50
54
  self.deserialize(hash) || []
51
55
  end
52
56
 
57
+ def self.create(iknow_auth, params = {})
58
+ self.new(params).save(iknow_auth)
59
+ end
60
+
53
61
  def initialize(params = {})
62
+ params[:responses] = [params[:response]] if params[:response]
54
63
  @id = params[:id].to_i
64
+ @list = params[:list]
55
65
  @cue = self.deserialize(params[:cue], :as => Iknow::Item::Cue)
56
- @hashs = self.deserialize(params[:hashs], :as => Iknow::Item::Response)
66
+ @responses = self.deserialize(params[:responses], :as => Iknow::Item::Response)
57
67
  @sentences = self.deserialize(params[:sentences], :as => Iknow::Sentence)
58
68
  end
59
69
 
70
+ def save(iknow_auth)
71
+ begin
72
+ item_id = Iknow::RestClient::Item.create(iknow_auth, self.to_post_data)
73
+ rescue
74
+ return false
75
+ end
76
+ Iknow::Item.find(item_id)
77
+ end
78
+
79
+ def add_image(iknow_auth, params)
80
+ post_params = if params.is_a?(String)
81
+ {'image[url]' => params,}
82
+ else
83
+ {'image[url]' => params[:url],
84
+ 'image[list_id]' => params[:list_id] }
85
+ end
86
+ Iknow::RestClient::Item.add_image(iknow_auth, post_params.merge(:id => self.id))
87
+ end
88
+
89
+ def add_sound(iknow_auth, params)
90
+ post_params = if params.is_a?(String)
91
+ {'sound[url]' => params,}
92
+ else
93
+ {'sound[url]' => params[:url],
94
+ 'sound[list_id]' => params[:list_id] }
95
+ end
96
+ Iknow::RestClient::Item.add_sound(iknow_auth, post_params.merge(:id => self.id))
97
+ end
98
+
99
+ def add_tags(iknow_auth, *tags)
100
+ post_params = {}
101
+ tags.each_with_index do |tag, idx|
102
+ post_params["semantic_tags[#{idx}][name]"] = tag
103
+ end
104
+ Iknow::RestClient::Item.add_tags(iknow_auth, post_params.merge(:id => self.id))
105
+ end
106
+
107
+ protected
108
+
109
+ def to_post_data
110
+ self.validate
111
+ post_data = {
112
+ 'cue[text]' => self.cue.text,
113
+ 'cue[language]' => self.cue.language,
114
+ 'cue[part_of_speech]' => self.cue.part_of_speech,
115
+ 'response[text]' => self.response.text,
116
+ 'response[language]' => self.response.language
117
+ }
118
+ # Optional attributes
119
+ if self.list
120
+ post_data['item[list_id]'] = self.list.id
121
+ end
122
+ if response.text_with_character
123
+ post_data['character_response[text]'] = self.response.character_text
124
+ end
125
+ post_data
126
+ end
127
+
128
+ def validate
129
+ raise ArgumentError.new("Item cue[text] is required.") if self.cue.text.nil? or self.cue.text.empty?
130
+ raise ArgumentError.new("Item cue[language] is required.") if self.cue.language.nil? or self.cue.language.empty?
131
+ raise ArgumentError.new("Item cue[part_of_speech] is required.") if self.cue.part_of_speech.nil? or self.cue.part_of_speech.empty?
132
+ raise ArgumentError.new("Item response[text] is required.") if self.response.text.nil? or self.response.text.empty?
133
+ raise ArgumentError.new("Item response[language] is required.") if self.response.language.nil? or self.response.language.empty?
134
+ end
135
+
136
+ def response
137
+ self.responses.first
138
+ end
139
+
60
140
  end
@@ -18,9 +18,9 @@ class Iknow::List < Iknow::Base
18
18
  :language, :translation_language, :list_type, :transcript, :embed,
19
19
  :tags, :media_entry, :author, :author_id, :author_url, :attribution_license_id,
20
20
  :items, :sentences]
21
- NOT_WRITABLE_ATTRIBUTES = [:id, :icon, :item_count, :user_count, :iknow, :dictation, :brainspeed]
22
- attr_accessor *(ATTRIBUTES - NOT_WRITABLE_ATTRIBUTES)
23
- attr_reader *NOT_WRITABLE_ATTRIBUTES
21
+ READONLY_ATTRIBUTES = [:id, :icon, :item_count, :user_count, :iknow, :dictation, :brainspeed]
22
+ attr_accessor *(ATTRIBUTES - READONLY_ATTRIBUTES)
23
+ attr_reader *READONLY_ATTRIBUTES
24
24
 
25
25
  class Application
26
26
  attr_reader :application, :list_id, :lang
@@ -100,8 +100,8 @@ class Iknow::List < Iknow::Base
100
100
  def save(iknow_auth)
101
101
  begin
102
102
  list_id = Iknow::RestClient::List.create(iknow_auth, self.to_post_data)
103
- # rescue
104
- # return false
103
+ rescue
104
+ return false
105
105
  end
106
106
  Iknow::List.find(list_id)
107
107
  end
@@ -122,8 +122,7 @@ class Iknow::List < Iknow::Base
122
122
  protected
123
123
 
124
124
  def to_post_data
125
- raise ArgumentError.new("List title is needed.") if self.title.nil? or self.title.empty?
126
-
125
+ self.validate
127
126
  post_data = {
128
127
  'list[name]' => self.title,
129
128
  'list[description]' => self.description,
@@ -143,4 +142,9 @@ class Iknow::List < Iknow::Base
143
142
  post_data
144
143
  end
145
144
 
145
+ def validate
146
+ raise ArgumentError.new("List title is required.") if self.title.nil? or self.title.empty?
147
+ raise ArgumentError.new("List description is required.") if self.description.nil? or self.description.empty?
148
+ end
149
+
146
150
  end
@@ -1,8 +1,8 @@
1
1
  class Iknow::Sentence < Iknow::Base
2
- ATTRIBUTES = [:sound, :image, :text, :language, :id, :transliterations, :translations]
3
- WRITABLE_ATTRIBUTES = [:sound, :image]
4
- attr_accessor *WRITABLE_ATTRIBUTES
5
- attr_reader *(ATTRIBUTES - WRITABLE_ATTRIBUTES)
2
+ ATTRIBUTES = [:sound, :image, :text, :language, :id, :transliterations, :translations, :item, :list]
3
+ READONLY_ATTRIBUTES = [:id]
4
+ attr_accessor *(ATTRIBUTES - READONLY_ATTRIBUTES)
5
+ attr_reader *READONLY_ATTRIBUTES
6
6
 
7
7
  def self.recent(params = {})
8
8
  hash = Iknow::RestClient::Sentence.recent(params)
@@ -21,8 +21,16 @@ class Iknow::Sentence < Iknow::Base
21
21
  self.deserialize(hash) || []
22
22
  end
23
23
 
24
+ def self.create(iknow_auth, params = {})
25
+ self.new(params).save(iknow_auth)
26
+ end
27
+
24
28
  def initialize(params = {})
29
+ params[:translations] = [params[:translation]] if params[:translation]
30
+ params[:transliterations] = [params[:transliteration]] if params[:transliteration]
25
31
  @id = params[:id]
32
+ @item = params[:item]
33
+ @list = params[:list]
26
34
  @sound = params[:sound]
27
35
  @image = params[:image]
28
36
  @text = params[:text]
@@ -31,4 +39,73 @@ class Iknow::Sentence < Iknow::Base
31
39
  @translations = self.deserialize(params[:translations], :as => Iknow::Sentence)
32
40
  end
33
41
 
42
+ def save(iknow_auth)
43
+ begin
44
+ sentence_id = Iknow::RestClient::Sentence.create(iknow_auth, self.to_post_data)
45
+ # rescue
46
+ # return false
47
+ end
48
+ Iknow::Sentence.find(sentence_id)
49
+ end
50
+
51
+ def add_image(iknow_auth, params)
52
+ post_params = if params.is_a?(String)
53
+ {'image[url]' => params,}
54
+ else
55
+ {'image[url]' => params[:url],
56
+ 'image[list_id]' => params[:list_id] }
57
+ end
58
+ Iknow::RestClient::Sentence.add_image(iknow_auth, post_params.merge(:id => self.id))
59
+ end
60
+
61
+ def add_sound(iknow_auth, params)
62
+ post_params = if params.is_a?(String)
63
+ {'sound[url]' => params,}
64
+ else
65
+ {'sound[url]' => params[:url],
66
+ 'sound[list_id]' => params[:list_id] }
67
+ end
68
+ Iknow::RestClient::Sentence.add_sound(iknow_auth, post_params.merge(:id => self.id))
69
+ end
70
+
71
+ protected
72
+
73
+ def to_post_data
74
+ self.validate
75
+ post_data = {
76
+ 'item_id' => self.item.id,
77
+ 'sentence[text]' => self.text
78
+ }
79
+ # Optional attributes
80
+ if self.list
81
+ post_data['sentence[list_id]'] = self.list.id
82
+ end
83
+ [:language, :transliteration].each do |key|
84
+ if self.send("#{key}")
85
+ post_data["sentence[#{key}]"] = self.send("#{key}")
86
+ end
87
+ end
88
+ if self.translation
89
+ [:text, :language, :transliteration].each do |key|
90
+ if self.translation.send("#{key}")
91
+ post_data["translation[#{key}]"] = self.translation.send("#{key}")
92
+ end
93
+ end
94
+ end
95
+ post_data
96
+ end
97
+
98
+ def validate
99
+ raise ArgumentError.new("Item is required.") unless self.item
100
+ raise ArgumentError.new("Sentence text is required.") if self.text.nil? or self.text.empty?
101
+ end
102
+
103
+ def translation
104
+ self.translations.first rescue nil
105
+ end
106
+
107
+ def transliteration
108
+ self.transliterations.first rescue nil
109
+ end
110
+
34
111
  end
@@ -63,16 +63,24 @@ class Iknow::RestClient::Base
63
63
  handle_json_response(response.body)
64
64
  when :nothing
65
65
  # success => nothing / failure => json error
66
- handle_json_response(response.body) rescue :success
67
- else
68
- response.body
66
+ begin
67
+ handle_json_response(response.body)
68
+ rescue Exception => e
69
+ e.is_a?(RESTError) ? raise(e) : :success
70
+ end
71
+ else
72
+ begin
73
+ handle_json_response(response.body)
74
+ rescue Exception => e
75
+ e.is_a?(RESTError) ? raise(e) : response.body
76
+ end
69
77
  end
70
78
  end
71
79
 
72
80
  def self.handle_json_response(json_response)
73
81
  hash = JSON.parse(json_response)
74
82
  unless (hash['error'].nil? rescue :success) # success response may be Array, not Hash.
75
- if hash['error']['code'].to_i == 404
83
+ if hash['error']['code'] == 404
76
84
  return nil
77
85
  else
78
86
  raise RESTError.new(:code => hash['error']['code'], :message => hash['error']['message'])
@@ -111,7 +119,7 @@ class Iknow::RestClient::Base
111
119
  unless params.empty?
112
120
  params.each do |key, value|
113
121
  if path_with_params=~/__#{key}__/
114
- path_with_params.sub!(/__#{key}__/, value.to_s)
122
+ path_with_params.sub!(/__#{key}__/, URI.encode(value.to_s))
115
123
  params.delete(key)
116
124
  end
117
125
  end
@@ -5,8 +5,10 @@ class Iknow::RestClient::Item < Iknow::RestClient::Base
5
5
  :find => { :path => '/items/__id__' },
6
6
  :matching => { :path => '/items/matching/__keyword__' },
7
7
  :extract => { :path => '/items/extract', },
8
+ :create => { :path => '/items', :http_method => :post },
8
9
  :add_image => { :path => '/items/__id__/images', :http_method => :post },
9
- :add_sound => { :path => '/items/__id__/sounds', :http_method => :post }
10
+ :add_sound => { :path => '/items/__id__/sounds', :http_method => :post },
11
+ :add_tags => { :path => '/items/__id__/tags', :http_method => :post }
10
12
  }
11
13
 
12
14
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nov-iknow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
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: 2008-12-20 00:00:00 -08:00
12
+ date: 2008-12-21 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency