jugyo-rubytter 0.8.0 → 0.9.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 (3) hide show
  1. data/lib/rubytter.rb +35 -52
  2. data/spec/rubytter_spec.rb +25 -9
  3. metadata +3 -2
data/lib/rubytter.rb CHANGED
@@ -9,7 +9,7 @@ require 'rubytter/oauth_rubytter'
9
9
 
10
10
  class Rubytter
11
11
 
12
- VERSION = '0.8.0'
12
+ VERSION = '0.9.0'
13
13
 
14
14
  class APIError < StandardError
15
15
  attr_reader :response
@@ -107,14 +107,14 @@ class Rubytter
107
107
  param_str = '?' + self.class.to_param_str(params)
108
108
  path = path + param_str unless param_str.empty?
109
109
  req = create_request(Net::HTTP::Get.new(path))
110
- self.class.json_to_struct(http_request(@host, req))
110
+ self.class.structize(http_request(@host, req))
111
111
  end
112
112
 
113
113
  def post(path, params = {})
114
114
  path += '.json'
115
115
  param_str = self.class.to_param_str(params)
116
116
  req = create_request(Net::HTTP::Post.new(path))
117
- self.class.json_to_struct(http_request(@host, req, param_str))
117
+ self.class.structize(http_request(@host, req, param_str))
118
118
  end
119
119
  alias delete post
120
120
 
@@ -124,7 +124,7 @@ class Rubytter
124
124
  path = path + param_str unless param_str.empty?
125
125
  req = create_request(Net::HTTP::Get.new(path), false)
126
126
  json_data = http_request("search.#{@host}", req)
127
- self.class.json_to_struct(
127
+ self.class.structize(
128
128
  json_data['results'].map do |result|
129
129
  self.class.search_result_to_hash(result)
130
130
  end
@@ -172,29 +172,45 @@ class Rubytter
172
172
  req
173
173
  end
174
174
 
175
- def self.json_to_struct(json)
176
- case json
175
+ def self.structize(data)
176
+ case data
177
177
  when Array
178
- json.map{|i| json_to_struct(i)}
178
+ data.map{|i| structize(i)}
179
179
  when Hash
180
- struct_values = {}
181
- json.each do |k, v|
182
- case k
183
- when String, Symbol
184
- struct_values[k.to_sym] = json_to_struct(v)
180
+ class << data
181
+ def id
182
+ self[:id]
183
+ end
184
+
185
+ def method_missing(name, *args)
186
+ self[name]
187
+ end
188
+
189
+ def symbolize_keys!
190
+ each do |key, value|
191
+ self[(key.to_sym rescue key) || key] = value
192
+ end
193
+
194
+ self
185
195
  end
186
196
  end
187
- unless struct_values.empty?
188
- get_struct(struct_values.keys).new(*struct_values.values)
189
- else
190
- nil
197
+
198
+ data.keys.each do |k|
199
+ case k
200
+ when String, Symbol # String しかまず来ないだろうからこの判定はいらない気もするなぁ
201
+ data[k] = structize(data[k])
202
+ else
203
+ data.delete(k)
204
+ end
191
205
  end
206
+
207
+ data.symbolize_keys!
192
208
  else
193
- case json
209
+ case data
194
210
  when String
195
- CGI.unescapeHTML(json)
211
+ CGI.unescapeHTML(data) # ここで unescapeHTML すべきか悩むところではある
196
212
  else
197
- json
213
+ data
198
214
  end
199
215
  end
200
216
  end
@@ -203,37 +219,4 @@ class Rubytter
203
219
  raise ArgumentError, 'Argument must be a Hash object' unless hash.is_a?(Hash)
204
220
  hash.to_a.map{|i| i[0].to_s + '=' + CGI.escape(i[1].to_s) }.join('&')
205
221
  end
206
-
207
- def self.get_struct(keys)
208
- @@structs ||= {}
209
- unless @@structs.has_key?(keys)
210
- struct = Struct.new(*keys)
211
- struct.class_eval do
212
- def method_missing(*args, &block)
213
- nil
214
- end
215
-
216
- def to_hash(escape = false)
217
- hash = {}
218
- self.members.each do |member|
219
- value = self[member]
220
- if value.respond_to?(:to_hash)
221
- hash[member] = value.to_hash(escape)
222
- elsif value.is_a?(Array)
223
- hash[member] = value.map{ |i| i.to_hash(escape) }
224
- else
225
- hash[member] = escape && value.is_a?(String) ? CGI.escapeHTML(value) : value
226
- end
227
- end
228
- hash
229
- end
230
-
231
- def to_json(escape = false)
232
- to_hash(escape).to_json
233
- end
234
- end
235
- @@structs[keys] = struct
236
- end
237
- @@structs[keys]
238
- end
239
222
  end
@@ -169,7 +169,7 @@ class Rubytter
169
169
  rubytter.header.should == {'User-Agent' => 'foo'}
170
170
  end
171
171
 
172
- it 'should create struct from json' do
172
+ it 'should create struct from json(Hash)' do
173
173
  hash = {
174
174
  :a => 'a',
175
175
  'b' => 1,
@@ -180,7 +180,7 @@ class Rubytter
180
180
  :d => {:a => {:a => 1, :b => 2}, :b => 1},
181
181
  :e => [{:a => 1, :b => 2}, {:c => '&quot;&lt;&gt;&amp;'}]
182
182
  }
183
- struct = Rubytter.json_to_struct(hash)
183
+ struct = Rubytter.structize(hash)
184
184
  struct.a.should == 'a'
185
185
  struct.b.should == 1
186
186
  struct.c.a.should == 1
@@ -193,7 +193,20 @@ class Rubytter
193
193
  struct.regex.should == nil
194
194
  end
195
195
 
196
+ it 'should create struct from json(Array)' do
197
+ data = [
198
+ {"status" => {"text" => "foo", "user" => {"screen_name" => "jugyo_foo"}}},
199
+ {"status" => {"text" => "bar", "user" => {"screen_name" => "jugyo_bar"}}},
200
+ ]
201
+ struct = Rubytter.structize(data)
202
+ struct[0].status.text.should == 'foo'
203
+ struct[0].status.user.screen_name.should == 'jugyo_foo'
204
+ struct[1].status.text.should == 'bar'
205
+ struct[1].status.user.screen_name.should == 'jugyo_bar'
206
+ end
207
+
196
208
  it 'should convert struct to hash' do
209
+ pending
197
210
  hash = {
198
211
  :a => 'a',
199
212
  'b' => 1,
@@ -204,19 +217,21 @@ class Rubytter
204
217
  :d => {:a => {:a => 1, :b => 2}, :b => 1},
205
218
  :e => [{:a => 1, :b => 2}, {:c => '&quot;&lt;&gt;&amp;'}]
206
219
  }
207
- struct = Rubytter.json_to_struct(hash)
220
+ struct = Rubytter.structize(hash)
208
221
  struct.to_hash.should == {"a"=>"a", "b"=>1, "c"=>{"a"=>1, "b"=>2}, "d"=>{"a"=>{"a"=>1, "b"=>2}, "b"=>1}, "e"=>[{"a"=>1, "b"=>2}, {"c"=>"\"<>&"}]}
209
222
  end
210
223
 
211
224
  it 'should convert struct to hash with escape as HTML' do
225
+ pending
212
226
  hash = {
213
227
  :e => [{:a => 1, :b => 2}, {:c => '&quot;&lt;&gt;&amp;'}]
214
228
  }
215
- struct = Rubytter.json_to_struct(hash)
229
+ struct = Rubytter.structize(hash)
216
230
  struct.to_hash(true).should == {"e"=>[{"a"=>1, "b"=>2}, {"c"=>"&quot;&lt;&gt;&amp;"}]}
217
231
  end
218
232
 
219
233
  it 'should convert struct to json' do
234
+ pending
220
235
  hash = {
221
236
  :a => 'a',
222
237
  'b' => 1,
@@ -227,22 +242,23 @@ class Rubytter
227
242
  :d => {:a => {:a => 1, :b => 2}, :b => 1},
228
243
  :e => [{:a => 1, :b => 2}, {:c => '&quot;&lt;&gt;&amp;'}]
229
244
  }
230
- struct = Rubytter.json_to_struct(hash)
245
+ struct = Rubytter.structize(hash)
231
246
  struct.to_json.should == '{"a":"a","b":1,"c":{"a":1,"b":2},"d":{"a":{"a":1,"b":2},"b":1},"e":[{"a":1,"b":2},{"c":"\"<>&"}]}'
232
247
  end
233
248
 
234
249
  it 'should convert struct to json with escape as HTML' do
250
+ pending
235
251
  hash = {
236
252
  :e => [{:a => 1, :b => 2}, {:c => '&quot;&lt;&gt;&amp;'}]
237
253
  }
238
- struct = Rubytter.json_to_struct(hash)
254
+ struct = Rubytter.structize(hash)
239
255
  struct.to_json(true).should == '{"e":[{"a":1,"b":2},{"c":"&quot;&lt;&gt;&amp;"}]}'
240
256
  end
241
257
 
242
258
  it 'should create same structs from same datas' do
243
- Rubytter.json_to_struct({:a => 'a'}).should == Rubytter.json_to_struct({:a => 'a'})
244
- Rubytter.json_to_struct({:a => 'a', :b => {:c => 'c'}}).should ==
245
- Rubytter.json_to_struct({:a => 'a', :b => {:c => 'c'}})
259
+ Rubytter.structize({:a => 'a'}).should == Rubytter.structize({:a => 'a'})
260
+ Rubytter.structize({:a => 'a', :b => {:c => 'c'}}).should ==
261
+ Rubytter.structize({:a => 'a', :b => {:c => 'c'}})
246
262
  end
247
263
 
248
264
  it 'should be set app_name' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jugyo-rubytter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jugyo
@@ -52,6 +52,7 @@ files:
52
52
  - Rakefile
53
53
  has_rdoc: true
54
54
  homepage: http://github.com/jugyo/rubytter
55
+ licenses:
55
56
  post_install_message:
56
57
  rdoc_options:
57
58
  - --main
@@ -75,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
76
  requirements: []
76
77
 
77
78
  rubyforge_project: rubytter
78
- rubygems_version: 1.2.0
79
+ rubygems_version: 1.3.5
79
80
  signing_key:
80
81
  specification_version: 2
81
82
  summary: Simple twitter client.