rawscsi 1.0.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.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.travis.yml +13 -0
  4. data/Gemfile +5 -0
  5. data/LICENSE.txt +23 -0
  6. data/README.md +288 -0
  7. data/Rakefile +11 -0
  8. data/lib/rawscsi/base.rb +13 -0
  9. data/lib/rawscsi/index.rb +64 -0
  10. data/lib/rawscsi/index_helpers/connection.rb +23 -0
  11. data/lib/rawscsi/index_helpers/sdf_add.rb +47 -0
  12. data/lib/rawscsi/index_helpers/sdf_delete.rb +19 -0
  13. data/lib/rawscsi/query/compound.rb +114 -0
  14. data/lib/rawscsi/query/simple.rb +16 -0
  15. data/lib/rawscsi/search.rb +46 -0
  16. data/lib/rawscsi/search_helpers/results_active_record.rb +28 -0
  17. data/lib/rawscsi/search_helpers/results_hash.rb +14 -0
  18. data/lib/rawscsi/version.rb +3 -0
  19. data/lib/rawscsi.rb +46 -0
  20. data/rawscsi.gemspec +41 -0
  21. data/spec/fixtures/vcr/index_spec/delete.yml +38 -0
  22. data/spec/fixtures/vcr/index_spec/handle_batch_limit.yml +73 -0
  23. data/spec/fixtures/vcr/index_spec/upload_hash.yml +39 -0
  24. data/spec/fixtures/vcr/search_spec/and_diff_field.yml +32 -0
  25. data/spec/fixtures/vcr/search_spec/and_or_combo.yml +35 -0
  26. data/spec/fixtures/vcr/search_spec/and_same_field.yml +31 -0
  27. data/spec/fixtures/vcr/search_spec/date.yml +34 -0
  28. data/spec/fixtures/vcr/search_spec/disjunction.yml +44 -0
  29. data/spec/fixtures/vcr/search_spec/fields.yml +35 -0
  30. data/spec/fixtures/vcr/search_spec/limit_sort.yml +32 -0
  31. data/spec/fixtures/vcr/search_spec/no_result.yml +30 -0
  32. data/spec/fixtures/vcr/search_spec/numeric_range.yml +32 -0
  33. data/spec/fixtures/vcr/search_spec/simple_search.yml +153 -0
  34. data/spec/lib/rawscsi/index_helpers/connection_spec.rb +25 -0
  35. data/spec/lib/rawscsi/index_helpers/sdf_add_spec.rb +44 -0
  36. data/spec/lib/rawscsi/index_helpers/sdf_delete_spec.rb +11 -0
  37. data/spec/lib/rawscsi/index_spec.rb +62 -0
  38. data/spec/lib/rawscsi/query/compound_spec.rb +80 -0
  39. data/spec/lib/rawscsi/query/simple_spec.rb +11 -0
  40. data/spec/lib/rawscsi/search_spec.rb +170 -0
  41. data/spec/lib/rawscsi/version_spec.rb +8 -0
  42. data/spec/rawscsi_spec.rb +33 -0
  43. data/spec/spec_helper.rb +17 -0
  44. metadata +250 -0
@@ -0,0 +1,46 @@
1
+ require "httparty"
2
+
3
+ module Rawscsi
4
+ class Search < Base
5
+ def search(arg)
6
+ if arg.is_a?(String)
7
+ query = Rawscsi::Query::Simple.new(arg).build
8
+ elsif arg.is_a?(Hash)
9
+ query = Rawscsi::Query::Compound.new(arg).build
10
+ else
11
+ raise "Unknown argument type"
12
+ end
13
+
14
+ response = send_request_to_aws(query)
15
+ build_results(response)
16
+ end
17
+
18
+ private
19
+ def url(query)
20
+ [
21
+ "http://search-",
22
+ "#{config.domain_name}-",
23
+ "#{config.domain_id}.",
24
+ "#{config.region}.",
25
+ "cloudsearch.amazonaws.com/",
26
+ "#{config.api_version}/",
27
+ "search?",
28
+ query
29
+ ].join
30
+ end
31
+
32
+ def send_request_to_aws(query)
33
+ url_query = url(query)
34
+ HTTParty.get(url_query)
35
+ end
36
+
37
+ def build_results(response)
38
+ if is_active_record
39
+ Rawscsi::SearchHelpers::ResultsActiveRecord.new(response, model).build
40
+ else
41
+ Rawscsi::SearchHelpers::ResultsHash.new(response).build
42
+ end
43
+ end
44
+ end
45
+ end
46
+
@@ -0,0 +1,28 @@
1
+ module Rawscsi
2
+ module SearchHelpers
3
+ class ResultsActiveRecord
4
+ def initialize(response, model)
5
+ @response = response
6
+ @model = model
7
+ end
8
+
9
+ def build
10
+ id_array = @response["hits"]["hit"].map {|h| h["id"].to_i }
11
+ return [] if id_array.empty?
12
+ results =
13
+ if ActiveRecord::VERSION::MAJOR > 2
14
+ klass.where(:id => id_array).to_a
15
+ else
16
+ klass.find_all_by_id(id_array)
17
+ end
18
+ results.index_by(&:id).slice(*id_array).values
19
+ end
20
+
21
+ private
22
+ def klass
23
+ @model.constantize
24
+ end
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,14 @@
1
+ module Rawscsi
2
+ module SearchHelpers
3
+ class ResultsHash
4
+ def initialize(response)
5
+ @response = response
6
+ end
7
+
8
+ def build
9
+ @response["hits"]["hit"].map {|h| h["fields"]}
10
+ end
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,3 @@
1
+ module Rawscsi
2
+ VERSION = "1.0.1"
3
+ end
data/lib/rawscsi.rb ADDED
@@ -0,0 +1,46 @@
1
+ $:.unshift(File.expand_path("../", __FILE__))
2
+
3
+ module Rawscsi
4
+ autoload :Version, 'rawscsi/version'
5
+ autoload :Base, 'rawscsi/base'
6
+ autoload :Search, 'rawscsi/search'
7
+ autoload :Index, 'rawscsi/index'
8
+
9
+ module Query
10
+ autoload :Simple, "rawscsi/query/simple"
11
+ autoload :Compound, "rawscsi/query/compound"
12
+ end
13
+
14
+ module SearchHelpers
15
+ autoload :ResultsHash, "rawscsi/search_helpers/results_hash"
16
+ autoload :ResultsActiveRecord, "rawscsi/search_helpers/results_active_record"
17
+ end
18
+
19
+ module IndexHelpers
20
+ autoload :Connection, "rawscsi/index_helpers/connection"
21
+ autoload :SdfDelete, "rawscsi/index_helpers/sdf_delete"
22
+ autoload :SdfAdd, "rawscsi/index_helpers/sdf_add"
23
+ end
24
+
25
+ @@registered_models = {}
26
+
27
+ class Configuration
28
+ attr_accessor :domain_name,
29
+ :domain_id,
30
+ :region,
31
+ :api_version,
32
+ :attributes,
33
+ :batch_size
34
+ end
35
+
36
+ def self.register(model)
37
+ config = Configuration.new
38
+ yield(config)
39
+ @@registered_models[model] = config
40
+ end
41
+
42
+ def self.registered_models
43
+ @@registered_models
44
+ end
45
+ end
46
+
data/rawscsi.gemspec ADDED
@@ -0,0 +1,41 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rawscsi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rawscsi"
8
+ spec.version = Rawscsi::VERSION
9
+ spec.authors = ["Steven Li"]
10
+ spec.email = ["StevenJLi@gmail.com"]
11
+ spec.description = %q{Ruby Amazon Web Services Cloud Search Interface}
12
+ spec.summary = %q{Adds service objects to upload and search active record models with AWS Cloud Search }
13
+ spec.homepage = "https://github.com/stevenjl/rawscsi"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "3.0"
24
+ spec.add_development_dependency "vcr"
25
+ spec.add_development_dependency "fakeweb"
26
+ spec.add_development_dependency "pry"
27
+
28
+ unless RUBY_VERSION == "1.8.7"
29
+ spec.add_development_dependency "activerecord", "> 2.0"
30
+ spec.add_dependency "httparty", "~> 0.11"
31
+ spec.add_dependency "faraday"
32
+ spec.add_dependency "faraday_middleware"
33
+ else
34
+ spec.add_development_dependency "activerecord", "2.0"
35
+ spec.add_dependency "httparty", "0.8"
36
+ spec.add_dependency "faraday", "=0.8.7"
37
+ spec.add_dependency "faraday_middleware", ">= 0.8.0"
38
+ spec.add_dependency "json"
39
+ end
40
+ end
41
+