tire-suggest_plugin 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 419896fca18e0300dbe135351d5d89e5d3828e44
4
+ data.tar.gz: a9f50303c4abff70d8b101fa11d5c336b38e4b07
5
+ SHA512:
6
+ metadata.gz: d062e70347a01ca62de67394dc60a18bcc98b420b1c439d930281fdca1802499d1b6a32e3625a817813940d798997d2ff1e1f6b48048412bd88e0845166d7b77
7
+ data.tar.gz: d4d960d992d999adf95eb539a112d342aa683cadb2e7682951233e768475cd5dfd3946f20a62778ba61a543413f35939087348769308509f39d512856cbff3b0
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tire-suggest-plugin.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Dmitry Zhlobo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # Tire for [Suggester Plugin for Elasticsearch](https://github.com/spinscale/elasticsearch-suggest-plugin)
2
+
3
+ Addition for [Tire](https://github.com/karmi/tire) to work with suggester plugin for elasticsearch: https://github.com/spinscale/elasticsearch-suggest-plugin.
4
+
5
+ Now it's just *build-it-fast-and-check-if-it-works* version. Stay tune for updates.
6
+
7
+ ## Usage
8
+
9
+ ### Searching for suggestions
10
+
11
+ ```ruby
12
+ Tire.suggest('cars', field: 'name.suggest', term: 'au').suggestions
13
+ #=> ["audi", "audi 100", "audi 200", "audi 80", "audi 90", "audi a2", "audi a3", "audi a4", "audi a5", "audi a6"]
14
+ ```
15
+
16
+ ### Searching by several indices
17
+
18
+ ```ruby
19
+ Tire.suggest ['cars', 'ads'] do
20
+ field 'name'
21
+ term 'bmw x'
22
+ end.suggestions
23
+ #=> ["bmw x3", "bmw x5"]
24
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,33 @@
1
+ module Tire::SuggestPlugin
2
+ AVAILABLE_PARAMS = [:field, :term, :size]
3
+ class Params < Struct.new(*AVAILABLE_PARAMS)
4
+
5
+ def initialize(params={})
6
+ AVAILABLE_PARAMS.each do |param_name|
7
+ send("#{param_name}=", params[param_name])
8
+ end
9
+ end
10
+
11
+ AVAILABLE_PARAMS.each do |param_name|
12
+ define_method(param_name) do |value=nil|
13
+ if value.nil?
14
+ super()
15
+ else
16
+ send("#{param_name}=", value)
17
+ end
18
+ end
19
+ end
20
+
21
+ def to_json
22
+ MultiJson.dump(self.to_hash)
23
+ end
24
+
25
+ def to_hash
26
+ result = {}
27
+ AVAILABLE_PARAMS.each do |param_name|
28
+ result[param_name] = send(param_name)
29
+ end
30
+ result
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ module Tire::SuggestPlugin
2
+ class Suggestions < Array
3
+ def initialize(response={})
4
+ super(response['suggestions'])
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Tire
2
+ module SuggestPlugin
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,42 @@
1
+ require 'tire'
2
+ require 'tire/suggest_plugin/version'
3
+ require 'tire/suggest_plugin/params'
4
+ require 'tire/suggest_plugin/suggestions'
5
+
6
+ module Tire::SuggestPlugin
7
+ class SuggestRequiestFailed < StandardError; end
8
+
9
+ class Suggest
10
+ def initialize(indices=nil, options={}, &block)
11
+ @indices = Array(indices)
12
+
13
+ @params = Params.new(options)
14
+ if block_given?
15
+ block.arity < 1 ? @params.instance_eval(&block) : block.call(@params)
16
+ end
17
+ end
18
+
19
+ def suggestions
20
+ @suggestions or (perform and @suggestions)
21
+ end
22
+
23
+ def perform
24
+ response = Tire::Configuration.client.get(self.url, @params.to_json)
25
+ if response.failure?
26
+ STDERR.puts "[REQUEST FAILED] #{self.to_curl}\n"
27
+ raise SuggestRequestFailed, response.to_s
28
+ end
29
+ @suggestions = Suggestions.new(MultiJson.load(response.body))
30
+ end
31
+
32
+ def url
33
+ path = ['/', @indices.join(','), '_suggest'].join('/').squeeze('/')
34
+ Tire::Configuration.url + path
35
+ end
36
+
37
+ def to_curl
38
+ params_to_json_escaped = @params.to_json.gsub("'", '\u0027')
39
+ "curl -X GET '#{url}' -d '#{params_to_json_escaped}'"
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,15 @@
1
+ require 'tire/suggest_plugin'
2
+
3
+ module Tire
4
+ module DSL
5
+ def suggest(indices=nil, options={}, &block)
6
+ if block_given?
7
+ Tire::SuggestPlugin::Suggest.new(indices, options, &block)
8
+ elsif options.respond_to?(:to_hash)
9
+ Tire::SuggestPlugin::Suggest.new(indices, options.to_hash)
10
+ else
11
+ raise ArgumentError, "Please pass a Ruby Hash or an object with `to_hash` method, not #{options.class}"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tire/suggest_plugin/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tire-suggest_plugin"
8
+ spec.version = Tire::SuggestPlugin::VERSION
9
+ spec.authors = ["Dmitry Zhlobo"]
10
+ spec.email = ["dima.zhlobo@gmail.com"]
11
+ spec.description = "Addition for Tire to work with suggester plugin for elasticsearch"
12
+ spec.summary = "Addition for Tire to work with suggester plugin for elasticsearch"
13
+ spec.homepage = "https://github.com/twinslash/tire-suggest-plugin"
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
+
24
+ spec.add_dependency "tire", "~> 0.5.8"
25
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tire-suggest_plugin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Zhlobo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tire
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.5.8
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.5.8
55
+ description: Addition for Tire to work with suggester plugin for elasticsearch
56
+ email:
57
+ - dima.zhlobo@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/tire-suggest_plugin.rb
68
+ - lib/tire/suggest_plugin.rb
69
+ - lib/tire/suggest_plugin/params.rb
70
+ - lib/tire/suggest_plugin/suggestions.rb
71
+ - lib/tire/suggest_plugin/version.rb
72
+ - tire-suggest_plugin.gemspec
73
+ homepage: https://github.com/twinslash/tire-suggest-plugin
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.0.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Addition for Tire to work with suggester plugin for elasticsearch
97
+ test_files: []