rubytter 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/rubytter.rb +12 -12
  2. data/spec/rubytter_spec.rb +41 -23
  3. metadata +1 -1
data/lib/rubytter.rb CHANGED
@@ -10,7 +10,7 @@ class Rubytter
10
10
  class APIError < StandardError; end
11
11
 
12
12
  APP_NAME = 'Rubytter'
13
- VERSION = '0.4.4'
13
+ VERSION = '0.4.5'
14
14
  HOMEPAGE = 'http://github.com/jugyo/rubytter'
15
15
 
16
16
  def initialize(login, password, options = {})
@@ -64,13 +64,13 @@ class Rubytter
64
64
  if /%s$/ =~ path
65
65
  eval <<-EOS
66
66
  def #{method}(id, params = {})
67
- #{http_method}('#{path}' % id, params)
67
+ #{http_method}(@host, '#{path}' % id, params)
68
68
  end
69
69
  EOS
70
70
  else
71
71
  eval <<-EOS
72
72
  def #{method}(params = {})
73
- #{http_method}('#{path}', params)
73
+ #{http_method}(@host, '#{path}', params)
74
74
  end
75
75
  EOS
76
76
  end
@@ -91,30 +91,29 @@ class Rubytter
91
91
  when Hash
92
92
  arg
93
93
  end
94
- path = '/search.json?' + to_param_str(params)
95
- req = create_request(Net::HTTP::Post.new(path))
96
- http_request(req, nil, "search.#{@host}")
94
+ get("search.#{@host}", '/search', params)
97
95
  end
98
96
 
99
- def get(path, params = {})
97
+ def get(host, path, params = {})
98
+ host ||= @host
100
99
  path += '.json'
101
100
  param_str = '?' + to_param_str(params)
102
101
  path = path + param_str unless param_str.empty?
103
102
  req = create_request(Net::HTTP::Get.new(path))
104
- http_request(req)
103
+ http_request(host, req)
105
104
  end
106
105
 
107
- def post(path, params = {})
106
+ def post(host, path, params = {})
107
+ host ||= @host
108
108
  path += '.json'
109
109
  param_str = to_param_str(params)
110
110
  req = create_request(Net::HTTP::Post.new(path))
111
- http_request(req, param_str)
111
+ http_request(host, req, param_str)
112
112
  end
113
113
 
114
114
  alias delete post
115
115
 
116
- def http_request(req, param_str = nil, host = nil)
117
- host ||= @host
116
+ def http_request(host, req, param_str = nil)
118
117
  res = @connection.start(host) do |http|
119
118
  if param_str
120
119
  http.request(req, param_str)
@@ -160,6 +159,7 @@ class Rubytter
160
159
  end
161
160
 
162
161
  def to_param_str(hash)
162
+ raise ArgumentError, 'Argument must be a Hash object' unless hash.is_a?(Hash)
163
163
  hash.to_a.map{|i| i[0].to_s + '=' + CGI.escape(i[1].to_s) }.join('&')
164
164
  end
165
165
  end
@@ -20,106 +20,118 @@ class Rubytter
20
20
 
21
21
  it 'should get or post' do
22
22
  # TODO: split specs
23
- @rubytter.should_receive(:get).with('/statuses/replies', {})
23
+ @rubytter.should_receive(:get).with('twitter.com', '/statuses/replies', {})
24
24
  @rubytter.replies
25
25
 
26
- @rubytter.should_receive(:get).with('/statuses/replies', {:page => 2})
26
+ @rubytter.should_receive(:get).with('twitter.com', '/statuses/replies', {:page => 2})
27
27
  @rubytter.replies(:page => 2)
28
28
 
29
- @rubytter.should_receive(:get).with('/statuses/user_timeline/1', {})
29
+ @rubytter.should_receive(:get).with('twitter.com', '/statuses/user_timeline/1', {})
30
30
  @rubytter.user_timeline(1)
31
31
 
32
- @rubytter.should_receive(:get).with('/users/show/1', {})
32
+ @rubytter.should_receive(:get).with('twitter.com', '/users/show/1', {})
33
33
  @rubytter.user(1)
34
34
 
35
- @rubytter.should_receive(:delete).with('/statuses/destroy/1', {})
35
+ @rubytter.should_receive(:delete).with('twitter.com', '/statuses/destroy/1', {})
36
36
  @rubytter.remove_status(1)
37
37
  end
38
38
 
39
39
  # direct_messages
40
40
 
41
41
  it 'should respond to direct_messages' do
42
- @rubytter.should_receive(:get).with('/direct_messages', {})
42
+ @rubytter.should_receive(:get).with('twitter.com', '/direct_messages', {})
43
43
  @rubytter.direct_messages()
44
44
  end
45
45
 
46
46
  it 'should respond to sent_direct_messages' do
47
- @rubytter.should_receive(:get).with('/direct_messages/sent', {})
47
+ @rubytter.should_receive(:get).with('twitter.com', '/direct_messages/sent', {})
48
48
  @rubytter.sent_direct_messages()
49
49
  end
50
50
 
51
51
  it 'should respond to send_direct_message' do
52
- @rubytter.should_receive(:post).with('/direct_messages/new', {})
52
+ @rubytter.should_receive(:post).with('twitter.com', '/direct_messages/new', {})
53
53
  @rubytter.send_direct_message()
54
54
  end
55
55
 
56
56
  it 'should respond to destroy_direct_message' do
57
- @rubytter.should_receive(:delete).with('/direct_messages/destroy/1', {})
57
+ @rubytter.should_receive(:delete).with('twitter.com', '/direct_messages/destroy/1', {})
58
58
  @rubytter.remove_direct_message(1)
59
59
  end
60
60
 
61
61
  it 'should respond to direct_message' do
62
- @rubytter.should_receive(:post).with('/direct_messages/new', {:user => 'test', :text => 'aaaaaaaaaaaaa'})
62
+ @rubytter.should_receive(:post).with('twitter.com', '/direct_messages/new', {:user => 'test', :text => 'aaaaaaaaaaaaa'})
63
63
  @rubytter.direct_message('test', 'aaaaaaaaaaaaa')
64
64
  end
65
65
 
66
66
  # statuses
67
67
 
68
68
  it 'should respond to update' do
69
- @rubytter.should_receive(:post).with('/statuses/update', {:status => 'test'})
69
+ @rubytter.should_receive(:post).with('twitter.com', '/statuses/update', {:status => 'test'})
70
70
  @rubytter.update('test')
71
71
  end
72
72
 
73
73
  it 'should respond to update_status' do
74
- @rubytter.should_receive(:post).with('/statuses/update', {:status => 'test'})
74
+ @rubytter.should_receive(:post).with('twitter.com', '/statuses/update', {:status => 'test'})
75
75
  @rubytter.update_status(:status => 'test')
76
76
  end
77
77
 
78
78
  # friendship
79
79
 
80
80
  it 'should respond to follow' do
81
- @rubytter.should_receive(:post).with('/friendships/create/test', {})
81
+ @rubytter.should_receive(:post).with('twitter.com', '/friendships/create/test', {})
82
82
  @rubytter.follow('test')
83
83
  end
84
84
 
85
85
  it 'should respond to leave' do
86
- @rubytter.should_receive(:delete).with('/friendships/destroy/test', {})
86
+ @rubytter.should_receive(:delete).with('twitter.com', '/friendships/destroy/test', {})
87
87
  @rubytter.leave('test')
88
88
  end
89
89
 
90
90
  it 'should respond to friendship_exists' do
91
- @rubytter.should_receive(:get).with('/friendships/exists', {:user_a => 'a', :user_b => 'b'})
91
+ @rubytter.should_receive(:get).with('twitter.com', '/friendships/exists', {:user_a => 'a', :user_b => 'b'})
92
92
  @rubytter.friendship_exists(:user_a => 'a', :user_b => 'b')
93
93
  end
94
94
 
95
95
  # Social Graph Methods
96
96
 
97
97
  it 'should respond to followers_ids' do
98
- @rubytter.should_receive(:get).with('/friends/ids/test', {})
98
+ @rubytter.should_receive(:get).with('twitter.com', '/friends/ids/test', {})
99
99
  @rubytter.friends_ids('test')
100
100
  end
101
101
 
102
102
  it 'should respond to followers_ids' do
103
- @rubytter.should_receive(:get).with('/followers/ids/test', {})
103
+ @rubytter.should_receive(:get).with('twitter.com', '/followers/ids/test', {})
104
104
  @rubytter.followers_ids('test')
105
105
  end
106
106
 
107
107
  it 'should respond to http_request' do
108
- @rubytter.should_receive(:http_request) {|req, param_str| param_str.should == 'status=test'}
108
+ @rubytter.should_receive(:http_request) {|host, req, param_str| param_str.should == 'status=test'}
109
109
  @rubytter.update_status(:status => 'test')
110
110
  end
111
111
 
112
- it 'should respond to search' do
113
- @rubytter.should_receive(:http_request) do |req, param_str, host|
112
+ it 'should respond to search (1)' do
113
+ @rubytter.should_receive(:get).with('search.twitter.com', '/search', {:q => 'test'})
114
+ @rubytter.search('test')
115
+ end
116
+
117
+ it 'should respond to search (2)' do
118
+ @rubytter.should_receive(:http_request) do |host, req, param_str|
114
119
  req.path.should == '/search.json?q=test'
115
120
  host.should == 'search.twitter.com'
116
121
  end
117
122
  @rubytter.search('test')
118
123
  end
119
124
 
120
- it 'should respond to search with params' do
121
- @rubytter.should_receive(:http_request) do |req, param_str, host|
122
- req.path.should == '/search.json?lang=ja&q=test'
125
+ it 'should respond to search with params (1)' do
126
+ @rubytter.should_receive(:get).with('search.twitter.com', "/search", {:q=>"test", :lang=>"ja"})
127
+ @rubytter.search(:q => 'test', :lang => 'ja')
128
+ end
129
+
130
+ it 'should respond to search with params (2)' do
131
+ @rubytter.should_receive(:http_request) do |host, req, param_str|
132
+ req.path.should =~ /\/search.json\?/
133
+ req.path.should =~ /q=test/
134
+ req.path.should =~ /lang=ja/
123
135
  end
124
136
  @rubytter.search(:q => 'test', :lang => 'ja')
125
137
  end
@@ -128,6 +140,12 @@ class Rubytter
128
140
  @rubytter.to_param_str(:page => 2, :foo => 'bar').should == 'foo=bar&page=2'
129
141
  end
130
142
 
143
+ it 'should raise when call to_param_str with invalid arg' do
144
+ lambda { @rubytter.to_param_str(nil) }.should raise_error ArgumentError
145
+ lambda { @rubytter.to_param_str('foo') }.should raise_error ArgumentError
146
+ lambda { @rubytter.to_param_str(:bar) }.should raise_error ArgumentError
147
+ end
148
+
131
149
  it 'should create struct from json' do
132
150
  hash = {
133
151
  :a => 'a',
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubytter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - jugyo