fracas 0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f086a0443ad8fe5746e5d5edd7deaf57e4e78549
4
+ data.tar.gz: 17e24bf030ba8a4fea76f886466258b28fdcf3d8
5
+ SHA512:
6
+ metadata.gz: 6dfea9b77028a28531cb2c5de2618bcb4738d39726b3bd097a61bafe590c762974f43bc3eccdc3d912b84389623beb38c09b0947a205e19e218b3ece1b7fbdfb
7
+ data.tar.gz: 0bf9e802f598f9d022b9c7ebe3f5981c4a581739e8cc851b08ebddf7df144a0497378a1da6cc31d00547c3e23a5ac52ba8a5e8e5268e93f05dc2e098276baca3
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --order random
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fracas.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Chris Hanks
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.
@@ -0,0 +1,29 @@
1
+ # Fracas
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'fracas'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install fracas
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/fracas/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fracas/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'fracas'
8
+ spec.version = Fracas::VERSION
9
+ spec.authors = ["Chris Hanks"]
10
+ spec.email = ['christopher.m.hanks@gmail.com']
11
+ spec.summary = %q{Write a short summary. Required.}
12
+ spec.description = %q{Write a longer description. Optional.}
13
+ spec.homepage = 'https://github.com/chanks/fracas'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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_dependency 'elasticsearch'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.6'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec', '~> 3.1.0'
26
+ spec.add_development_dependency 'faker'
27
+ spec.add_development_dependency 'pry'
28
+ end
@@ -0,0 +1,12 @@
1
+ require 'elasticsearch'
2
+
3
+ require 'fracas/dataset'
4
+ require 'fracas/version'
5
+
6
+ module Fracas
7
+ class << self
8
+ def connect(url = nil)
9
+ Dataset.new(url)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,119 @@
1
+ module Fracas
2
+ class Dataset
3
+ include Enumerable
4
+
5
+ attr_reader :client, :query, :results
6
+
7
+ def initialize(url)
8
+ @query = {
9
+ indices: [],
10
+ types: [],
11
+ filters: []
12
+ }
13
+
14
+ @client = Elasticsearch::Client.new(url: url)
15
+ end
16
+
17
+ def refresh
18
+ @client.indices.refresh index: indices
19
+ end
20
+
21
+ def from_indices(*indices)
22
+ merge(indices: indices)
23
+ end
24
+ alias :from_index :from_indices
25
+
26
+ def from_types(*types)
27
+ merge(types: types)
28
+ end
29
+ alias :from_type :from_types
30
+
31
+ def each(&block)
32
+ with_loaded_results { |ds| ds.all.each(&block) }
33
+ end
34
+
35
+ def all
36
+ with_loaded_results { |ds| ds.results['hits']['hits'].map { |hit| hit['_source'] } }
37
+ end
38
+
39
+ def count
40
+ with_loaded_results { |ds| ds.results['hits']['total'] }
41
+ end
42
+
43
+ def filter(condition = {})
44
+ merge filters: condition
45
+ end
46
+
47
+ def to_query
48
+ query = {
49
+ index: indices,
50
+ type: types,
51
+ body: {
52
+ query: queries,
53
+ filter: filters
54
+ }
55
+ }
56
+ end
57
+
58
+ def merge(query)
59
+ clone.tap { |clone| clone.merge!(query) }
60
+ end
61
+
62
+ def merge!(query)
63
+ @query = @query.merge(query) do |key, oldval, newval|
64
+ case key
65
+ when :indices, :types then oldval + newval
66
+ when :filters then oldval + [newval]
67
+ else raise "Unrecognized key! #{key.inspect}"
68
+ end
69
+ end
70
+ end
71
+
72
+ def load
73
+ clone.tap(&:load!)
74
+ end
75
+
76
+ def load!
77
+ @results = @client.search(to_query)
78
+ end
79
+
80
+ private
81
+
82
+ def with_loaded_results
83
+ yield results ? self : load
84
+ end
85
+
86
+ def indices
87
+ indices = @query[:indices]
88
+ indices.count.zero? ? '_all' : indices.join(',')
89
+ end
90
+
91
+ def types
92
+ types = @query[:types]
93
+ types.join(',') unless types.count.zero?
94
+ end
95
+
96
+ def queries
97
+ {
98
+ match_all: {}
99
+ }
100
+ end
101
+
102
+ def filters
103
+ filters = @query[:filters]
104
+ if filters.count.zero?
105
+ {
106
+ match_all: {}
107
+ }
108
+ else
109
+ {
110
+ and: filters.map { |w|
111
+ {
112
+ term: w
113
+ }
114
+ }
115
+ }
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,3 @@
1
+ module Fracas
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fracas, '.connect' do
4
+ it "should instantiate a Fracas::Dataset instance" do
5
+ Fracas.connect.should be_an_instance_of Fracas::Dataset
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fracas::Dataset, '#client' do
4
+ it "should return the ElasticSearch client object used by that Dataset object" do
5
+ FTS.client.should be_an_instance_of Elasticsearch::Transport::Client
6
+ proc { FTS.client.info }.should_not raise_error
7
+ end
8
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fracas::Dataset, '#filter' do
4
+ it "should add a filter to the search" do
5
+ store body: {title: "Title 1", comments_count: 5}
6
+ store body: {title: "Title 2", comments_count: 9}
7
+ FTS.refresh
8
+
9
+ titles = []
10
+ FTS.filter(comments_count: 5).each do |doc|
11
+ titles << doc['title']
12
+ end
13
+ titles.sort.should == ["Title 1"]
14
+ end
15
+
16
+ it "should return a new copy of the dataset" do
17
+ ds1 = FTS.filter(comments_count: 5)
18
+ ds1.query[:filters].should == [{comments_count: 5}]
19
+
20
+ ds2 = ds1.filter(title: "The Joy of Ferrets")
21
+ ds2.query[:filters].should == [{comments_count: 5}, {title: "The Joy of Ferrets"}]
22
+
23
+ ds1.query[:filters].should == [{comments_count: 5}]
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fracas::Dataset, '#from_indices' do
4
+ it "should specify the index(es) to be covered by the search" do
5
+ store index: 'index1', body: {title: "Title 1", comments_count: 1}
6
+ store index: 'index1', body: {title: "Title 2", comments_count: 2}
7
+ store index: 'index2', body: {title: "Title 3", comments_count: 3}
8
+ store index: 'index3', body: {title: "Title 4", comments_count: 4}
9
+ FTS.refresh
10
+
11
+ FTS.from_indices('index1').map{|d| d['comments_count']}.sort.should == [1, 2]
12
+ FTS.from_indices('index1', 'index2').map{|d| d['comments_count']}.sort.should == [1, 2, 3]
13
+ FTS.from_indices('index1', 'index3').map{|d| d['comments_count']}.sort.should == [1, 2, 4]
14
+ FTS.from_indices('index1').from_indices('index3').map{|d| d['comments_count']}.sort.should == [1, 2, 4]
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fracas::Dataset, '#from_types' do
4
+ it "should specify the type(s) to be returned by the search" do
5
+ store type: 'type1', body: {title: "Title 1", comments_count: 1}
6
+ store type: 'type1', body: {title: "Title 2", comments_count: 2}
7
+ store type: 'type2', body: {title: "Title 3", comments_count: 3}
8
+ store type: 'type3', body: {title: "Title 4", comments_count: 4}
9
+ FTS.refresh
10
+
11
+ FTS.from_types('type1').map{|d| d['comments_count']}.sort.should == [1, 2]
12
+ FTS.from_types('type1', 'type2').map{|d| d['comments_count']}.sort.should == [1, 2, 3]
13
+ FTS.from_types('type1', 'type3').map{|d| d['comments_count']}.sort.should == [1, 2, 4]
14
+ FTS.from_types('type1').from_types('type3').map{|d| d['comments_count']}.sort.should == [1, 2, 4]
15
+ end
16
+ end
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fracas::Dataset do
4
+ context "#each" do
5
+ it "should iterate over the JSON documents matching the search" do
6
+ store body: {title: "Title 1"}
7
+ store body: {title: "Title 2"}
8
+ FTS.refresh
9
+
10
+ titles = []
11
+ FTS.each do |doc|
12
+ titles << doc['title']
13
+ end
14
+ titles.sort.should == ["Title 1", "Title 2"]
15
+
16
+ FTS.each{|d| d}.should == FTS.all
17
+ end
18
+
19
+ it "should not load the results into the dataset on which it is called" do
20
+ store body: {title: "Title 1", comments_count: 5}
21
+ store body: {title: "Title 2", comments_count: 9}
22
+ store body: {title: "Title 3", comments_count: 5}
23
+ FTS.refresh
24
+ ds = FTS.filter(comments_count: 5)
25
+ ds.results.should be_nil
26
+
27
+ titles = []
28
+ ds.each { |doc| titles << doc['title'] }
29
+ titles.sort.should == ["Title 1", "Title 3"]
30
+
31
+ ds.results.should be_nil
32
+ end
33
+
34
+ it "should allow for the use of Enumerable methods" do
35
+ store body: {title: "Title 1", comments_count: 5}
36
+ store body: {title: "Title 2", comments_count: 9}
37
+ store body: {title: "Title 3", comments_count: 5}
38
+ FTS.refresh
39
+ ds = FTS.filter(comments_count: 5)
40
+ ds.results.should be_nil
41
+ ds.inject(0){|number, doc| number + doc['comments_count']}.should == 10
42
+ ds.results.should be_nil
43
+ end
44
+ end
45
+
46
+ context "#all" do
47
+ it "should return an array of matching documents without mutating the dataset" do
48
+ store body: {title: "Title 1", comments_count: 5}
49
+ store body: {title: "Title 2", comments_count: 9}
50
+ store body: {title: "Title 3", comments_count: 5}
51
+ FTS.refresh
52
+ ds = FTS.filter(comments_count: 5)
53
+ ds.results.should be_nil
54
+ all = ds.all
55
+ all.length.should == 2
56
+ all.map{|d| d['title']}.sort.should == ["Title 1", "Title 3"]
57
+ ds.results.should be_nil
58
+ end
59
+ end
60
+
61
+ context "#count" do
62
+ it "should return a count of matching documents without mutating the dataset" do
63
+ store body: {title: "Title 1", comments_count: 5}
64
+ store body: {title: "Title 2", comments_count: 9}
65
+ store body: {title: "Title 3", comments_count: 5}
66
+ FTS.refresh
67
+
68
+ ds = FTS.filter(comments_count: 5)
69
+ ds.results.should be_nil
70
+ ds.count.should == 2
71
+ ds.results.should be_nil
72
+ end
73
+ end
74
+
75
+ context "#load" do
76
+ it "should copy the dataset and load the results into it" do
77
+ store body: {title: "Title 1", comments_count: 5}
78
+ store body: {title: "Title 2", comments_count: 9}
79
+ store body: {title: "Title 3", comments_count: 5}
80
+ FTS.refresh
81
+ ds1 = FTS.filter(comments_count: 5)
82
+ ds1.results.should be_nil
83
+
84
+ ds2 = ds1.load
85
+ ds2.results.should_not be_nil
86
+
87
+ ds2.count.should == 2
88
+ ds2.all.length.should == 2
89
+
90
+ ds1.results.should be_nil
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,25 @@
1
+ require 'fracas'
2
+ require 'faker'
3
+ require 'pry'
4
+
5
+ FTS = Fracas.connect
6
+
7
+ RSpec.configure do |config|
8
+ config.expect_with(:rspec) { |c| c.syntax = [:expect, :should] }
9
+
10
+ config.before do
11
+ FTS.client.indices.delete index: '_all'
12
+ end
13
+ end
14
+
15
+ def store(index: 'fracas_test', type: 'fracas_test', body: {})
16
+ FTS.client.index index: index,
17
+ type: type,
18
+ body: {
19
+ title: Faker::Lorem.sentence,
20
+ body: Faker::Lorem.paragraph,
21
+ tags: Faker::Lorem.words(3),
22
+ published: rand > 0.5,
23
+ comments_count: rand(50)
24
+ }.merge(body)
25
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fracas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Hanks
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: elasticsearch
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 3.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 3.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: faker
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Write a longer description. Optional.
98
+ email:
99
+ - christopher.m.hanks@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .rspec
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - fracas.gemspec
111
+ - lib/fracas.rb
112
+ - lib/fracas/dataset.rb
113
+ - lib/fracas/version.rb
114
+ - spec/fracas/connect_spec.rb
115
+ - spec/fracas/dataset/client_spec.rb
116
+ - spec/fracas/dataset/filter_spec.rb
117
+ - spec/fracas/dataset/from_indices_spec.rb
118
+ - spec/fracas/dataset/from_types_spec.rb
119
+ - spec/fracas/dataset/results_spec.rb
120
+ - spec/spec_helper.rb
121
+ homepage: https://github.com/chanks/fracas
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.2.2
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Write a short summary. Required.
145
+ test_files:
146
+ - spec/fracas/connect_spec.rb
147
+ - spec/fracas/dataset/client_spec.rb
148
+ - spec/fracas/dataset/filter_spec.rb
149
+ - spec/fracas/dataset/from_indices_spec.rb
150
+ - spec/fracas/dataset/from_types_spec.rb
151
+ - spec/fracas/dataset/results_spec.rb
152
+ - spec/spec_helper.rb