csquery 0.1.2

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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4e52a544d9ece14775462fc5cacc08be8e4f10f5
4
+ data.tar.gz: 325e403731957769f74f0b97ee2b0cf020fcac5c
5
+ SHA512:
6
+ metadata.gz: 264a55240faffa2adbc0c9f0bc7489c51c6f532486ca42eb059efab2d5564a65adcff07f1c3499b14f959e85373a9863920dce6f84a8c1d385d2b812c7358b12
7
+ data.tar.gz: 60126045b4b5abee10b25066e7bb9a328d70eadae06ef5409154c1a297a68c8e17ebc559f71b11f9a7de787f63c4f67cff783f414cc92e6ef4011a827c994a88
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,6 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.12.4
6
+ script: bundle exec rspec
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at masashi.oyamada@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in csquery.gemspec
4
+ gemspec
@@ -0,0 +1,48 @@
1
+ # Csquery
2
+
3
+ [![Build Status](https://travis-ci.org/marcy/csquery.svg?branch=master)](https://travis-ci.org/marcy/csquery)
4
+ [![Gem Version](https://badge.fury.io/rb/csquery.svg)](https://badge.fury.io/rb/csquery)
5
+
6
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/csquery`. To experiment with that code, run `bin/console` for an interactive prompt.
7
+
8
+ A simple query builder for Amazon Cloudsearch Structured query parser for ruby.
9
+ see: https://github.com/tell-k/csquery
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'csquery'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install csquery
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ Csquery::Structured.and_(
31
+ Csquery::Structured.not_('test', field:'genres'),
32
+ Csquery::Structured.or_(
33
+ Csquery::Structured.term_('star', field:'title', boost:2),
34
+ Csquery::Structured.term_('star', field:'plot')
35
+ )
36
+ ).query
37
+ # => "(and (not field=genres 'test') (or (term field=title boost=2 'star') (term field=plot 'star')))"
38
+ ```
39
+
40
+ ## Development
41
+
42
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
43
+
44
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/csquery. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "csquery"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'csquery/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "csquery"
8
+ spec.version = Csquery::VERSION
9
+ spec.authors = ["marcy"]
10
+ spec.email = ["masashi.oyamada@gmail.com"]
11
+
12
+ spec.summary = %q{A simple query builder for Amazon Cloudsearch Structured query parser.}
13
+ spec.description = %q{A simple query builder for Amazon Cloudsearch Structured query parser.}
14
+ spec.homepage = "https://github.com/marcy/csquery"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
@@ -0,0 +1,6 @@
1
+ require "csquery/version"
2
+
3
+ require "csquery/structured"
4
+
5
+ require "csquery/field_value"
6
+ require "csquery/expression"
@@ -0,0 +1,29 @@
1
+ module Csquery
2
+ class Expression
3
+ def initialize(operator, *args, options: {}, **kwargs)
4
+ @operator = operator
5
+ @options = options
6
+ @fields = []
7
+
8
+ args.each do |arg|
9
+ if arg.is_a? FieldValue
10
+ @fields << arg
11
+ else
12
+ @fields << FieldValue.new(value: arg)
13
+ end
14
+ end
15
+
16
+ kwargs.sort_by {|k, _| k }.each do |k, v|
17
+ @fields << FieldValue.new(name: k, value: v)
18
+ end
19
+ end
20
+
21
+ def query
22
+ "(#{@operator}#{Structured::format_options(@options)} #{@fields.map(&:to_s).join(' ')})".gsub(/\s+/, ' ')
23
+ end
24
+
25
+ def to_s
26
+ query
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ module Csquery
2
+ class FieldValue
3
+ attr_reader :name, :value
4
+
5
+ def initialize(value:, name: nil)
6
+ value = value[0] if value.is_a?(Array) and value[0].is_a?(Hash)
7
+
8
+ if value.is_a?(Hash)
9
+ @name = value.keys[0]
10
+ @value = Structured.format_value(value.values[0])
11
+ else
12
+ @name = name
13
+ @value = Structured.format_value(value)
14
+ end
15
+ end
16
+
17
+ def to_value
18
+ return "#{@name}:#{@value}" if @name
19
+
20
+ @value
21
+ end
22
+
23
+ def to_s
24
+ to_value
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,92 @@
1
+ module Csquery
2
+ class Structured
3
+ class << self
4
+ def field(value, name = nil)
5
+ FieldValue.new(value: value, name: name)
6
+ end
7
+
8
+ def and_(*args, **kwargs)
9
+ Expression.new('and', *args, options: _get_option([:boost], kwargs), **kwargs)
10
+ end
11
+
12
+ def not_(*args, **kwargs)
13
+ Expression.new('not', *args, options: _get_option([:field, :boost], kwargs), **kwargs)
14
+ end
15
+
16
+ def or_(*args, **kwargs)
17
+ Expression.new('or', *args, options: _get_option([:boost], kwargs), **kwargs)
18
+ end
19
+
20
+ def term_(*args, **kwargs)
21
+ Expression.new('term', *args, options: _get_option([:field, :boost], kwargs), **kwargs)
22
+ end
23
+
24
+ def near_(*args, **kwargs)
25
+ Expression.new('near', *args, options: _get_option([:field, :distance, :boost], kwargs), **kwargs)
26
+ end
27
+
28
+ def phrase_(*args, **kwargs)
29
+ Expression.new('phrase', *args, options: _get_option([:field, :boost], kwargs), **kwargs)
30
+ end
31
+
32
+ def prefix_(*args, **kwargs)
33
+ Expression.new('prefix', *args, options: _get_option([:field, :boost], kwargs), **kwargs)
34
+ end
35
+
36
+ def range_(*args, **kwargs)
37
+ Expression.new('range', *args, options: _get_option([:field, :boost], kwargs), **kwargs)
38
+ end
39
+
40
+ def format_value(value)
41
+ return format_range_values(*value) if value.is_a?(Array)
42
+
43
+ return "#{value}" if value.is_a?(Numeric)
44
+
45
+ return value.query if value.is_a?(Expression)
46
+
47
+ value = value.to_s
48
+
49
+ if(value.start_with?('(') and value.end_with?(')'))\
50
+ or(value.start_with?('{') and value.end_with?(']'))\
51
+ or(value.start_with?('[') and value.end_with?('}'))\
52
+ or(value.start_with?('[') and value.end_with?(']'))
53
+
54
+ return value
55
+ end
56
+
57
+ "'#{escape(value)}'"
58
+ end
59
+
60
+ def format_options(options={})
61
+ return '' unless options
62
+
63
+ ' ' + options.map{|k,v| "#{k}=#{v}"}.join(' ')
64
+ end
65
+
66
+ private
67
+
68
+ def escape(string)
69
+ string.gsub(/\\|\'/) {|word| "\\#{word}" }
70
+ end
71
+
72
+ def format_range_values(start_val, end_val = nil)
73
+ start_paren = [nil, ''].include?(start_val) ? '{' : '['
74
+ start_value = [nil, ''].include?(start_val) ? '' : start_val
75
+ end_value = [nil, ''].include?(end_val) ? '' : end_val
76
+ end_paren = [nil, ''].include?(end_val) ? '}' : ']'
77
+
78
+ "#{start_paren}#{start_value},#{end_value}#{end_paren}"
79
+ end
80
+
81
+ def _get_option(keys, options)
82
+ opts = {}
83
+
84
+ keys.each do |key|
85
+ opts[key] = options.delete(key) if options.keys.include?(key)
86
+ end
87
+
88
+ opts
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,3 @@
1
+ module Csquery
2
+ VERSION = "0.1.2"
3
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csquery
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - marcy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-27 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: A simple query builder for Amazon Cloudsearch Structured query parser.
56
+ email:
57
+ - masashi.oyamada@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - csquery.gemspec
72
+ - lib/csquery.rb
73
+ - lib/csquery/expression.rb
74
+ - lib/csquery/field_value.rb
75
+ - lib/csquery/structured.rb
76
+ - lib/csquery/version.rb
77
+ homepage: https://github.com/marcy/csquery
78
+ licenses: []
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.5.1
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A simple query builder for Amazon Cloudsearch Structured query parser.
100
+ test_files: []