asari 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  require "asari/version"
2
2
 
3
+ require "asari/collection"
3
4
  require "asari/exceptions"
4
5
 
5
6
  require "httparty"
@@ -53,15 +54,12 @@ class Asari
53
54
  def search(term, options = {})
54
55
  return [] if self.class.mode == :sandbox
55
56
 
56
- url = "http://search-#{search_domain}.us-east-1.cloudsearch.amazonaws.com/#{api_version}/search?q=#{CGI.escape(term)}"
57
+ page_size = options[:page_size] || 10
57
58
 
58
- if options[:page_size]
59
- url << "&size=#{options[:page_size]}"
60
- end
59
+ url = "http://search-#{search_domain}.us-east-1.cloudsearch.amazonaws.com/#{api_version}/search?q=#{CGI.escape(term)}&size=#{page_size}"
61
60
 
62
61
  if options[:page]
63
- size = options[:page_size] || 10
64
- start = (options[:page] - 1) * size + 1
62
+ start = (options[:page] - 1) * page_size
65
63
  url << "&start=#{start}"
66
64
  end
67
65
 
@@ -77,7 +75,7 @@ class Asari
77
75
  raise Asari::SearchException.new("#{response.response.code}: #{response.response.msg}")
78
76
  end
79
77
 
80
- response.parsed_response["hits"]["hit"].map { |h| h["id"] }
78
+ Asari::Collection.new(response, page_size)
81
79
  end
82
80
 
83
81
  # Public: Add an item to the index with the given ID.
@@ -0,0 +1,35 @@
1
+ class Asari
2
+ class Collection < BasicObject
3
+ attr_reader :current_page
4
+ attr_reader :page_size
5
+ attr_reader :total_entries
6
+
7
+ # Internal: This object should really only ever be instantiated from within
8
+ # Asari code. The Asari Collection knows how to build itself from an
9
+ # HTTParty::Response object representing a search query result from
10
+ # CloudSearch.
11
+ #
12
+ # We also have to pass the page size in directly, because the CloudSearch
13
+ # response doesn't have any data about page size. It's cool, though. I
14
+ # guess.
15
+ #
16
+ def initialize(httparty_response, page_size)
17
+ resp = httparty_response.parsed_response
18
+ @total_entries = resp["hits"]["found"]
19
+ @page_size = page_size
20
+
21
+ start = resp["hits"]["start"]
22
+ @current_page = (start / page_size) + 1
23
+
24
+ @data = resp["hits"]["hit"].map { |h| h["id"] }
25
+ end
26
+
27
+ def offset
28
+ (@current_page - 1) * @page_size
29
+ end
30
+
31
+ def method_missing(method, *args, &block)
32
+ @data.send(method, *args, &block)
33
+ end
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  class Asari
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asari
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-11 00:00:00.000000000 Z
12
+ date: 2012-07-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
16
- requirement: &70161865408960 !ruby/object:Gem::Requirement
16
+ requirement: &70182006566380 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70161865408960
24
+ version_requirements: *70182006566380
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70161865407860 !ruby/object:Gem::Requirement
27
+ requirement: &70182006565620 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70161865407860
35
+ version_requirements: *70182006565620
36
36
  description: Asari s a Ruby interface for AWS CloudSearch
37
37
  email:
38
38
  - tommy@wellbredgrapefruit.com
@@ -47,6 +47,7 @@ files:
47
47
  - asari.gemspec
48
48
  - lib/asari.rb
49
49
  - lib/asari/active_record.rb
50
+ - lib/asari/collection.rb
50
51
  - lib/asari/exceptions.rb
51
52
  - lib/asari/version.rb
52
53
  - spec/active_record_spec.rb