rubytter 0.6.2 → 0.6.3
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/lib/rubytter.rb +9 -9
- data/spec/rubytter_spec.rb +5 -5
- metadata +1 -1
data/lib/rubytter.rb
CHANGED
@@ -16,7 +16,7 @@ class Rubytter
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
VERSION = '0.6.
|
19
|
+
VERSION = '0.6.3'
|
20
20
|
|
21
21
|
attr_reader :login
|
22
22
|
attr_accessor :host, :header
|
@@ -103,27 +103,27 @@ class Rubytter
|
|
103
103
|
|
104
104
|
def get(path, params = {})
|
105
105
|
path += '.json'
|
106
|
-
param_str = '?' + to_param_str(params)
|
106
|
+
param_str = '?' + self.class.to_param_str(params)
|
107
107
|
path = path + param_str unless param_str.empty?
|
108
108
|
req = create_request(Net::HTTP::Get.new(path))
|
109
|
-
json_to_struct(http_request(@host, req))
|
109
|
+
self.class.json_to_struct(http_request(@host, req))
|
110
110
|
end
|
111
111
|
|
112
112
|
def post(path, params = {})
|
113
113
|
path += '.json'
|
114
|
-
param_str = to_param_str(params)
|
114
|
+
param_str = self.class.to_param_str(params)
|
115
115
|
req = create_request(Net::HTTP::Post.new(path))
|
116
|
-
json_to_struct(http_request(@host, req, param_str))
|
116
|
+
self.class.json_to_struct(http_request(@host, req, param_str))
|
117
117
|
end
|
118
118
|
alias delete post
|
119
119
|
|
120
120
|
def search(query, params = {})
|
121
121
|
path = '/search.json'
|
122
|
-
param_str = '?' + to_param_str(params.merge({:q => query}))
|
122
|
+
param_str = '?' + self.class.to_param_str(params.merge({:q => query}))
|
123
123
|
path = path + param_str unless param_str.empty?
|
124
124
|
req = create_request(Net::HTTP::Get.new(path), false)
|
125
125
|
json_data = http_request("search.#{@host}", req)
|
126
|
-
json_to_struct(
|
126
|
+
self.class.json_to_struct(
|
127
127
|
json_data['results'].map do |result|
|
128
128
|
search_result_to_struct(result)
|
129
129
|
end
|
@@ -171,7 +171,7 @@ class Rubytter
|
|
171
171
|
req
|
172
172
|
end
|
173
173
|
|
174
|
-
def json_to_struct(json)
|
174
|
+
def self.json_to_struct(json)
|
175
175
|
case json
|
176
176
|
when Array
|
177
177
|
json.map{|i| json_to_struct(i)}
|
@@ -193,7 +193,7 @@ class Rubytter
|
|
193
193
|
end
|
194
194
|
end
|
195
195
|
|
196
|
-
def to_param_str(hash)
|
196
|
+
def self.to_param_str(hash)
|
197
197
|
raise ArgumentError, 'Argument must be a Hash object' unless hash.is_a?(Hash)
|
198
198
|
hash.to_a.map{|i| i[0].to_s + '=' + CGI.escape(i[1].to_s) }.join('&')
|
199
199
|
end
|
data/spec/rubytter_spec.rb
CHANGED
@@ -129,16 +129,16 @@ class Rubytter
|
|
129
129
|
end
|
130
130
|
|
131
131
|
it 'should respond to to_param_str' do
|
132
|
-
param_str =
|
132
|
+
param_str = Rubytter.to_param_str(:page => 2, :foo => 'bar')
|
133
133
|
param_str.should =~ /^.+?=.+?&.+?=.+?$/
|
134
134
|
param_str.should =~ /page=2/
|
135
135
|
param_str.should =~ /foo=bar/
|
136
136
|
end
|
137
137
|
|
138
138
|
it 'should raise when call to_param_str with invalid arg' do
|
139
|
-
lambda {
|
140
|
-
lambda {
|
141
|
-
lambda {
|
139
|
+
lambda { Rubytter.to_param_str(nil) }.should raise_error(ArgumentError)
|
140
|
+
lambda { Rubytter.to_param_str('foo') }.should raise_error(ArgumentError)
|
141
|
+
lambda { Rubytter.to_param_str(:bar) }.should raise_error(ArgumentError)
|
142
142
|
end
|
143
143
|
|
144
144
|
it 'should set default header' do
|
@@ -180,7 +180,7 @@ class Rubytter
|
|
180
180
|
:d => {:a => {:a => 1, :b => 2}, :b => 1},
|
181
181
|
:e => [{:a => 1, :b => 2}, {:c => 3}]
|
182
182
|
}
|
183
|
-
struct =
|
183
|
+
struct = Rubytter.json_to_struct(hash)
|
184
184
|
struct.a.should == 'a'
|
185
185
|
struct.b.should == 1
|
186
186
|
struct.c.a.should == 1
|