rcicindela 0.1.0 → 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.
data/README.rdoc CHANGED
@@ -5,6 +5,7 @@ http://wiki.github.com/jugyo/rcicindela
5
5
  == DESCRIPTION:
6
6
 
7
7
  RCicindela is a wrapper library for Cicindela.
8
+
8
9
  Cicindela is a recommendation engine made by Livedoor.
9
10
  See Also: http://code.google.com/p/cicindela2/
10
11
 
@@ -26,6 +27,10 @@ Getting recommendations:
26
27
 
27
28
  == INSTALL:
28
29
 
30
+ sudo gem install rcicindela
31
+
32
+ or
33
+
29
34
  gem source -a http://gems.github.com
30
35
  sudo gem install jugyo-rcicindela
31
36
 
data/lib/rcicindela.rb CHANGED
@@ -1,12 +1,18 @@
1
- require 'open-uri'
1
+ require 'net/http'
2
2
 
3
3
  class RCicindela
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
 
6
- attr_reader :base_uri
6
+ attr_reader :host, :port, :base_path, :timeout, :default_params
7
7
 
8
- def initialize(base_uri)
9
- @base_uri = base_uri.sub(/\/*$/, '')
8
+ def initialize(host, options = {})
9
+ @host = host
10
+ @port = options[:port] || 80
11
+ @base_path = options[:base_path] || '/'
12
+ @timeout = options[:timeout] || 1
13
+ @default_params = options[:default_params] || {}
14
+
15
+ @base_path.sub!(/\/+$/, '')
10
16
  end
11
17
 
12
18
  %w(
@@ -26,15 +32,24 @@ class RCicindela
26
32
  ).each do |api_method|
27
33
  prefix, method = api_method.split('/')
28
34
  define_method(method) do |params|
29
- get(prefix, {:op => method}.merge(params))
35
+ request(prefix, {:op => method}.merge(params))
30
36
  end
31
37
  end
32
38
 
33
39
  private
34
40
 
35
- def get(prefix, params)
36
- url = base_uri + '/' + prefix + '?' + params.map{ |k, v| [k.to_s, v.to_s] }.sort.map{ |i| i.join('=') }.join('&')
37
- open(url).read
41
+ def request(prefix, params)
42
+ params = default_params.merge(params)
43
+ path = base_path + '/' + prefix + '?' + params.map{ |k, v| [k.to_s, v.to_s] }.sort.map{ |i| i.join('=') }.join('&')
44
+ get(path)
38
45
  end
39
- end
40
46
 
47
+ def get(path)
48
+ result = nil
49
+ Net::HTTP.start(host, port) do |http|
50
+ http.open_timeout = http.read_timeout = timeout
51
+ result = http.get(path).body
52
+ end
53
+ result
54
+ end
55
+ end
@@ -3,52 +3,60 @@
3
3
  require File.dirname(__FILE__) + '/spec_helper'
4
4
 
5
5
  describe RCicindela do
6
- it 'should create instance with base_uri' do
7
- uri = 'http://localhost/cicindela'
8
- c = RCicindela.new(uri)
9
- c.base_uri.should == uri
10
- end
11
-
12
- it 'should create instance with base_uri end "/"' do
13
- uri = 'http://localhost/cicindela/'
14
- c = RCicindela.new(uri)
15
- c.base_uri.should == 'http://localhost/cicindela'
16
- end
17
-
18
- it 'should create instance with base_uri end "///"' do
19
- uri = 'http://localhost/cicindela///'
20
- c = RCicindela.new(uri)
21
- c.base_uri.should == 'http://localhost/cicindela'
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
22
12
  end
23
13
 
24
14
  describe 'Create instance with "http://localhost/cicindela" as base_uri' do
25
15
  before(:each) do
26
- @cicindela = RCicindela.new('http://localhost/cicindela')
27
- end
16
+ @cicindela = RCicindela.new('localhost', :base_path => '/cicindela')
17
+ end
28
18
 
29
19
  it 'call method "get" when call insert_pick' do
30
- @cicindela.should_receive(:get).with('record', {:op => 'insert_pick', :set => 'test', :user_id => 1, :item_id => 1})
20
+ @cicindela.should_receive(:request).with('record', {:op => 'insert_pick', :set => 'test', :user_id => 1, :item_id => 1})
31
21
  @cicindela.insert_pick(:set => 'test', :user_id => 1, :item_id => 1)
32
22
  end
33
23
 
34
24
  it 'call method "get" when call for_item' do
35
- @cicindela.should_receive(:get).with('recommend', {:op => 'for_item', :set => 'test', :item_id => 1})
25
+ @cicindela.should_receive(:request).with('recommend', {:op => 'for_item', :set => 'test', :item_id => 1})
36
26
  @cicindela.for_item(:set => 'test', :item_id => 1)
37
27
  end
38
28
 
39
29
  it 'call method "open" when call insert_pick' do
40
- @cicindela.should_receive(:open).
41
- with("http://localhost/cicindela/record?item_id=1&op=insert_pick&set=test&user_id=1").and_return(StringIO.new('result'))
30
+ @cicindela.should_receive(:get).
31
+ with("/cicindela/record?item_id=1&op=insert_pick&set=test&user_id=1").and_return('result')
42
32
  result = @cicindela.insert_pick(:set => 'test', :user_id => 1, :item_id => 1)
43
33
  result.should == 'result'
44
34
  end
45
35
 
46
36
  it 'call method "open" when call for_item' do
47
- @cicindela.should_receive(:open).
48
- with("http://localhost/cicindela/recommend?item_id=1&op=for_item&set=test").and_return(StringIO.new('result'))
37
+ @cicindela.should_receive(:get).
38
+ with("/cicindela/recommend?item_id=1&op=for_item&set=test").and_return('result')
49
39
  result = @cicindela.for_item(:set => 'test', :item_id => 1)
50
40
  result.should == 'result'
51
41
  end
52
42
  end
53
- end
54
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rcicindela
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jugyo
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-14 00:00:00 +09:00
12
+ date: 2009-05-21 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15