cloud_search 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  *.gem
2
+ *.sw*
2
3
  *.rbc
3
4
  .bundle
4
5
  .config
data/Guardfile CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  guard 'rspec', :version => 2 do
5
5
  watch(%r{^spec/.+_spec\.rb$})
6
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
7
  watch('spec/spec_helper.rb') { "spec" }
8
8
  watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
9
9
  end
@@ -25,6 +25,7 @@ Gem::Specification.new do |gem|
25
25
  gem.add_development_dependency "vcr" , "~> 2.2"
26
26
  gem.add_development_dependency "webmock"
27
27
 
28
- gem.add_dependency "em-http-request" , "~> 1.0"
28
+ # gem.add_dependency "em-http-request" , "~> 1.0"
29
+ gem.add_dependency "rest-client", "~> 1.6.7"
29
30
  end
30
31
 
@@ -1,4 +1,5 @@
1
- require "em-http"
1
+ # require "em-http"
2
+ require "rest_client"
2
3
  require "json"
3
4
  require "cloud_search/version"
4
5
 
@@ -18,23 +18,9 @@ module CloudSearch
18
18
  end
19
19
 
20
20
  def index
21
- response, message = nil
22
- EM.run do
23
- http = EM::HttpRequest.new(url).post :body => documents_json, :head => headers
24
-
25
- http.callback {
26
- message = "#{http.response_header.status} - #{http.response.length} bytes\n#{url}\n"
27
- response = JSON.parse(http.response)
28
-
29
- EM.stop
30
- }
31
-
32
- http.errback {
33
- message = "#{url}\n#{http.error}"
34
-
35
- EM.stop
36
- }
37
- end
21
+ cloud_search_response = RestClient.post url, documents_json, headers
22
+ message = "#{cloud_search_response.code} - #{cloud_search_response.length} bytes\n#{url}\n"
23
+ response = JSON.parse cloud_search_response.body
38
24
 
39
25
  [response, message]
40
26
  end
@@ -4,25 +4,25 @@ module CloudSearch
4
4
  attr_accessor :http_code
5
5
 
6
6
  def results
7
- (_hits and _hits['hit']) or []
7
+ _hits["hit"] || []
8
8
  end
9
9
 
10
10
  def hits
11
- (_hits and _hits['found']) or 0
11
+ _hits["found"] || 0
12
12
  end
13
13
 
14
14
  def found?
15
- hits >= 1
15
+ hits > 0
16
16
  end
17
17
 
18
18
  def body=(body)
19
- @body = body
19
+ @body = body || {}
20
20
  calculate_pages
21
21
  @body
22
22
  end
23
23
 
24
24
  def items_per_page
25
- @items_per_page or 10
25
+ @items_per_page || 10
26
26
  end
27
27
 
28
28
  alias :page_size :items_per_page
@@ -38,18 +38,19 @@ module CloudSearch
38
38
  end
39
39
 
40
40
  private
41
+
41
42
  def calculate_pages
42
43
  num_full_pages = hits / items_per_page
43
44
  @total_pages = hits % items_per_page > 0 ? num_full_pages + 1 : num_full_pages
44
45
  @total_pages = 1 if @total_pages == 0
45
46
 
46
- start = (_hits and _hits["start"]) || 0
47
+ start = _hits["start"] || 0
47
48
  @current_page = (start / items_per_page) + 1
48
49
  @current_page = @total_pages if @current_page > @total_pages
49
50
  end
50
51
 
51
52
  def _hits
52
- body and body['hits']
53
+ body["hits"] || {}
53
54
  end
54
55
  end
55
56
  end
@@ -1,27 +1,15 @@
1
+ require 'uri'
2
+
1
3
  module CloudSearch
2
4
  class Searcher
3
5
  include ConfigurationChecking
4
6
 
5
7
  def search
6
- response = SearchResponse.new
7
-
8
- EM.run do
9
- http = EM::HttpRequest.new(url).get
10
-
11
- http.callback do
12
- response.http_code = http.response_header.status
13
- response.body = JSON.parse(http.response)
8
+ response = SearchResponse.new
14
9
 
15
- EM.stop
16
- end
17
-
18
- http.errback do
19
- response.http_code = http.error
20
- response.body = http.response
21
-
22
- EM.stop
23
- end
24
- end
10
+ cloud_search_response = RestClient.get url
11
+ response.http_code = cloud_search_response.code
12
+ response.body = JSON.parse(cloud_search_response.body)
25
13
 
26
14
  response.items_per_page = items_per_page
27
15
  response
@@ -39,7 +27,7 @@ module CloudSearch
39
27
  end
40
28
 
41
29
  def query
42
- @query or ''
30
+ URI.escape(@query || '').gsub('&', '%26')
43
31
  end
44
32
 
45
33
  def boolean_query?
@@ -78,8 +66,8 @@ module CloudSearch
78
66
  check_configuration_parameters
79
67
 
80
68
  "#{CloudSearch.config.search_url}/search".tap do |u|
81
- u.concat("?#{query_parameter}=#{CGI.escape(query)}&size=#{items_per_page}&start=#{start}")
82
- u.concat("&return-fields=#{CGI.escape(@fields.join(","))}") unless @fields.nil? or @fields.empty?
69
+ u.concat("?#{query_parameter}=#{query}&size=#{items_per_page}&start=#{start}")
70
+ u.concat("&return-fields=#{URI.escape(@fields.join(","))}") if @fields && @fields.any?
83
71
  end
84
72
  end
85
73
 
@@ -1,3 +1,3 @@
1
1
  module CloudSearch
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -3,20 +3,20 @@ require "spec_helper"
3
3
  describe CloudSearch::SearchResponse do
4
4
  subject { described_class.new }
5
5
 
6
- before do
7
- subject.body = YAML.load_file File.expand_path("../../fixtures/full.yml", __FILE__)
8
- end
9
-
10
6
  context "when there are results" do
7
+ before do
8
+ subject.body = YAML.load_file File.expand_path("../../fixtures/full.yml", __FILE__)
9
+ end
10
+
11
11
  describe "#results" do
12
12
  it "list matched documents" do
13
- subject.results.inject([]){ |acc, i| acc << i['data']['title']}.flatten(1)
14
- .should == ["Star Wars: The Clone Wars",
15
- "Star Wars",
16
- "Star Wars: Episode II - Attack of the Clones",
17
- "Star Wars: Episode V - The Empire Strikes Back",
18
- "Star Wars: Episode VI - Return of the Jedi",
19
- "Star Wars: Episode I - The Phantom Menace",
13
+ subject.results.map{ |item| item['data']['title'] }.flatten
14
+ .should == ["Star Wars: The Clone Wars",
15
+ "Star Wars",
16
+ "Star Wars: Episode II - Attack of the Clones",
17
+ "Star Wars: Episode V - The Empire Strikes Back",
18
+ "Star Wars: Episode VI - Return of the Jedi",
19
+ "Star Wars: Episode I - The Phantom Menace",
20
20
  "Star Wars: Episode III - Revenge of the Sith"]
21
21
  end
22
22
  end
@@ -40,7 +40,7 @@ describe CloudSearch::SearchResponse do
40
40
  end
41
41
 
42
42
  describe "#items_per_page" do
43
- it "returns items per page" do
43
+ it "returns items per page as default 10" do
44
44
  subject.items_per_page.should == 10
45
45
  end
46
46
  end
@@ -52,7 +52,7 @@ describe CloudSearch::SearchResponse do
52
52
  end
53
53
 
54
54
  describe "#offset" do
55
- it "returns offset" do
55
+ it "returns offset as default 0" do
56
56
  subject.offset.should == 0
57
57
  end
58
58
  end
@@ -97,6 +97,118 @@ describe CloudSearch::SearchResponse do
97
97
  it "returns offset" do
98
98
  subject.offset.should == 0
99
99
  end
100
- end
100
+ end
101
+ end
102
+
103
+ context "pagination" do
104
+ let(:seven_hits) { YAML.load_file File.expand_path("../../fixtures/full.yml", __FILE__) }
105
+
106
+ it "returns number of pages based on hits" do
107
+ subject.items_per_page = 8
108
+ subject.body = seven_hits
109
+ subject.total_pages.should == 1
110
+
111
+ subject.items_per_page = 7
112
+ subject.body = seven_hits
113
+ subject.total_pages.should == 1
114
+
115
+ subject.items_per_page = 6
116
+ subject.body = seven_hits
117
+ subject.total_pages.should == 2
118
+
119
+ subject.items_per_page = 5
120
+ subject.body = seven_hits
121
+ subject.total_pages.should == 2
122
+
123
+ subject.items_per_page = 4
124
+ subject.body = seven_hits
125
+ subject.total_pages.should == 2
126
+
127
+ subject.items_per_page = 3
128
+ subject.body = seven_hits
129
+ subject.total_pages.should == 3
130
+
131
+ subject.items_per_page = 2
132
+ subject.body = seven_hits
133
+ subject.total_pages.should == 4
134
+
135
+ subject.items_per_page = 1
136
+ subject.body = seven_hits
137
+ subject.total_pages.should == 7
138
+ end
139
+
140
+ it "returns current page based on start and items per page" do
141
+ subject.items_per_page = 3
142
+ seven_hits['hits']['start'] = nil
143
+ subject.body = seven_hits
144
+ subject.current_page.should == 1
145
+
146
+ subject.items_per_page = 3
147
+ seven_hits['hits']['start'] = 0
148
+ subject.body = seven_hits
149
+ subject.current_page.should == 1
150
+
151
+ subject.items_per_page = 3
152
+ seven_hits['hits']['start'] = 2
153
+ subject.body = seven_hits
154
+ subject.current_page.should == 1
155
+
156
+ subject.items_per_page = 3
157
+ seven_hits['hits']['start'] = 3
158
+ subject.body = seven_hits
159
+ subject.current_page.should == 2
160
+
161
+ subject.items_per_page = 3
162
+ seven_hits['hits']['start'] = 4
163
+ subject.body = seven_hits
164
+ subject.current_page.should == 2
165
+
166
+ subject.items_per_page = 3
167
+ seven_hits['hits']['start'] = 5
168
+ subject.body = seven_hits
169
+ subject.current_page.should == 2
170
+
171
+ subject.items_per_page = 3
172
+ seven_hits['hits']['start'] = 6
173
+ subject.body = seven_hits
174
+ subject.current_page.should == 3
175
+ end
176
+
177
+ it "calculates offset based on current page and items_per_page" do
178
+ subject.items_per_page = 3
179
+ seven_hits['hits']['start'] = nil
180
+ subject.body = seven_hits
181
+ subject.offset.should == 0
182
+
183
+ subject.items_per_page = 3
184
+ seven_hits['hits']['start'] = 0
185
+ subject.body = seven_hits
186
+ subject.offset.should == 0
187
+
188
+ subject.items_per_page = 3
189
+ seven_hits['hits']['start'] = 2
190
+ subject.body = seven_hits
191
+ subject.offset.should == 0
192
+
193
+ subject.items_per_page = 3
194
+ seven_hits['hits']['start'] = 3
195
+ subject.body = seven_hits
196
+ subject.offset.should == 3
197
+
198
+ subject.items_per_page = 3
199
+ seven_hits['hits']['start'] = 4
200
+ subject.body = seven_hits
201
+ subject.offset.should == 3
202
+
203
+ subject.items_per_page = 3
204
+ seven_hits['hits']['start'] = 5
205
+ subject.body = seven_hits
206
+ subject.offset.should == 3
207
+
208
+ subject.items_per_page = 3
209
+ seven_hits['hits']['start'] = 6
210
+ subject.body = seven_hits
211
+ subject.offset.should == 6
212
+ end
101
213
  end
102
214
  end
@@ -46,7 +46,7 @@ describe CloudSearch::Searcher do
46
46
 
47
47
  it "uses 'bq' to specify the query in the URL" do
48
48
  subject.with_boolean_query("year:2000")
49
- subject.url.should == "#{url_prefix}bq=year%3A2000&size=10&start=0"
49
+ subject.url.should == "#{url_prefix}bq=year:2000&size=10&start=0"
50
50
  end
51
51
  end
52
52
 
@@ -124,6 +124,14 @@ describe CloudSearch::Searcher do
124
124
  subject.with_query("foo").url.should == "#{url_prefix}q=foo&size=10&start=0"
125
125
  end
126
126
 
127
+ it "returns cloud search url with foo query" do
128
+ subject.with_query("f&oo").url.should == "#{url_prefix}q=f%26oo&size=10&start=0"
129
+ end
130
+
131
+ it "returns cloud search url with foo* query" do
132
+ subject.with_query("foo*").url.should == "#{url_prefix}q=foo*&size=10&start=0"
133
+ end
134
+
127
135
  it "returns cloud search url with size equals 20" do
128
136
  subject.with_items_per_page(20).url.should == "#{url_prefix}q=&size=20&start=0"
129
137
  end
@@ -133,7 +141,7 @@ describe CloudSearch::Searcher do
133
141
  end
134
142
 
135
143
  it "returns cloud search url with foo and bar fields" do
136
- subject.with_fields(:foo, :bar).url.should == "#{url_prefix}q=&size=10&start=0&return-fields=foo%2Cbar"
144
+ subject.with_fields(:foo, :bar).url.should == "#{url_prefix}q=&size=10&start=0&return-fields=foo,bar"
137
145
  end
138
146
  end
139
147
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloud_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -108,13 +108,13 @@ dependencies:
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  - !ruby/object:Gem::Dependency
111
- name: em-http-request
111
+ name: rest-client
112
112
  requirement: !ruby/object:Gem::Requirement
113
113
  none: false
114
114
  requirements:
115
115
  - - ~>
116
116
  - !ruby/object:Gem::Version
117
- version: '1.0'
117
+ version: 1.6.7
118
118
  type: :runtime
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
@@ -122,7 +122,7 @@ dependencies:
122
122
  requirements:
123
123
  - - ~>
124
124
  - !ruby/object:Gem::Version
125
- version: '1.0'
125
+ version: 1.6.7
126
126
  description: A wraper to Amazon CloudSearch's API
127
127
  email:
128
128
  - willian@willianfernandes.com.br