pilha 0.2.0 → 0.2.1

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.
@@ -31,7 +31,7 @@ Pilha aims to provide a ActiveRecord like interface for querying the StackExchan
31
31
 
32
32
  (The MIT License)
33
33
 
34
- Copyright (c) 2010-2010 Dalto Curvelano Junior
34
+ Copyright (c) 2010-2011 Dalto Curvelano Junior
35
35
 
36
36
  Permission is hereby granted, free of charge, to any person obtaining
37
37
  a copy of this software and associated documentation files (the
@@ -1,8 +1,6 @@
1
1
  path = File.expand_path(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
3
3
 
4
- require 'rubygems'
5
-
6
4
  require 'zlib'
7
5
  require 'json'
8
6
  require 'open-uri'
@@ -20,6 +18,9 @@ require 'pilha/stack_overflow/statistics'
20
18
 
21
19
  module StackExchange
22
20
  module StackOverflow
21
+
22
+ VERSION = '0.2.1'
23
+
23
24
  class Client
24
25
  URL = 'http://api.stackoverflow.com/'
25
26
  API_VERSION = '1.0'
@@ -29,34 +30,31 @@ module StackExchange
29
30
  attr_reader :api_key
30
31
 
31
32
  class << self
32
- def config &block
33
+
34
+ def config(&block)
33
35
  options = OpenStruct.new
34
36
  yield options if block_given?
35
- init_client! Client.new(options)
37
+ @client = Client.new(options)
36
38
  end
37
39
 
38
- def init_client!(client)
39
- base_eigenclass = class << Base; self; end
40
- base_eigenclass.send :define_method, :client do
41
- @client = client
42
- end
43
- client
40
+ def instance
41
+ @client
44
42
  end
43
+
45
44
  end
46
45
 
47
46
  def initialize(options = OpenStruct.new)
48
- @url = normalize(options.url || URL )
47
+ @url = normalize(options.url || URL)
49
48
  @api_version = options.api_version || API_VERSION
50
49
  @api_key = options.api_key
51
50
  end
52
51
 
53
52
  def api_method_path(pattern, options = {})
54
53
  pattern = normalize(pattern)
55
- parts = pattern.split('/').select { |part| part =~ /^:/ }
56
54
 
57
- parts.each do |part|
58
- key = part.sub(':', '').intern
59
- pattern.sub!(part, options[key].to_s)
55
+ pattern.scan(/:(\w+)/).each do |part|
56
+ val = part.first
57
+ pattern.sub!(":" + val, options[val.to_sym].to_s)
60
58
  end
61
59
 
62
60
  pattern
@@ -68,8 +66,7 @@ module StackExchange
68
66
  end
69
67
 
70
68
  def get(url)
71
- stream = open(url) { |stream| Zlib::GzipReader.new(stream).read }
72
- JSON.parse(stream)
69
+ JSON.parse(Zlib::GzipReader.new(open(url)).read)
73
70
  end
74
71
 
75
72
  def root_path
@@ -86,7 +83,7 @@ module StackExchange
86
83
  return '' unless params
87
84
 
88
85
  params = params.sort_by { |k, v| k.to_s }
89
- pairs = params.map { |key, value| "#{key}=#{value}" }
86
+ pairs = params.map { |k, v| "#{k}=#{v}" }
90
87
 
91
88
  '?' + pairs.join('&')
92
89
  end
@@ -3,16 +3,12 @@ module StackExchange
3
3
  class Base
4
4
 
5
5
  class << self
6
- def client
7
- @client ||= StackExchange::StackOverflow::Client.config
8
- end
9
-
10
6
  def parse_with_class(hash, key, klass)
11
7
  case hash[key]
12
- when Hash
13
- hash[key] = klass.new(hash[key])
14
- when Array
15
- hash[key] = hash[key].map { |value| klass.new(value) }
8
+ when Hash
9
+ hash[key] = klass.new(hash[key])
10
+ when Array
11
+ hash[key] = hash[key].map { |value| klass.new(value) }
16
12
  end
17
13
  end
18
14
 
@@ -20,6 +16,10 @@ module StackExchange
20
16
  options.merge! :id => id if id
21
17
  parse client.request(path_pattern, options)
22
18
  end
19
+
20
+ def client
21
+ StackExchange::StackOverflow::Client.instance
22
+ end
23
23
  end
24
24
 
25
25
  def initialize(hash)
@@ -28,13 +28,7 @@ module StackExchange
28
28
  end
29
29
 
30
30
  def find_by_tags(*tags)
31
- if tags.last.is_a? Hash
32
- options = tags.last
33
- tags = tags[0, tags.size - 1]
34
- else
35
- options = {}
36
- end
37
-
31
+ options = tags.last.is_a?(Hash) ? tags.pop : {}
38
32
  options.merge!(:conditions => { :tagged => tags.join('+') })
39
33
  request('/questions', nil, options)
40
34
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pilha
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 21
5
+ prerelease:
5
6
  segments:
6
7
  - 0
7
8
  - 2
8
- - 0
9
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - Dalto Curvelano Junior
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2011-02-13 00:00:00 -02:00
18
+ date: 2011-04-28 00:00:00 -03:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
@@ -25,6 +26,7 @@ dependencies:
25
26
  requirements:
26
27
  - - ">="
27
28
  - !ruby/object:Gem::Version
29
+ hash: 7
28
30
  segments:
29
31
  - 1
30
32
  - 4
@@ -40,6 +42,7 @@ dependencies:
40
42
  requirements:
41
43
  - - ">="
42
44
  - !ruby/object:Gem::Version
45
+ hash: 3
43
46
  segments:
44
47
  - 0
45
48
  version: "0"
@@ -53,6 +56,7 @@ dependencies:
53
56
  requirements:
54
57
  - - ">="
55
58
  - !ruby/object:Gem::Version
59
+ hash: 3
56
60
  segments:
57
61
  - 0
58
62
  version: "0"
@@ -67,85 +71,85 @@ extensions: []
67
71
  extra_rdoc_files: []
68
72
 
69
73
  files:
70
- - lib/pilha.rb
71
- - lib/pilha/stack_overflow/comment.rb
72
- - lib/pilha/stack_overflow/answer.rb
73
74
  - lib/pilha/stack_overflow/statistics.rb
74
- - lib/pilha/stack_overflow/badge.rb
75
75
  - lib/pilha/stack_overflow/question.rb
76
- - lib/pilha/stack_overflow/user.rb
77
- - lib/pilha/stack_overflow/base.rb
76
+ - lib/pilha/stack_overflow/comment.rb
78
77
  - lib/pilha/stack_overflow/tag.rb
78
+ - lib/pilha/stack_overflow/base.rb
79
+ - lib/pilha/stack_overflow/badge.rb
80
+ - lib/pilha/stack_overflow/answer.rb
81
+ - lib/pilha/stack_overflow/user.rb
82
+ - lib/pilha.rb
79
83
  - README.rdoc
84
+ - spec/pilha/stack_overflow/user_spec.rb
85
+ - spec/pilha/stack_overflow/answer_spec.rb
86
+ - spec/pilha/stack_overflow/statistics_spec.rb
87
+ - spec/pilha/stack_overflow/badge_spec.rb
88
+ - spec/pilha/stack_overflow/stack_overflow_spec.rb
89
+ - spec/pilha/stack_overflow/comment_spec.rb
90
+ - spec/pilha/stack_overflow/question_spec.rb
91
+ - spec/pilha/stack_overflow/tag_spec.rb
80
92
  - spec/spec_helper.rb
81
- - spec/fixtures/tags_by_user_id.json.gz
82
- - spec/fixtures/comments_by_user_id.json.gz
83
- - spec/fixtures/user_tags.json
84
- - spec/fixtures/questions_tagged_gwt_and_googleappengine.json
93
+ - spec/fixtures/badges.json
94
+ - spec/fixtures/comments.json
85
95
  - spec/fixtures/users.json
86
- - spec/fixtures/question_by_id_with_body.json.gz
87
- - spec/fixtures/badges_tag_based.json.gz
96
+ - spec/fixtures/comments_by_user_id.json
97
+ - spec/fixtures/answer_by_id_with_body.json.gz
98
+ - spec/fixtures/questions_tagged_gwt_and_googleappengine.json
99
+ - spec/fixtures/questions_by_user_id.json
88
100
  - spec/fixtures/comments_by_mentioned_user_id.json
89
- - spec/fixtures/tags_by_user_id.json
90
- - spec/fixtures/questions_tagged_ruby.json
91
- - spec/fixtures/questions_tagged_gwt_and_googleappengine.json.gz
92
101
  - spec/fixtures/users_answers.json
93
- - spec/fixtures/comments.json
94
- - spec/fixtures/comments_by_question_id.json.gz
95
- - spec/fixtures/answers_by_question_id.json
102
+ - spec/fixtures/answer_by_id.json.gz
103
+ - spec/fixtures/user_tags.json
96
104
  - spec/fixtures/questions_unanswered.json
97
- - spec/fixtures/questions_by_user_id.json
98
- - spec/fixtures/all_tags.json.gz
99
- - spec/fixtures/badges_by_id.json
100
- - spec/fixtures/comments_by_user_id.json
105
+ - spec/fixtures/tags_by_user_id.json.gz
106
+ - spec/fixtures/comments_by_user_to_mentioned_user.json.gz
107
+ - spec/fixtures/questions.json
108
+ - spec/fixtures/answers_by_question_id.json
101
109
  - spec/fixtures/questions_tagged_ruby.json.gz
102
- - spec/fixtures/badges.json.gz
103
- - spec/fixtures/answer_with_comments.json
104
- - spec/fixtures/questions.json.gz
105
- - spec/fixtures/users_by_id.json.gz
106
- - spec/fixtures/stats.json
107
- - spec/fixtures/answer_by_id_with_body.json
108
- - spec/fixtures/answer_comments.json
109
- - spec/fixtures/answer_with_comments.json.gz
110
- - spec/fixtures/badges_name.json
111
- - spec/fixtures/comments_by_user_id_to_mentioned_user_id.json.gz
112
110
  - spec/fixtures/all_tags.json
113
- - spec/fixtures/stats.json.gz
114
- - spec/fixtures/answer_by_id.json.gz
115
111
  - spec/fixtures/favorite_questions_by_user_id.json
116
- - spec/fixtures/users_answers.json.gz
112
+ - spec/fixtures/question_by_user_id.json.gz
113
+ - spec/fixtures/questions_tagged_gwt_and_googleappengine.json.gz
114
+ - spec/fixtures/badges_by_id.json
117
115
  - spec/fixtures/question_by_id.json
118
- - spec/fixtures/question_by_id.json.gz
119
- - spec/fixtures/comments.json.gz
120
- - spec/fixtures/comments_by_user_to_mentioned_user.json.gz
121
- - spec/fixtures/question_by_id_with_body.json
122
- - spec/fixtures/badges_tag_based.json
123
- - spec/fixtures/comments_by_user_to_mentioned_user_id.json.gz
124
- - spec/fixtures/badges.json
116
+ - spec/fixtures/tags_by_user_id.json
117
+ - spec/fixtures/questions_unanswered.json.gz
118
+ - spec/fixtures/stats.json.gz
125
119
  - spec/fixtures/favorite_questions_by_user_id.json.gz
120
+ - spec/fixtures/badges_name.json
121
+ - spec/fixtures/badges_by_id_page2.json
122
+ - spec/fixtures/comments_by_question_id.json.gz
123
+ - spec/fixtures/badges_name.json.gz
124
+ - spec/fixtures/answer_with_comments.json
125
+ - spec/fixtures/comments_by_user_to_mentioned_user_id.json.gz
126
+ - spec/fixtures/question_by_id_with_body.json.gz
127
+ - spec/fixtures/badges.json.gz
128
+ - spec/fixtures/answer_by_id_with_body.json
129
+ - spec/fixtures/badges_by_id_page2.json.gz
130
+ - spec/fixtures/users_by_id.json.gz
126
131
  - spec/fixtures/comments_by_mentioned_user_id.json.gz
127
- - spec/fixtures/answers_by_question_id.json.gz
128
- - spec/fixtures/users_by_id.json
129
- - spec/fixtures/comments_by_question_id.json
132
+ - spec/fixtures/questions_tagged_ruby.json
133
+ - spec/fixtures/stats.json
134
+ - spec/fixtures/users_answers.json.gz
135
+ - spec/fixtures/answer_comments.json
130
136
  - spec/fixtures/comments_by_user_to_mentioned_user.json
131
- - spec/fixtures/badges_by_id_page2.json
132
- - spec/fixtures/answer_by_id_with_body.json.gz
137
+ - spec/fixtures/comments_by_question_id.json
138
+ - spec/fixtures/all_tags.json.gz
139
+ - spec/fixtures/badges_tag_based.json.gz
133
140
  - spec/fixtures/questions_by_user_id.json.gz
134
- - spec/fixtures/badges_by_id_page2.json.gz
135
- - spec/fixtures/badges_name.json.gz
136
- - spec/fixtures/questions_unanswered.json.gz
141
+ - spec/fixtures/comments_by_user_id_to_mentioned_user_id.json.gz
142
+ - spec/fixtures/badges_tag_based.json
137
143
  - spec/fixtures/badges_by_id.json.gz
138
144
  - spec/fixtures/answer_by_id.json
139
- - spec/fixtures/questions.json
140
- - spec/fixtures/question_by_user_id.json.gz
141
- - spec/pilha/stack_overflow/badge_spec.rb
142
- - spec/pilha/stack_overflow/user_spec.rb
143
- - spec/pilha/stack_overflow/statistics_spec.rb
144
- - spec/pilha/stack_overflow/comment_spec.rb
145
- - spec/pilha/stack_overflow/tag_spec.rb
146
- - spec/pilha/stack_overflow/answer_spec.rb
147
- - spec/pilha/stack_overflow/question_spec.rb
148
- - spec/pilha/stack_overflow/stack_overflow_spec.rb
145
+ - spec/fixtures/answer_with_comments.json.gz
146
+ - spec/fixtures/comments.json.gz
147
+ - spec/fixtures/question_by_id_with_body.json
148
+ - spec/fixtures/comments_by_user_id.json.gz
149
+ - spec/fixtures/answers_by_question_id.json.gz
150
+ - spec/fixtures/question_by_id.json.gz
151
+ - spec/fixtures/questions.json.gz
152
+ - spec/fixtures/users_by_id.json
149
153
  has_rdoc: true
150
154
  homepage: http://github.com/dlt/pilha
151
155
  licenses:
@@ -160,6 +164,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
160
164
  requirements:
161
165
  - - ">="
162
166
  - !ruby/object:Gem::Version
167
+ hash: 3
163
168
  segments:
164
169
  - 0
165
170
  version: "0"
@@ -168,83 +173,84 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
173
  requirements:
169
174
  - - ">="
170
175
  - !ruby/object:Gem::Version
176
+ hash: 3
171
177
  segments:
172
178
  - 0
173
179
  version: "0"
174
180
  requirements: []
175
181
 
176
182
  rubyforge_project:
177
- rubygems_version: 1.3.7
183
+ rubygems_version: 1.5.2
178
184
  signing_key:
179
185
  specification_version: 3
180
186
  summary: A ruby wrapper to the Stack Exchange (Stack Overflow and friends) API.
181
187
  test_files:
188
+ - spec/pilha/stack_overflow/user_spec.rb
189
+ - spec/pilha/stack_overflow/answer_spec.rb
190
+ - spec/pilha/stack_overflow/statistics_spec.rb
191
+ - spec/pilha/stack_overflow/badge_spec.rb
192
+ - spec/pilha/stack_overflow/stack_overflow_spec.rb
193
+ - spec/pilha/stack_overflow/comment_spec.rb
194
+ - spec/pilha/stack_overflow/question_spec.rb
195
+ - spec/pilha/stack_overflow/tag_spec.rb
182
196
  - spec/spec_helper.rb
183
- - spec/fixtures/tags_by_user_id.json.gz
184
- - spec/fixtures/comments_by_user_id.json.gz
185
- - spec/fixtures/user_tags.json
186
- - spec/fixtures/questions_tagged_gwt_and_googleappengine.json
197
+ - spec/fixtures/badges.json
198
+ - spec/fixtures/comments.json
187
199
  - spec/fixtures/users.json
188
- - spec/fixtures/question_by_id_with_body.json.gz
189
- - spec/fixtures/badges_tag_based.json.gz
200
+ - spec/fixtures/comments_by_user_id.json
201
+ - spec/fixtures/answer_by_id_with_body.json.gz
202
+ - spec/fixtures/questions_tagged_gwt_and_googleappengine.json
203
+ - spec/fixtures/questions_by_user_id.json
190
204
  - spec/fixtures/comments_by_mentioned_user_id.json
191
- - spec/fixtures/tags_by_user_id.json
192
- - spec/fixtures/questions_tagged_ruby.json
193
- - spec/fixtures/questions_tagged_gwt_and_googleappengine.json.gz
194
205
  - spec/fixtures/users_answers.json
195
- - spec/fixtures/comments.json
196
- - spec/fixtures/comments_by_question_id.json.gz
197
- - spec/fixtures/answers_by_question_id.json
206
+ - spec/fixtures/answer_by_id.json.gz
207
+ - spec/fixtures/user_tags.json
198
208
  - spec/fixtures/questions_unanswered.json
199
- - spec/fixtures/questions_by_user_id.json
200
- - spec/fixtures/all_tags.json.gz
201
- - spec/fixtures/badges_by_id.json
202
- - spec/fixtures/comments_by_user_id.json
209
+ - spec/fixtures/tags_by_user_id.json.gz
210
+ - spec/fixtures/comments_by_user_to_mentioned_user.json.gz
211
+ - spec/fixtures/questions.json
212
+ - spec/fixtures/answers_by_question_id.json
203
213
  - spec/fixtures/questions_tagged_ruby.json.gz
204
- - spec/fixtures/badges.json.gz
205
- - spec/fixtures/answer_with_comments.json
206
- - spec/fixtures/questions.json.gz
207
- - spec/fixtures/users_by_id.json.gz
208
- - spec/fixtures/stats.json
209
- - spec/fixtures/answer_by_id_with_body.json
210
- - spec/fixtures/answer_comments.json
211
- - spec/fixtures/answer_with_comments.json.gz
212
- - spec/fixtures/badges_name.json
213
- - spec/fixtures/comments_by_user_id_to_mentioned_user_id.json.gz
214
214
  - spec/fixtures/all_tags.json
215
- - spec/fixtures/stats.json.gz
216
- - spec/fixtures/answer_by_id.json.gz
217
215
  - spec/fixtures/favorite_questions_by_user_id.json
218
- - spec/fixtures/users_answers.json.gz
216
+ - spec/fixtures/question_by_user_id.json.gz
217
+ - spec/fixtures/questions_tagged_gwt_and_googleappengine.json.gz
218
+ - spec/fixtures/badges_by_id.json
219
219
  - spec/fixtures/question_by_id.json
220
- - spec/fixtures/question_by_id.json.gz
221
- - spec/fixtures/comments.json.gz
222
- - spec/fixtures/comments_by_user_to_mentioned_user.json.gz
223
- - spec/fixtures/question_by_id_with_body.json
224
- - spec/fixtures/badges_tag_based.json
225
- - spec/fixtures/comments_by_user_to_mentioned_user_id.json.gz
226
- - spec/fixtures/badges.json
220
+ - spec/fixtures/tags_by_user_id.json
221
+ - spec/fixtures/questions_unanswered.json.gz
222
+ - spec/fixtures/stats.json.gz
227
223
  - spec/fixtures/favorite_questions_by_user_id.json.gz
224
+ - spec/fixtures/badges_name.json
225
+ - spec/fixtures/badges_by_id_page2.json
226
+ - spec/fixtures/comments_by_question_id.json.gz
227
+ - spec/fixtures/badges_name.json.gz
228
+ - spec/fixtures/answer_with_comments.json
229
+ - spec/fixtures/comments_by_user_to_mentioned_user_id.json.gz
230
+ - spec/fixtures/question_by_id_with_body.json.gz
231
+ - spec/fixtures/badges.json.gz
232
+ - spec/fixtures/answer_by_id_with_body.json
233
+ - spec/fixtures/badges_by_id_page2.json.gz
234
+ - spec/fixtures/users_by_id.json.gz
228
235
  - spec/fixtures/comments_by_mentioned_user_id.json.gz
229
- - spec/fixtures/answers_by_question_id.json.gz
230
- - spec/fixtures/users_by_id.json
231
- - spec/fixtures/comments_by_question_id.json
236
+ - spec/fixtures/questions_tagged_ruby.json
237
+ - spec/fixtures/stats.json
238
+ - spec/fixtures/users_answers.json.gz
239
+ - spec/fixtures/answer_comments.json
232
240
  - spec/fixtures/comments_by_user_to_mentioned_user.json
233
- - spec/fixtures/badges_by_id_page2.json
234
- - spec/fixtures/answer_by_id_with_body.json.gz
241
+ - spec/fixtures/comments_by_question_id.json
242
+ - spec/fixtures/all_tags.json.gz
243
+ - spec/fixtures/badges_tag_based.json.gz
235
244
  - spec/fixtures/questions_by_user_id.json.gz
236
- - spec/fixtures/badges_by_id_page2.json.gz
237
- - spec/fixtures/badges_name.json.gz
238
- - spec/fixtures/questions_unanswered.json.gz
245
+ - spec/fixtures/comments_by_user_id_to_mentioned_user_id.json.gz
246
+ - spec/fixtures/badges_tag_based.json
239
247
  - spec/fixtures/badges_by_id.json.gz
240
248
  - spec/fixtures/answer_by_id.json
241
- - spec/fixtures/questions.json
242
- - spec/fixtures/question_by_user_id.json.gz
243
- - spec/pilha/stack_overflow/badge_spec.rb
244
- - spec/pilha/stack_overflow/user_spec.rb
245
- - spec/pilha/stack_overflow/statistics_spec.rb
246
- - spec/pilha/stack_overflow/comment_spec.rb
247
- - spec/pilha/stack_overflow/tag_spec.rb
248
- - spec/pilha/stack_overflow/answer_spec.rb
249
- - spec/pilha/stack_overflow/question_spec.rb
250
- - spec/pilha/stack_overflow/stack_overflow_spec.rb
249
+ - spec/fixtures/answer_with_comments.json.gz
250
+ - spec/fixtures/comments.json.gz
251
+ - spec/fixtures/question_by_id_with_body.json
252
+ - spec/fixtures/comments_by_user_id.json.gz
253
+ - spec/fixtures/answers_by_question_id.json.gz
254
+ - spec/fixtures/question_by_id.json.gz
255
+ - spec/fixtures/questions.json.gz
256
+ - spec/fixtures/users_by_id.json