csdn-tire 0.5

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.
@@ -0,0 +1,74 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ module Tire
5
+
6
+ class ConfigurationTest < Test::Unit::TestCase
7
+
8
+ def teardown
9
+ Tire::Configuration.reset
10
+ ENV['ELASTICSEARCH_URL'] = nil
11
+ end
12
+
13
+ context "Configuration" do
14
+ setup do
15
+ Configuration.instance_variable_set(:@url, nil)
16
+ Configuration.instance_variable_set(:@client, nil)
17
+ end
18
+
19
+ teardown do
20
+ Configuration.reset
21
+ end
22
+
23
+ should "return default URL" do
24
+ assert_equal 'http://localhost:9200', Configuration.url
25
+ end
26
+
27
+ should "use environment variable, if present" do
28
+ ENV['ELASTICSEARCH_URL'] = 'http://es.example.com'
29
+ assert_equal 'http://es.example.com', Configuration.url
30
+ end
31
+
32
+ should "allow setting and retrieving the URL" do
33
+ assert_nothing_raised { Configuration.url 'http://example.com' }
34
+ assert_equal 'http://example.com', Configuration.url
35
+ end
36
+
37
+ should "strip trailing slash from the URL" do
38
+ assert_nothing_raised { Configuration.url 'http://slash.com:9200/' }
39
+ assert_equal 'http://slash.com:9200', Configuration.url
40
+ end
41
+
42
+ should "return default client" do
43
+ assert_equal HTTP::Client::RestClient, Configuration.client
44
+ end
45
+
46
+ should "return nil as logger by default" do
47
+ assert_nil Configuration.logger
48
+ end
49
+
50
+ should "return set and return logger" do
51
+ Configuration.logger STDERR
52
+ assert_not_nil Configuration.logger
53
+ assert_instance_of Tire::Logger, Configuration.logger
54
+ end
55
+
56
+ should "allow to reset the configuration for specific property" do
57
+ Configuration.url 'http://example.com'
58
+ assert_equal 'http://example.com', Configuration.url
59
+ Configuration.reset :url
60
+ assert_equal 'http://localhost:9200', Configuration.url
61
+ end
62
+
63
+ should "allow to reset the configuration for all properties" do
64
+ Configuration.url 'http://example.com'
65
+ assert_equal 'http://example.com', Configuration.url
66
+ Configuration.reset
67
+ assert_equal 'http://localhost:9200', Configuration.url
68
+ assert_equal HTTP::Client::RestClient, Configuration.client
69
+ end
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,77 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+ require 'tire/http/clients/curb'
4
+
5
+ module Tire
6
+ module HTTP
7
+
8
+ class ClientTest < Test::Unit::TestCase
9
+
10
+ context "RestClient" do
11
+
12
+ should "be default" do
13
+ assert_equal Client::RestClient, Configuration.client
14
+ end
15
+
16
+ should "respond to HTTP methods" do
17
+ assert_respond_to Client::RestClient, :get
18
+ assert_respond_to Client::RestClient, :post
19
+ assert_respond_to Client::RestClient, :put
20
+ assert_respond_to Client::RestClient, :delete
21
+ assert_respond_to Client::RestClient, :head
22
+ end
23
+
24
+ should "not rescue generic exceptions" do
25
+ Client::RestClient.expects(:get).raises(RuntimeError, "Something bad happened in YOUR code")
26
+
27
+ assert_raise(RuntimeError) do
28
+ Client::RestClient.get 'http://example.com'
29
+ end
30
+ end
31
+
32
+ should "not rescue ServerBrokeConnection errors" do
33
+ Client::RestClient.expects(:get).raises(RestClient::ServerBrokeConnection)
34
+
35
+ assert_raise(RestClient::ServerBrokeConnection) do
36
+ Client::RestClient.get 'http://example.com'
37
+ end
38
+ end
39
+
40
+ should "not rescue RequestTimeout errors" do
41
+ Client::RestClient.expects(:get).raises(RestClient::RequestTimeout)
42
+
43
+ assert_raise(RestClient::RequestTimeout) do
44
+ Client::RestClient.get 'http://example.com'
45
+ end
46
+ end
47
+
48
+ end
49
+
50
+ context "Curb" do
51
+ setup do
52
+ Configuration.client Client::Curb
53
+ end
54
+
55
+ teardown do
56
+ Configuration.client Client::RestClient
57
+ end
58
+
59
+ should "use POST method if request body passed" do
60
+ ::Curl::Easy.any_instance.expects(:http_post)
61
+
62
+ response = Configuration.client.get "http://localhost:3000", '{ "query_string" : { "query" : "apple" }}'
63
+ end
64
+
65
+ should "use GET method if request body is nil" do
66
+ ::Curl::Easy.any_instance.expects(:http_get)
67
+
68
+ response = Configuration.client.get "http://localhost:9200/articles/article/1"
69
+ end
70
+
71
+ end
72
+
73
+
74
+ end
75
+ end
76
+
77
+ end
@@ -0,0 +1,50 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ module Tire
5
+ module HTTP
6
+
7
+ class ResponseTest < Test::Unit::TestCase
8
+
9
+ context "Response" do
10
+
11
+ should "take response body, code and headers on initialization" do
12
+ response = Response.new "http response body",
13
+ 200,
14
+ :content_length => 20,
15
+ :content_encoding => 'gzip'
16
+
17
+ assert_equal "http response body", response.body
18
+ assert_equal 200, response.code
19
+ end
20
+
21
+ should "not require headers" do
22
+ assert_nothing_raised do
23
+ Response.new "Forbidden", 403
24
+ end
25
+ end
26
+
27
+ should "return success" do
28
+ responses = []
29
+ responses << Response.new('OK', 200)
30
+ responses << Response.new('Redirect', 302)
31
+
32
+ responses.each { |response| assert response.success? }
33
+
34
+ assert ! Response.new('NotFound', 404).success?
35
+ end
36
+
37
+ should "return failure" do
38
+ assert Response.new('NotFound', 404).failure?
39
+ end
40
+
41
+ should "return string representation" do
42
+ assert_equal "200 : Hello", Response.new('Hello', 200).to_s
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,126 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+ require 'time'
4
+
5
+ module Tire
6
+
7
+ class LoggerTest < Test::Unit::TestCase
8
+ include Tire
9
+
10
+ context "Logger" do
11
+
12
+ context "initialized with an IO object" do
13
+
14
+ should "take STDOUT" do
15
+ assert_nothing_raised do
16
+ logger = Logger.new STDOUT
17
+ end
18
+ end
19
+
20
+ should "write to STDERR" do
21
+ STDERR.expects(:write).with('BOOM!')
22
+ logger = Logger.new STDERR
23
+ logger.write('BOOM!')
24
+ end
25
+
26
+ end
27
+
28
+ context "initialized with file" do
29
+ teardown { File.delete('myfile.log') }
30
+
31
+ should "create the file" do
32
+ assert_nothing_raised do
33
+ logger = Logger.new 'myfile.log'
34
+ assert File.exists?('myfile.log')
35
+ end
36
+ end
37
+
38
+ should "write to file" do
39
+ File.any_instance.expects(:write).with('BOOM!')
40
+ logger = Logger.new 'myfile.log'
41
+ logger.write('BOOM!')
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ context "levels" do
49
+
50
+ should "have the default level" do
51
+ logger = Logger.new STDERR
52
+ assert_equal 'info', logger.level
53
+ end
54
+
55
+ should "set the level" do
56
+ logger = Logger.new STDERR, :level => 'debug'
57
+ assert_equal 'debug', logger.level
58
+ end
59
+
60
+ end
61
+
62
+ context "tracing requests" do
63
+ setup do
64
+ Time.stubs(:now).returns(Time.parse('2011-03-19 11:00'))
65
+ @logger = Logger.new STDERR
66
+ end
67
+
68
+ should "log request in correct format" do
69
+ log = (<<-"log;").gsub(/^ +/, '')
70
+ # 2011-03-19 11:00:00:000 [_search] (["articles", "users"])
71
+ #
72
+ curl -X GET http://...
73
+
74
+ log;
75
+ @logger.expects(:write).with do |payload|
76
+ payload =~ Regexp.new( Regexp.escape('2011-03-19 11:00:00') )
77
+ payload =~ Regexp.new( Regexp.escape('_search') )
78
+ payload =~ Regexp.new( Regexp.escape('(["articles", "users"])') )
79
+ end
80
+ @logger.log_request('_search', ["articles", "users"], 'curl -X GET http://...')
81
+ end
82
+
83
+ should "log response in correct format" do
84
+ json = (<<-"json;").gsub(/^\s*/, '')
85
+ {
86
+ "took" : 4,
87
+ "hits" : {
88
+ "total" : 20,
89
+ "max_score" : 1.0,
90
+ "hits" : [ {
91
+ "_index" : "articles",
92
+ "_type" : "article",
93
+ "_id" : "Hmg0B0VSRKm2VAlsasdnqg",
94
+ "_score" : 1.0, "_source" : { "title" : "Article 1", "published_on" : "2011-01-01" }
95
+ }, {
96
+ "_index" : "articles",
97
+ "_type" : "article",
98
+ "_id" : "booSWC8eRly2I06GTUilNA",
99
+ "_score" : 1.0, "_source" : { "title" : "Article 2", "published_on" : "2011-01-12" }
100
+ }
101
+ ]
102
+ }
103
+ }
104
+ json;
105
+ log = (<<-"log;").gsub(/^\s*/, '')
106
+ # 2011-03-19 11:00:00:000 [200 OK] (4 msec)
107
+ #
108
+ log;
109
+ # log += json.split.map { |line| "# #{line}" }.join("\n")
110
+ json.each_line { |line| log += "# #{line}" }
111
+ log += "\n\n"
112
+ @logger.expects(:write).with do |payload|
113
+ payload =~ Regexp.new( Regexp.escape('2011-03-19 11:00:00') )
114
+ payload =~ Regexp.new( Regexp.escape('[200 OK]') )
115
+ payload =~ Regexp.new( Regexp.escape('(4 msec)') )
116
+ payload =~ Regexp.new( Regexp.escape('took') )
117
+ payload =~ Regexp.new( Regexp.escape('hits') )
118
+ payload =~ Regexp.new( Regexp.escape('_score') )
119
+ end
120
+ @logger.log_response('200 OK', 4, json)
121
+ end
122
+
123
+ end
124
+
125
+ end
126
+ end
data/tire.gemspec ADDED
@@ -0,0 +1,49 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.unshift File.expand_path("../lib", __FILE__)
3
+ require "tire/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "csdn-tire"
7
+ s.version = Tire::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.summary = "Ruby client for CSDNSearch"
10
+ s.homepage = "https://github.com/csdn-dev/tire"
11
+ s.authors = [ 'Hooopo' ]
12
+ s.email = 'wangxz@csdn.net'
13
+
14
+ s.rubyforge_project = "tire"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+
20
+ s.require_paths = ["lib"]
21
+
22
+ s.extra_rdoc_files = [ "README.markdown", "MIT-LICENSE" ]
23
+ s.rdoc_options = [ "--charset=UTF-8" ]
24
+
25
+ s.required_rubygems_version = ">= 1.3.6"
26
+
27
+ # = Library dependencies
28
+ #
29
+ s.add_dependency "rake"
30
+ s.add_dependency "rest-client", "~> 1.6"
31
+ s.add_dependency "multi_json", "~> 1.0"
32
+ s.add_dependency "hashr", "~> 0.0.19"
33
+ s.add_dependency "activesupport", ">= 2.3"
34
+
35
+ # = Development dependencies
36
+ #
37
+ s.add_development_dependency "bundler", "~> 1.0"
38
+ s.add_development_dependency "yajl-ruby", "~> 1.0"
39
+ s.add_development_dependency "shoulda"
40
+ s.add_development_dependency "mocha"
41
+ s.add_development_dependency "bson_ext"
42
+ s.add_development_dependency "curb"
43
+ s.add_development_dependency "minitest"
44
+
45
+
46
+ s.description = <<-DESC
47
+ Tire is a Ruby client for the CSDNSearch search engine/database.
48
+ DESC
49
+ end
metadata ADDED
@@ -0,0 +1,275 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csdn-tire
3
+ version: !ruby/object:Gem::Version
4
+ hash: 1
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 5
9
+ version: "0.5"
10
+ platform: ruby
11
+ authors:
12
+ - Hooopo
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-06-18 00:00:00 +08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rake
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rest-client
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 1
45
+ - 6
46
+ version: "1.6"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: multi_json
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ hash: 15
58
+ segments:
59
+ - 1
60
+ - 0
61
+ version: "1.0"
62
+ type: :runtime
63
+ version_requirements: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ name: hashr
66
+ prerelease: false
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ~>
71
+ - !ruby/object:Gem::Version
72
+ hash: 57
73
+ segments:
74
+ - 0
75
+ - 0
76
+ - 19
77
+ version: 0.0.19
78
+ type: :runtime
79
+ version_requirements: *id004
80
+ - !ruby/object:Gem::Dependency
81
+ name: activesupport
82
+ prerelease: false
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 5
89
+ segments:
90
+ - 2
91
+ - 3
92
+ version: "2.3"
93
+ type: :runtime
94
+ version_requirements: *id005
95
+ - !ruby/object:Gem::Dependency
96
+ name: bundler
97
+ prerelease: false
98
+ requirement: &id006 !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ hash: 15
104
+ segments:
105
+ - 1
106
+ - 0
107
+ version: "1.0"
108
+ type: :development
109
+ version_requirements: *id006
110
+ - !ruby/object:Gem::Dependency
111
+ name: yajl-ruby
112
+ prerelease: false
113
+ requirement: &id007 !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ~>
117
+ - !ruby/object:Gem::Version
118
+ hash: 15
119
+ segments:
120
+ - 1
121
+ - 0
122
+ version: "1.0"
123
+ type: :development
124
+ version_requirements: *id007
125
+ - !ruby/object:Gem::Dependency
126
+ name: shoulda
127
+ prerelease: false
128
+ requirement: &id008 !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ hash: 3
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ type: :development
138
+ version_requirements: *id008
139
+ - !ruby/object:Gem::Dependency
140
+ name: mocha
141
+ prerelease: false
142
+ requirement: &id009 !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ hash: 3
148
+ segments:
149
+ - 0
150
+ version: "0"
151
+ type: :development
152
+ version_requirements: *id009
153
+ - !ruby/object:Gem::Dependency
154
+ name: bson_ext
155
+ prerelease: false
156
+ requirement: &id010 !ruby/object:Gem::Requirement
157
+ none: false
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ hash: 3
162
+ segments:
163
+ - 0
164
+ version: "0"
165
+ type: :development
166
+ version_requirements: *id010
167
+ - !ruby/object:Gem::Dependency
168
+ name: curb
169
+ prerelease: false
170
+ requirement: &id011 !ruby/object:Gem::Requirement
171
+ none: false
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ hash: 3
176
+ segments:
177
+ - 0
178
+ version: "0"
179
+ type: :development
180
+ version_requirements: *id011
181
+ - !ruby/object:Gem::Dependency
182
+ name: minitest
183
+ prerelease: false
184
+ requirement: &id012 !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ hash: 3
190
+ segments:
191
+ - 0
192
+ version: "0"
193
+ type: :development
194
+ version_requirements: *id012
195
+ description: " Tire is a Ruby client for the CSDNSearch search engine/database.\n"
196
+ email: wangxz@csdn.net
197
+ executables: []
198
+
199
+ extensions: []
200
+
201
+ extra_rdoc_files:
202
+ - README.markdown
203
+ - MIT-LICENSE
204
+ files:
205
+ - .gitignore
206
+ - .travis.yml
207
+ - Gemfile
208
+ - MIT-LICENSE
209
+ - README.markdown
210
+ - Rakefile
211
+ - examples/tire.rb
212
+ - lib/tire.rb
213
+ - lib/tire/configuration.rb
214
+ - lib/tire/count.rb
215
+ - lib/tire/dsl.rb
216
+ - lib/tire/http/client.rb
217
+ - lib/tire/http/clients/curb.rb
218
+ - lib/tire/http/response.rb
219
+ - lib/tire/index.rb
220
+ - lib/tire/logger.rb
221
+ - lib/tire/results/pagination.rb
222
+ - lib/tire/rubyext/ruby_1_8.rb
223
+ - lib/tire/rubyext/to_json.rb
224
+ - lib/tire/search.rb
225
+ - lib/tire/state.rb
226
+ - lib/tire/utils.rb
227
+ - lib/tire/version.rb
228
+ - test/integration/count_test.rb
229
+ - test/integration/index_test.rb
230
+ - test/integration/search_test.rb
231
+ - test/integration/state_test.rb
232
+ - test/test_helper.rb
233
+ - test/unit/configuration_test.rb
234
+ - test/unit/http_client_test.rb
235
+ - test/unit/http_response_test.rb
236
+ - test/unit/logger_test.rb
237
+ - tire.gemspec
238
+ has_rdoc: true
239
+ homepage: https://github.com/csdn-dev/tire
240
+ licenses: []
241
+
242
+ post_install_message:
243
+ rdoc_options:
244
+ - --charset=UTF-8
245
+ require_paths:
246
+ - lib
247
+ required_ruby_version: !ruby/object:Gem::Requirement
248
+ none: false
249
+ requirements:
250
+ - - ">="
251
+ - !ruby/object:Gem::Version
252
+ hash: 3
253
+ segments:
254
+ - 0
255
+ version: "0"
256
+ required_rubygems_version: !ruby/object:Gem::Requirement
257
+ none: false
258
+ requirements:
259
+ - - ">="
260
+ - !ruby/object:Gem::Version
261
+ hash: 23
262
+ segments:
263
+ - 1
264
+ - 3
265
+ - 6
266
+ version: 1.3.6
267
+ requirements: []
268
+
269
+ rubyforge_project: tire
270
+ rubygems_version: 1.3.9.4
271
+ signing_key:
272
+ specification_version: 3
273
+ summary: Ruby client for CSDNSearch
274
+ test_files: []
275
+