iknow 0.0.4 → 0.1.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.
Files changed (47) hide show
  1. data/README +4 -23
  2. data/Rakefile +5 -2
  3. data/examples/pure_ruby.rb +77 -35
  4. data/lib/ext/hash.rb +29 -0
  5. data/lib/iknow/core/auth.rb +29 -0
  6. data/lib/iknow/core/config.rb +1 -1
  7. data/lib/iknow/core/version.rb +2 -2
  8. data/lib/iknow/core.rb +2 -1
  9. data/lib/iknow/model/list.rb +31 -18
  10. data/lib/iknow/rest_client/base.rb +66 -49
  11. data/lib/iknow/rest_client/item.rb +5 -5
  12. data/lib/iknow/rest_client/list.rb +9 -7
  13. metadata +14 -39
  14. data/examples/iknow_on_rails/app/controllers/application.rb +0 -15
  15. data/examples/iknow_on_rails/app/controllers/iknow_oauth_controller.rb +0 -24
  16. data/examples/iknow_on_rails/app/controllers/users_controller.rb +0 -10
  17. data/examples/iknow_on_rails/app/helpers/application_helper.rb +0 -3
  18. data/examples/iknow_on_rails/app/helpers/iknow_oauth_helper.rb +0 -3
  19. data/examples/iknow_on_rails/app/helpers/users_helper.rb +0 -2
  20. data/examples/iknow_on_rails/app/models/iknow_oauth_token.rb +0 -37
  21. data/examples/iknow_on_rails/config/boot.rb +0 -109
  22. data/examples/iknow_on_rails/config/environment.rb +0 -76
  23. data/examples/iknow_on_rails/config/environments/development.rb +0 -17
  24. data/examples/iknow_on_rails/config/environments/production.rb +0 -22
  25. data/examples/iknow_on_rails/config/environments/test.rb +0 -22
  26. data/examples/iknow_on_rails/config/initializers/inflections.rb +0 -10
  27. data/examples/iknow_on_rails/config/initializers/mime_types.rb +0 -5
  28. data/examples/iknow_on_rails/config/initializers/new_rails_defaults.rb +0 -17
  29. data/examples/iknow_on_rails/config/routes.rb +0 -44
  30. data/examples/iknow_on_rails/db/migrate/20081017012212_create_iknow_oauth_tokens.rb +0 -15
  31. data/examples/iknow_on_rails/db/schema.rb +0 -23
  32. data/examples/iknow_on_rails/lib/iknow_oauth_system.rb +0 -26
  33. data/examples/iknow_on_rails/public/dispatch.rb +0 -10
  34. data/examples/iknow_on_rails/test/functional/iknow_oauth_controller_test.rb +0 -7
  35. data/examples/iknow_on_rails/test/functional/users_controller_test.rb +0 -8
  36. data/examples/iknow_on_rails/test/test_helper.rb +0 -38
  37. data/examples/iknow_on_rails/test/unit/iknow_oauth_token_test.rb +0 -7
  38. data/generators/iknow_oauth/USAGE +0 -12
  39. data/generators/iknow_oauth/iknow_oauth_generator.rb +0 -22
  40. data/generators/iknow_oauth/templates/iknow_oauth_controller.rb +0 -24
  41. data/generators/iknow_oauth/templates/iknow_oauth_controller_test.rb +0 -7
  42. data/generators/iknow_oauth/templates/iknow_oauth_helper.rb +0 -3
  43. data/generators/iknow_oauth/templates/iknow_oauth_system.rb +0 -26
  44. data/generators/iknow_oauth/templates/iknow_oauth_token.rb +0 -37
  45. data/generators/iknow_oauth/templates/iknow_oauth_token_test.rb +0 -7
  46. data/generators/iknow_oauth/templates/iknow_oauth_tokens_migration.rb +0 -15
  47. data/generators/iknow_oauth/templates/index.rhtml +0 -4
data/README CHANGED
@@ -6,6 +6,9 @@
6
6
 
7
7
  This rubygem is a wrapper of iKnow! API.
8
8
  You can get pure-ruby example at examples/pure_ruby.rb.
9
+ http://github.com/nov/iknow/tree/master/examples/pure_ruby.rb
10
+
11
+ It shows all API calls you can use with this gem.
9
12
 
10
13
  == Installation
11
14
 
@@ -19,7 +22,7 @@
19
22
 
20
23
  === Gem Installation
21
24
 
22
- gem install nov-iknow
25
+ gem install iknow
23
26
 
24
27
  == Features/Problems
25
28
 
@@ -29,31 +32,9 @@
29
32
  They will be implemented in a few weeks.
30
33
 
31
34
  iKnow! OAuth is still pre-alpha.
32
- Wait a few weeks to use it, please.
33
35
 
34
36
  == Synopsis
35
-
36
- % rails iknow_on_rails
37
- % cd iknow_on_rails
38
- % ./script/generate iknow_oauth
39
- % rake db:create
40
- % rake db:migrate
41
-
42
- and edit environment.rb like examples/iknow_on_rails
43
- and add some controllers and views like examples/iknow_on_rails
44
- and go http://localhost:3000/
45
37
 
46
- You can run examples/iknow_on_rails
47
-
48
- 1. DOWNLOAD THIS GEM
49
-
50
- 2. Run the example
51
- % cd iknow/examples/iknow_on_rails (cd nov-iknow/examples/iknow_on_rails ??)
52
- % rake db:create
53
- % rake db:migrate
54
- % ./script/server
55
-
56
- About Another things
57
38
  See examples and iKnow! Developers, please.
58
39
  iKnow! Developers (http://developer.iknow.co.jp)
59
40
 
data/Rakefile CHANGED
@@ -62,11 +62,12 @@ spec = Gem::Specification.new do |s|
62
62
 
63
63
  s.add_dependency('rails', '>=2.1.0')
64
64
  s.add_dependency('json')
65
- s.add_dependency('oauth', '>=0.2.4')
65
+ s.add_dependency('mechanize')
66
+ s.add_dependency('oauth', '>=0.2.7')
66
67
  s.required_ruby_version = '>= 1.8.6'
67
68
 
68
69
  s.files = %w(README ChangeLog Rakefile) +
69
- Dir.glob("{bin,doc,test,lib,templates,generators,extras,website,script}/**/*") +
70
+ Dir.glob("{bin,doc,test,lib,templates,extras,website,script}/**/*") +
70
71
  Dir.glob("ext/**/*.{h,c,rb}") +
71
72
  Dir.glob("examples/**/*.rb") +
72
73
  Dir.glob("tools/*.rb") +
@@ -80,12 +81,14 @@ Rake::GemPackageTask.new(spec) do |p|
80
81
  p.gem_spec = spec
81
82
  end
82
83
 
84
+ desc "Install"
83
85
  task :install do
84
86
  name = "#{NAME}-#{VERS}.gem"
85
87
  sh %{rake package}
86
88
  sh %{sudo gem install pkg/#{name}}
87
89
  end
88
90
 
91
+ desc "Uninstall"
89
92
  task :uninstall => [:clean] do
90
93
  sh %{sudo gem uninstall #{NAME}}
91
94
  end
@@ -1,12 +1,22 @@
1
1
  require 'rubygems'
2
2
  require 'iknow'
3
+ require 'oauth/consumer'
3
4
 
4
5
  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'
6
+ conf.api_host = 'api.iknow.co.jp'
7
+ conf.api_key = '' # 'SET_YOUR_API_KEY'
8
+ conf.oauth_consumer_key = '' # 'SET_YOUR_OAUTH_CONSUMER_KEY'
9
+ conf.oauth_consumer_secret = '' # 'SET_YOUR_OAUTH_CONSUMER_SECRET'
8
10
  end
9
11
 
12
+ # Edit here
13
+ OAUTH_ACCESS_TOKEN = ''
14
+ OAUTH_ACCESS_TOKEN_SECRET = ''
15
+
16
+ # Edit here
17
+ IKNOW_USERNAME = ''
18
+ IKNOW_PASSWORD = ''
19
+
10
20
  please_get_api_key =<<EOS
11
21
  This example needs your own iKnow! API key.
12
22
  (for only Iknow::Item.extract example)
@@ -17,38 +27,70 @@ iKnow! Developers (http://developer.iknow.co.jp/)
17
27
  Thanks!
18
28
  EOS
19
29
 
20
- if Iknow::Config.api_key == ''# or
21
- # Iknow::Config.oauth_consumer_key == '' or
22
- # Iknow::Config.oauth_consumer_secret == ''
30
+ if Iknow::Config.api_key == ''
23
31
  raise ArgumentError.new(please_get_api_key)
24
32
  end
25
33
 
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')
34
+
35
+ ###########################
36
+ ## WITHOUT AUTHORIZATION ##
37
+ ###########################
38
+
39
+ ## User API
40
+ @user = Iknow::User.find('kirk')
41
+ @user.items(:include_sentences => true)
42
+ @user.lists
43
+ @user.friends
44
+ @user.study.results
45
+ @user.study.total_summary
46
+ @matched_users = Iknow::User.matching('matake')
47
+
48
+ ## List API
49
+ @recent_lists = Iknow::List.recent
50
+ @list = Iknow::List.find(31509, :include_sentences => true, :include_items => true)
51
+ @list.items
52
+ @list.sentences
53
+ @matched_lists = Iknow::List.matching("イタリア語であいさつ")
54
+
55
+ ## Item API
56
+ @recent_items = Iknow::Item.recent(:include_sentences => true)
57
+ @item = Iknow::Item.find(437525)
58
+ @matched_items = Iknow::Item.matching('record', :include_sentences => true)
59
+ @items = Iknow::Item.extract("sometimes, often, electrical")
60
+ @items.first.sentences
61
+
62
+ ## Sentence API
63
+ @recent_sentences = Iknow::Sentence.recent
64
+ @sentence = Iknow::Sentence.find(312271)
65
+ @matched_sentences = Iknow::Sentence.matching('record')
66
+
67
+
68
+ ########################
69
+ ## WITH AUTHORIZATION ##
70
+ ########################
71
+
72
+ iknow_auth = case
73
+ when OAUTH_ACCESS_TOKEN != ''
74
+ if Iknow::Config.oauth_consumer_key == '' or Iknow::Config.oauth_consumer_secret == ''
75
+ raise ArgumentError.new("oauth_consumer_key and oauth_consumer_secret are required")
76
+ end
77
+ Iknow::Auth.new(:token => OAUTH_ACCESS_TOKEN, :secret => OAUTH_ACCESS_TOKEN_SECRET)
78
+ when IKNOW_USERNAME != ''
79
+ Iknow::Auth.new(:username => IKNOW_USERNAME, :password => IKNOW_PASSWORD)
80
+ else
81
+ nil
82
+ end
83
+ unless iknow_auth
84
+ puts "Skip calls which require authentication"
85
+ exit
86
+ end
87
+
88
+ ## List API
89
+
90
+ @list = Iknow::List.create(iknow_auth, :title => 'iKnow! gem test', :description => 'A list for iKnow! gem test')
91
+ @list.add_item(iknow_auth, Iknow::Item.find(437525))
92
+ unless OAUTH_ACCESS_TOKEN.empty?
93
+ # A kind of weird, only with basic auth...
94
+ @list.delete_item(iknow_auth, @list.items.first)
95
+ end
96
+ @list.delete(iknow_auth)
data/lib/ext/hash.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  class Hash
2
+
2
3
  def to_http_str
3
4
  result = ''
4
5
  return result if self.empty?
@@ -7,6 +8,7 @@ class Hash
7
8
  end
8
9
  result.chop
9
10
  end
11
+
10
12
  def symbolize_keys!
11
13
  self.each do |key, value|
12
14
  unless self.delete(key.to_s).nil?
@@ -20,4 +22,31 @@ class Hash
20
22
  end
21
23
  self
22
24
  end
25
+
26
+ def stringfy_keys!
27
+ self.each do |key, value|
28
+ unless self.delete(key.to_sym).nil?
29
+ if value.is_a?(Hash)
30
+ value.stringfy_keys!
31
+ elsif value.is_a?(Array)
32
+ value.map!{ |v| v.stringfy_keys! if v.is_a?(Hash) }
33
+ end
34
+ self[key.to_s] = value
35
+ end
36
+ end
37
+ self
38
+ end
39
+
40
+ def stringfy_values!
41
+ self.each do |key, value|
42
+ if value.is_a?(Hash)
43
+ value.stringfy_values!
44
+ elsif value.is_a?(Array)
45
+ value.map!{ |v| v.stringfy_values! if v.is_a?(Hash) }
46
+ end
47
+ self[key] = value.to_s
48
+ end
49
+ self
50
+ end
51
+
23
52
  end
@@ -0,0 +1,29 @@
1
+ require 'oauth/consumer'
2
+ require 'mechanize'
3
+
4
+ class Iknow::Auth
5
+ attr_accessor :mode, :auth_token
6
+
7
+ def initialize(options = {})
8
+ if options[:username] && options[:password]
9
+ @mode = :basic_auth
10
+ @auth_token = WWW::Mechanize.new
11
+ @auth_token.auth(options[:username], options[:password])
12
+ elsif options[:token] && options[:secret]
13
+ @mode = :oauth
14
+ @auth_token = OAuth::AccessToken.new(Iknow::Auth.consumer, options[:token], options[:secret])
15
+ else
16
+ raise ArgumentError.new('{:auth => "oauth_access_token", :secret => "oauth_access_token_secret"} or {:username "iknow_username", :password => "iknow_password"} is needed')
17
+ end
18
+ end
19
+
20
+ def self.consumer
21
+ @@consumer ||= OAuth::Consumer.new(
22
+ Iknow::Config.oauth_consumer_key,
23
+ Iknow::Config.oauth_consumer_secret,
24
+ :site => Iknow::Config.iknow_api_base_url,
25
+ :authorize_url => "#{Iknow::Config.iknow_base_url}/oauth/authorize"
26
+ )
27
+ end
28
+
29
+ end
@@ -20,7 +20,7 @@ class Iknow::Config
20
20
  :user_agent => 'default',
21
21
  :application_name => 'A rubygem for iKnow! API',
22
22
  :application_version => Iknow::Version.to_version,
23
- :application_url => 'http://github.com/nov/iknow_oauth_generator',
23
+ :application_url => 'http://github.com/nov/iknow',
24
24
  :source => 'iknow'
25
25
  }.each do |key, value| conf.send("#{key}=", value) end
26
26
  yield conf if block_given?
@@ -1,7 +1,7 @@
1
1
  module Iknow::Version
2
2
  MAJOR = 0
3
- MINOR = 0
4
- REVISION = 4
3
+ MINOR = 1
4
+ REVISION = 0
5
5
  class << self
6
6
  def to_version
7
7
  "#{MAJOR}.#{MINOR}.#{REVISION}"
data/lib/iknow/core.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require 'iknow/core/version'
2
- require 'iknow/core/config'
2
+ require 'iknow/core/config'
3
+ require 'iknow/core/auth'
@@ -14,11 +14,11 @@
14
14
  # "author_url": "http://www.iknow.co.jp/user/Cerego"
15
15
 
16
16
  class Iknow::List < Iknow::Base
17
- ATTRIBUTES = [:list_id, :title, :description, :icon, :item_count, :user_count, :iknow, :dictation, :brainspeed,
17
+ ATTRIBUTES = [:id, :title, :description, :icon, :item_count, :user_count, :iknow, :dictation, :brainspeed,
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 = [:list_id, :icon, :item_count, :user_count, :iknow, :dictation, :brainspeed]
21
+ NOT_WRITABLE_ATTRIBUTES = [:id, :icon, :item_count, :user_count, :iknow, :dictation, :brainspeed]
22
22
  attr_accessor *(ATTRIBUTES - NOT_WRITABLE_ATTRIBUTES)
23
23
  attr_reader *NOT_WRITABLE_ATTRIBUTES
24
24
 
@@ -51,13 +51,16 @@ class Iknow::List < Iknow::Base
51
51
  self.deserialize(response) || []
52
52
  end
53
53
 
54
- def self.create(params = {})
55
- new_list = self.new(params)
56
- new_list.save!
54
+ def self.create(iknow_auth, params = {})
55
+ self.new(params).save(iknow_auth)
56
+ end
57
+
58
+ def self.delete(list_id)
59
+ self.find(list_id).delete
57
60
  end
58
61
 
59
62
  def initialize(params = {})
60
- @list_id = (params[:id].to_i rescue nil)
63
+ @id = (params[:id].to_i rescue nil)
61
64
  @title = params[:title]
62
65
  @description = params[:description]
63
66
  @icon = params[:icon]
@@ -66,7 +69,7 @@ class Iknow::List < Iknow::Base
66
69
  @language = params[:language]
67
70
  @translation_language = params[:translation_language]
68
71
  if @list_id and @translation_language
69
- common_settings = {:list_id => @list_id, :lang => @translation_language}
72
+ common_settings = {:list_id => @id, :lang => @translation_language}
70
73
  @iknow = Application.new(common_settings.merge(:application => 'iknow')) if params[:iknow]
71
74
  @dictation = Application.new(common_settings.merge(:application => 'dictation')) if params[:dictation]
72
75
  @brainspeed = Application.new(common_settings.merge(:application => 'brainspeed')) if params[:brainspeed]
@@ -85,24 +88,35 @@ class Iknow::List < Iknow::Base
85
88
  end
86
89
 
87
90
  def items(params = {})
88
- response = Iknow::RestClient::List.items(params.merge(:id => self.list_id))
91
+ response = Iknow::RestClient::List.items(params.merge(:id => self.id))
89
92
  self.deserialize(response, :as => Iknow::Item) || []
90
93
  end
91
94
 
92
95
  def sentences(params = {})
93
- response = Iknow::RestClient::List.sentences(params.merge(:id => self.list_id))
96
+ response = Iknow::RestClient::List.sentences(params.merge(:id => self.id))
94
97
  self.deserialize(response, :as => Iknow::Sentence) || []
95
98
  end
96
99
 
97
- def save!
98
- Iknow::RestClient::List.create(self.to_post_data)
100
+ def save(iknow_auth)
101
+ begin
102
+ list_id = Iknow::RestClient::List.create(iknow_auth, self.to_post_data)
103
+ rescue
104
+ return false
105
+ end
106
+ Iknow::List.find(list_id)
107
+ end
108
+
109
+ def delete(iknow_auth)
110
+ Iknow::RestClient::List.delete(iknow_auth, :id => self.id)
99
111
  end
112
+ alias_method :destroy, :delete
100
113
 
101
- def save
102
- self.save!
103
- true
104
- rescue
105
- false
114
+ def add_item(iknow_auth, item)
115
+ Iknow::RestClient::List.add_item(iknow_auth, {:list_id => self.id, :id => item.id})
116
+ end
117
+
118
+ def delete_item(iknow_auth, item)
119
+ Iknow::RestClient::List.delete_item(iknow_auth, {:list_id => self.id, :id => item.id})
106
120
  end
107
121
 
108
122
  protected
@@ -116,11 +130,10 @@ class Iknow::List < Iknow::Base
116
130
  'list[language]' => self.language || 'en',
117
131
  'list[translation_language]' => self.translation_language || 'ja'
118
132
  }
119
- # Object#type should not be used
133
+ # Optional attributes
120
134
  if self.list_type
121
135
  post_data['list[type]'] = self.list_type
122
136
  end
123
- # Optional attributes
124
137
  [ :transcript, :embed, :tags, :media_entry,
125
138
  :author, :author_url, :attribution_license_id ].each do |key|
126
139
  if self.send("#{key}")
@@ -1,16 +1,15 @@
1
1
  class Iknow::RestClient::Base
2
2
 
3
3
  class RESTError < Exception
4
- attr_accessor :code, :message, :uri
4
+ attr_accessor :code, :message
5
5
 
6
6
  def initialize(params = {})
7
7
  self.code = params[:code]
8
8
  self.message = params[:message]
9
- self.uri = params[:uri]
10
9
  end
11
10
 
12
11
  def to_s
13
- "HTTP #{@code}: #{@message} at #{@uri}"
12
+ "HTTP #{@code}: #{@message}"
14
13
  end
15
14
  end
16
15
 
@@ -22,43 +21,41 @@ class Iknow::RestClient::Base
22
21
  # GET methods are handled here
23
22
  # POST and DELETE methods should be implemented in each class
24
23
  super unless self.valid_action?(action)
25
- uri, params = replace_uri_params(self.path(action), args[0])
26
- http_connect do |conn|
27
- case self.http_method(action)
28
- when :get
29
- http_get_request(uri, params)
30
- when :post
31
- http_post_request(uri, params)
32
- when :delete
33
- http_delete_request(uri, params)
34
- end
24
+ case self.http_method(action)
25
+ when :get
26
+ path, params = path_with_params(self.path(action), args[0])
27
+ http_get_request(path, params)
28
+ when :post
29
+ path, params = path_with_params(self.path(action), args[1])
30
+ http_post_request(iknow_auth(args[0]), path, params)
31
+ when :delete
32
+ path, params = path_with_params(self.path(action), args[1])
33
+ http_delete_request(iknow_auth(args[0]), path, params)
35
34
  end
36
35
  end
37
36
 
38
37
  private
39
38
 
40
39
  def self.config; Iknow::Config end
40
+
41
+ def self.iknow_auth(iknow_auth)
42
+ if iknow_auth.is_a?(Iknow::Auth)
43
+ iknow_auth
44
+ else
45
+ raise ArgumentError.new("Authorized Iknow::Auth instance is required")
46
+ end
47
+ end
41
48
 
42
49
  def self.api_key_required
43
50
  raise ArgumentError.new("iKnow! API key is required") if self.config.api_key == ''
44
51
  end
45
52
 
46
- def self.http_connect
47
- connection = Net::HTTP.new(self.config.api_host, self.config.api_port)
48
- connection.start do |conn|
49
- request = yield connection
50
- response = conn.request(request)
51
- handle_rest_response(response)
52
- JSON.parse(response.body)
53
- end
54
- end
55
-
56
- def self.raise_rest_error(response, uri = nil)
57
- raise RESTError.new(:code => response.code, :message => response.message, :uri => uri)
53
+ def self.raise_rest_error(response)
54
+ raise RESTError.new(:code => response.code, :message => response.message)
58
55
  end
59
56
 
60
- def self.handle_rest_response(response, uri = nil)
61
- raise_rest_error(response, uri) unless response.is_a?(Net::HTTPSuccess)
57
+ def self.handle_rest_response(response)
58
+ raise_rest_error(response) unless response.is_a?(Net::HTTPSuccess)
62
59
  end
63
60
 
64
61
  def self.http_header
@@ -72,43 +69,63 @@ class Iknow::RestClient::Base
72
69
  @@http_header
73
70
  end
74
71
 
75
- def self.replace_uri_params(uri, params = {})
76
- replaced_uri = uri.clone
72
+ def self.path_with_params(path, params = {})
73
+ path_with_params = path.clone
77
74
  unless params.empty?
78
75
  params.each do |key, value|
79
- if replaced_uri=~/__#{key}__/
80
- replaced_uri.sub!(/__#{key}__/, value.to_s)
76
+ if path_with_params=~/__#{key}__/
77
+ path_with_params.sub!(/__#{key}__/, value.to_s)
81
78
  params.delete(key)
82
79
  end
83
80
  end
84
81
  end
85
- return replaced_uri, params
82
+ return path_with_params, params
86
83
  end
87
84
 
88
- def self.http_get_request(uri, params = {})
89
- unless self.config.api_key == ''
90
- params.merge!(:api_key => self.config.api_key)
91
- end
92
- path = (params.size > 0) ? "#{uri}?#{params.to_http_str}" : uri
93
- Net::HTTP::Get.new(path, http_header)
85
+ def self.http_get_request(path, params = {})
86
+ params.merge!(:api_key => self.config.api_key) unless self.config.api_key == ''
87
+ path = (params.size > 0) ? "#{path}?#{params.to_http_str}" : path
88
+ http = Net::HTTP.new(self.config.api_host, self.config.api_port)
89
+ response = http.get(path, http_header)
90
+ handle_rest_response(response)
91
+ JSON.parse(response.body)
94
92
  end
95
93
 
96
- def self.http_post_request(uri, params = {})
94
+ def self.http_post_request(iknow_auth, path, params = {})
97
95
  self.api_key_required
98
- request = Net::HTTP::Post.new(uri, http_header)
99
- request.body = params.merge(:api_key => self.config.api_key).to_http_str
100
- request
96
+ params.merge!(:api_key => self.config.api_key)
97
+ case iknow_auth.mode
98
+ when :oauth
99
+ response = iknow_auth.auth_token.post(path, params, http_header)
100
+ handle_rest_response(response)
101
+ response.body
102
+ when :basic_auth
103
+ begin
104
+ response = iknow_auth.auth_token.post(self.config.iknow_api_base_url + path, params)
105
+ response.body
106
+ rescue WWW::Mechanize::ResponseCodeError => e
107
+ raise RESTError.new(:code => e.response_code, :message => e.to_s)
108
+ end
109
+ end
101
110
  end
102
111
 
103
- def self.http_delete_request(uri, params = {})
104
- unless self.config.api_key == ''
105
- params.merge!(:api_key => self.config.api_key)
112
+ def self.http_delete_request(iknow_auth, path, params = {})
113
+ self.api_key_required
114
+ params.merge!(:api_key => self.config.api_key)
115
+ case iknow_auth.mode
116
+ when :oauth
117
+ response = iknow_auth.auth_token.delete(path, params.stringfy_keys!.stringfy_values!)
118
+ handle_rest_response(response)
119
+ when :basic_auth
120
+ begin
121
+ iknow_auth.auth_token.delete(self.config.iknow_api_base_url + path, params.stringfy_keys!)
122
+ rescue WWW::Mechanize::ResponseCodeError => e
123
+ raise RESTError.new(:code => e.response_code, :message => e.to_s)
124
+ end
106
125
  end
107
- path = (params.size > 0) ? "#{uri}?#{params.to_http_str}" : uri
108
- Net::HTTP::Delete.new(path, http_header)
109
126
  end
110
127
 
111
- private_class_method :http_connect, :raise_rest_error, :handle_rest_response, :http_header,
112
- :replace_uri_params, :http_get_request, :http_post_request, :http_delete_request
128
+ private_class_method :raise_rest_error, :handle_rest_response, :http_header,
129
+ :path_with_params, :http_get_request, :http_post_request, :http_delete_request
113
130
 
114
131
  end
@@ -1,12 +1,12 @@
1
1
  class Iknow::RestClient::Item < Iknow::RestClient::Base
2
2
 
3
3
  ACTIONS = {
4
- :recent => { :path => '/items' },
5
- :find => { :path => '/items/__id__' },
6
- :matching => { :path => '/items/matching/__keyword__' },
4
+ :recent => { :path => '/items' },
5
+ :find => { :path => '/items/__id__' },
6
+ :matching => { :path => '/items/matching/__keyword__' },
7
+ :extract => { :path => '/items/extract', },
7
8
  :add_image => { :path => '/items/__id__/images', :http_method => :post },
8
- :add_sound => { :path => '/items/__id__/sounds', :http_method => :post },
9
- :extract => { :path => '/items/extract', :http_method => :post }
9
+ :add_sound => { :path => '/items/__id__/sounds', :http_method => :post }
10
10
  }
11
11
 
12
12
  end
@@ -1,13 +1,15 @@
1
1
  class Iknow::RestClient::List < Iknow::RestClient::Base
2
2
 
3
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 }
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 }
11
13
  }
12
14
 
13
15
  end