jugyo-rcicindela 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,2 @@
1
+ - 0.1.0
2
+ - Released first version.
data/README.rdoc ADDED
@@ -0,0 +1,60 @@
1
+ = rcicindela
2
+
3
+ http://wiki.github.com/jugyo/rcicindela
4
+
5
+ == DESCRIPTION:
6
+
7
+ RCicindela is a wrapper library for Cicindela.
8
+
9
+ Cicindela is a recommendation engine made by Livedoor.
10
+ See Also: http://code.google.com/p/cicindela2/
11
+
12
+ == SYNOPSIS:
13
+
14
+ Initialization:
15
+
16
+ cicindela = RCicindela.new('http://localhost/cicindela')
17
+
18
+ Accumulating data:
19
+
20
+ cicindela.insert_pick(:set => 'test', :user_id => 1, :item_id => 1)
21
+ ...
22
+
23
+ Getting recommendations:
24
+
25
+ cicindela.for_item(:set => 'test', :item_id => 1)
26
+ ...
27
+
28
+ == INSTALL:
29
+
30
+ sudo gem install rcicindela
31
+
32
+ or
33
+
34
+ gem source -a http://gems.github.com
35
+ sudo gem install jugyo-rcicindela
36
+
37
+ == LICENSE:
38
+
39
+ (The MIT License)
40
+
41
+ Copyright (c) 2008-2009 jugyo
42
+
43
+ Permission is hereby granted, free of charge, to any person obtaining
44
+ a copy of this software and associated documentation files (the
45
+ 'Software'), to deal in the Software without restriction, including
46
+ without limitation the rights to use, copy, modify, merge, publish,
47
+ distribute, sublicense, and/or sell copies of the Software, and to
48
+ permit persons to whom the Software is furnished to do so, subject to
49
+ the following conditions:
50
+
51
+ The above copyright notice and this permission notice shall be
52
+ included in all copies or substantial portions of the Software.
53
+
54
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
55
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
56
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
57
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
58
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
59
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
60
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,41 @@
1
+ # -*- coding: utf-8 -*-
2
+ $:.unshift File.dirname(__FILE__) + '/lib/'
3
+ require 'rcicindela'
4
+ require 'spec/rake/spectask'
5
+
6
+ desc 'run all specs'
7
+ Spec::Rake::SpecTask.new do |t|
8
+ t.spec_files = FileList['spec/**/*_spec.rb']
9
+ t.spec_opts = ['-c']
10
+ end
11
+
12
+ desc 'Generate gemspec'
13
+ task :gemspec do |t|
14
+ open('rcicindela.gemspec', "wb" ) do |file|
15
+ file << <<-EOS
16
+ Gem::Specification.new do |s|
17
+ s.name = 'rcicindela'
18
+ s.version = '#{RCicindela::VERSION}'
19
+ s.summary = "RCicindela is a wrapper library for Cicindela."
20
+ s.description = "RCicindela is a wrapper library for Cicindela.\\n" +
21
+ "Cicindela is a recommendation engine made by Livedoor.\\n" +
22
+ "See Also: http://code.google.com/p/cicindela2/"
23
+ s.files = %w( #{Dir['lib/**/*.rb'].join(' ')}
24
+ #{Dir['spec/**/*.rb'].join(' ')}
25
+ README.rdoc
26
+ History.txt
27
+ Rakefile )
28
+ s.author = 'jugyo'
29
+ s.email = 'jugyo.org@gmail.com'
30
+ s.homepage = 'http://github.com/jugyo/rcicindela'
31
+ s.rubyforge_project = 'rcicindela'
32
+ end
33
+ EOS
34
+ end
35
+ puts "Generate gemspec"
36
+ end
37
+
38
+ desc 'Generate gem'
39
+ task :gem => :gemspec do |t|
40
+ system 'gem', 'build', 'rcicindela.gemspec'
41
+ end
data/lib/rcicindela.rb ADDED
@@ -0,0 +1,66 @@
1
+ require 'net/http'
2
+
3
+ class RCicindela
4
+ VERSION = '0.2.0'
5
+
6
+ class APIError < StandardError
7
+ attr_reader :response
8
+ def initialize(response, message = '')
9
+ super(message)
10
+ @response = response
11
+ end
12
+ end
13
+
14
+ attr_reader :host, :port, :base_path, :timeout, :default_params
15
+
16
+ def initialize(host, options = {})
17
+ @host = host
18
+ @port = options[:port] || 80
19
+ @base_path = options[:base_path] || '/'
20
+ @timeout = options[:timeout] || 1
21
+ @default_params = options[:default_params] || {}
22
+
23
+ @base_path.sub!(/\/+$/, '')
24
+ end
25
+
26
+ %w(
27
+ record/insert_pick
28
+ record/insert_rating
29
+ record/insert_tag
30
+ record/set_category
31
+ record/insert_uninterested
32
+ record/delete_pick
33
+ record/delete_rating
34
+ record/delete_tag
35
+ record/delete_uninterested
36
+ record/remove_category
37
+ recommend/for_item
38
+ recommend/for_user
39
+ recommend/similar_users
40
+ ).each do |api_method|
41
+ prefix, method = api_method.split('/')
42
+ define_method(method) do |params|
43
+ request(prefix, {:op => method}.merge(params))
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def request(prefix, params)
50
+ params = default_params.merge(params)
51
+ path = base_path + '/' + prefix + '?' + params.map{ |k, v| [k.to_s, v.to_s] }.sort.map{ |i| i.join('=') }.join('&')
52
+ get(path)
53
+ end
54
+
55
+ def get(path)
56
+ Net::HTTP.start(host, port) do |http|
57
+ http.open_timeout = http.read_timeout = timeout
58
+ response = http.get(path)
59
+ if response.code =~ /^2/
60
+ response.body
61
+ else
62
+ raise APIError.new(response)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,62 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.dirname(__FILE__) + '/spec_helper'
4
+
5
+ describe RCicindela do
6
+ it 'should create instance' do
7
+ c = RCicindela.new('localhost', :port => 80, :base_path => '/cicindela/', :timeout => 10)
8
+ c.host.should == 'localhost'
9
+ c.port.should == 80
10
+ c.base_path.should == '/cicindela'
11
+ c.timeout.should == 10
12
+ end
13
+
14
+ describe 'Create instance with "http://localhost/cicindela" as base_uri' do
15
+ before(:each) do
16
+ @cicindela = RCicindela.new('localhost', :base_path => '/cicindela')
17
+ end
18
+
19
+ it 'call method "get" when call insert_pick' do
20
+ @cicindela.should_receive(:request).with('record', {:op => 'insert_pick', :set => 'test', :user_id => 1, :item_id => 1})
21
+ @cicindela.insert_pick(:set => 'test', :user_id => 1, :item_id => 1)
22
+ end
23
+
24
+ it 'call method "get" when call for_item' do
25
+ @cicindela.should_receive(:request).with('recommend', {:op => 'for_item', :set => 'test', :item_id => 1})
26
+ @cicindela.for_item(:set => 'test', :item_id => 1)
27
+ end
28
+
29
+ it 'call method "open" when call insert_pick' do
30
+ @cicindela.should_receive(:get).
31
+ with("/cicindela/record?item_id=1&op=insert_pick&set=test&user_id=1").and_return('result')
32
+ result = @cicindela.insert_pick(:set => 'test', :user_id => 1, :item_id => 1)
33
+ result.should == 'result'
34
+ end
35
+
36
+ it 'call method "open" when call for_item' do
37
+ @cicindela.should_receive(:get).
38
+ with("/cicindela/recommend?item_id=1&op=for_item&set=test").and_return('result')
39
+ result = @cicindela.for_item(:set => 'test', :item_id => 1)
40
+ result.should == 'result'
41
+ end
42
+ end
43
+
44
+ describe 'Create instance with default option' do
45
+ before(:each) do
46
+ @cicindela = RCicindela.new(
47
+ 'localhost', :base_path => '/cicindela', :default_params => {:set => 'test', :user_id => 1})
48
+ end
49
+
50
+ it 'call method "get" when call insert_pick' do
51
+ @cicindela.should_receive(:request).with('record', {:op => 'insert_pick', :item_id => 1})
52
+ @cicindela.insert_pick(:item_id => 1)
53
+ end
54
+
55
+ it 'call method "open" when call insert_pick' do
56
+ @cicindela.should_receive(:get).
57
+ with("/cicindela/record?item_id=1&op=insert_pick&set=test&user_id=1").and_return('result')
58
+ result = @cicindela.insert_pick(:item_id => 1)
59
+ result.should == 'result'
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+ require 'rcicindela'
3
+
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jugyo-rcicindela
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - jugyo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: "RCicindela is a wrapper library for Cicindela. Cicindela is a recommendation engine made by Livedoor. See Also: http://code.google.com/p/cicindela2/"
17
+ email: jugyo.org@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/rcicindela.rb
26
+ - spec/rcicindela_spec.rb
27
+ - spec/spec_helper.rb
28
+ - README.rdoc
29
+ - History.txt
30
+ - Rakefile
31
+ has_rdoc: false
32
+ homepage: http://github.com/jugyo/rcicindela
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project: rcicindela
53
+ rubygems_version: 1.2.0
54
+ signing_key:
55
+ specification_version: 2
56
+ summary: RCicindela is a wrapper library for Cicindela.
57
+ test_files: []
58
+