date_range_solr_query 0.0.1.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,5 @@
1
+ notifications:
2
+ email: false
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in date_range_solr_query.gemspec
4
+ gemspec
@@ -0,0 +1,5 @@
1
+ Copyright (c) 2012-2013. The Board of Trustees of the Leland Stanford Junior University. All rights reserved.
2
+
3
+ Redistribution and use of this distribution in source and binary forms, with or without modification, are permitted provided that: The above copyright notice and this permission notice appear in all copies and supporting documentation; The name, identifiers, and trademarks of The Board of Trustees of the Leland Stanford Junior University are not used in advertising or publicity without the express prior written permission of The Board of Trustees of the Leland Stanford Junior University; Recipients acknowledge that this distribution is made available as a research courtesy, "as is", potentially with defects, without any obligation on the part of The Board of Trustees of the Leland Stanford Junior University to provide support, services, or repair;
4
+
5
+ THE BOARD OF TRUSTEES OF THE LELAND STANFORD JUNIOR UNIVERSITY DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN NO EVENT SHALL THE BOARD OF TRUSTEES OF THE LELAND STANFORD JUNIOR UNIVERSITY BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
@@ -0,0 +1,67 @@
1
+ [![Build Status](https://travis-ci.org/jkeck/date_range_solr_query.png?branch=master)](https://travis-ci.org/jkeck/date_range_solr_query)
2
+
3
+ # DateRangeSolrQuery
4
+
5
+ Given a start date, end date, and solr field this gem will return a string that can be sent to Solr as a range query.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'date_range_solr_query'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install date_range_solr_query
20
+
21
+ ## Usage
22
+
23
+ There are 3 options accepted on initialization: start_date, end_date, and field.
24
+
25
+ You can pass dates in the format of YYYY, YYYY-MM, or YYYY-MM-DD as the start_date and/or end_date and get the correctly formatted date range query. Below you will find various examples.
26
+
27
+ ### Start Dates Only
28
+
29
+ DateRangeSolrQuery.new(:start_date => "2013").range_query
30
+ => "[2013-01-01T00:00:00Z TO 2013-12-31T23:59:59Z]"
31
+
32
+ DateRangeSolrQuery.new(:start_date => "2013-01").range_query
33
+ => "[2013-01-01T00:00:00Z TO 2013-01-31T23:59:59Z]"
34
+
35
+ DateRangeSolrQuery.new(:start_date => "2013-03-21").range_query
36
+ => "[2013-03-21T00:00:00Z TO 2013-03-21T23:59:59Z]"
37
+
38
+
39
+ ### Start and End Dates
40
+
41
+ DateRangeSolrQuery.new(:start_date => "2013", :end_date => "2014").range_query
42
+ => "[2013-01-01T00:00:00Z TO 2014-12-31T23:59:59Z]"
43
+
44
+ DateRangeSolrQuery.new(:start_date => "2013-01", :end_date => "2013-03").range_query
45
+ => "[2013-01-01T00:00:00Z TO 2013-03-31T23:59:59Z]"
46
+
47
+ DateRangeSolrQuery.new(:start_date => "2013-03-21", :end_date => "2013-05-12").range_query
48
+ => "[2013-03-21T00:00:00Z TO 2013-05-12T23:59:59Z]"
49
+
50
+
51
+ ### Mixed Formats
52
+
53
+ DateRangeSolrQuery.new(:start_date => "2013-01", :end_date => "2013-03-12").range_query
54
+ => "[2013-01-01T00:00:00Z TO 2013-03-12T23:59:59Z]"
55
+
56
+ ### Field Option
57
+
58
+ DateRangeSolrQuery.new(:start_date => "2013", :field => "search_date").range_query
59
+ => "search_date:[2013-01-01T00:00:00Z TO 2013-12-31T23:59:59Z]"
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new Pull Request
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'date_range_solr_query/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "date_range_solr_query"
8
+ spec.version = DateRangeSolrQuery::VERSION
9
+ spec.authors = ["Jessie Keck"]
10
+ spec.email = ["jessie.keck@gmail.com"]
11
+ spec.description = %q{Generates a date range query to send to Solr.}
12
+ spec.summary = %q{Given a start date, end date, and solr field this gem will return a string that can be sent to Solr as a range query.}
13
+ spec.homepage = ""
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_dependency "activesupport"
22
+ spec.add_dependency "i18n"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,137 @@
1
+ require "date_range_solr_query/version"
2
+ require "active_support/core_ext"
3
+ require "date"
4
+
5
+ class DateRangeSolrQuery
6
+ attr_reader :start_date, :end_date, :range_query
7
+ def initialize(options={})
8
+ @options = options
9
+ @_start = @options[:start_date]
10
+ @_end = @options[:end_date] || ""
11
+ end
12
+
13
+ def range_query
14
+ [@options[:field], "[#{start_date} TO #{end_date}]"].compact.join(":")
15
+ end
16
+
17
+ def start_date
18
+ "#{DateTime.parse(simple_start).strftime("%Y-%m-%d")}T00:00:00Z"
19
+ end
20
+
21
+ def end_date
22
+ "#{(DateTime.parse(simple_end)).strftime("%Y-%m-%d")}T23:59:59Z"
23
+ end
24
+
25
+ private
26
+
27
+ def start_year
28
+ case
29
+ when full_date_search?
30
+ DateTime.parse(@_start).strftime("%Y")
31
+ when year_and_month_search?
32
+ DateTime.parse("#{@_start}-01").strftime("%Y")
33
+ when year_only_search?
34
+ DateTime.parse("#{@_start}-01-01").strftime("%Y")
35
+ end
36
+ end
37
+
38
+ def end_year
39
+ return start_year if single_date_search?
40
+ case
41
+ when full_end_date?
42
+ DateTime.parse(@_end).strftime("%Y")
43
+ when end_year_and_month_date?
44
+ DateTime.parse("#{@_end}-01").strftime("%Y")
45
+ when end_year_only_date?
46
+ DateTime.parse("#{@_end}-01-01").strftime("%Y")
47
+ end
48
+ end
49
+
50
+ def start_month
51
+ case
52
+ when full_date_search?
53
+ DateTime.parse(@_start).strftime("%m")
54
+ when year_and_month_search?
55
+ DateTime.parse("#{@_start}-01").strftime("%m")
56
+ when year_only_search?
57
+ DateTime.parse("#{@_start}-01-01").strftime("%m")
58
+ end
59
+ end
60
+
61
+ def end_month
62
+ case
63
+ when full_end_date?
64
+ DateTime.parse(@_end).strftime("%m")
65
+ when end_year_and_month_date?
66
+ DateTime.parse("#{@_end}-01").strftime("%m")
67
+ when (end_year_only_date? or year_only_search?)
68
+ # this should always be Dec. 31st
69
+ "12"
70
+ else
71
+ start_month
72
+ end
73
+ end
74
+
75
+ def start_day
76
+ case
77
+ when full_date_search?
78
+ DateTime.parse(@_start).strftime("%d")
79
+ when year_and_month_search?
80
+ DateTime.parse("#{@_start}-01").strftime("%d")
81
+ when year_only_search?
82
+ DateTime.parse("#{@_start}-01-01").strftime("%d")
83
+ end
84
+ end
85
+
86
+ def end_day
87
+ return (DateTime.parse(simple_start)).strftime("%d") if (single_date_search? and full_date_search?)
88
+ case
89
+ when full_end_date?
90
+ (DateTime.parse(@_end)).strftime("%d")
91
+ when (end_year_and_month_date? or year_only_search? or year_and_month_search?)
92
+ # this ensures we get the last day of the month.
93
+ ((DateTime.parse("#{end_year}-#{end_month}-01") + 1.month) - 1.day).strftime("%d")
94
+ when end_year_only_date?
95
+ # this should always be Dec. 31st
96
+ "31"
97
+ end
98
+ end
99
+
100
+ def simple_start
101
+ "#{start_year}-#{start_month}-#{start_day}"
102
+ end
103
+
104
+ def simple_end
105
+ "#{end_year}-#{end_month}-#{end_day}"
106
+ end
107
+
108
+ def single_date_search?
109
+ @_start and @_end.blank?
110
+ end
111
+
112
+ def year_only_search?
113
+ @_start.strip =~ /^\d{4}$/
114
+ end
115
+
116
+ def year_and_month_search?
117
+ @_start.strip =~ /^\d{4}-\d{2}$/
118
+ end
119
+
120
+ def full_date_search?
121
+ @_start.strip =~ /^\d{4}-\d{2}-\d{2}$/
122
+ end
123
+
124
+
125
+ def end_year_only_date?
126
+ !@_end.blank? and @_end.strip =~ /^\d{4}$/
127
+ end
128
+
129
+ def end_year_and_month_date?
130
+ !@_end.blank? and @_end.strip =~ /^\d{4}-\d{2}$/
131
+ end
132
+
133
+ def full_end_date?
134
+ !@_end.blank? and @_end.strip =~ /^\d{4}-\d{2}-\d{2}$/
135
+ end
136
+
137
+ end
@@ -0,0 +1,3 @@
1
+ class DateRangeSolrQuery
2
+ VERSION = "0.0.1.beta1"
3
+ end
@@ -0,0 +1,72 @@
1
+ require "spec_helper"
2
+ require "date_range_solr_query"
3
+
4
+ describe DateRangeSolrQuery do
5
+ describe "field option" do
6
+ it "should take a field as an option to the query" do
7
+ range_query = DateRangeSolrQuery.new(:start_date => "2013-03-21", :field => "my_search_field").range_query
8
+ range_query.should match /^my_search_field:\[2013-03-21.*/
9
+ end
10
+ it "should only return a range when no field options is provided" do
11
+ range_query = DateRangeSolrQuery.new(:start_date => "2013-03-21").range_query
12
+ range_query.should match /^\[2013-03-21.* TO 2013-03-21.*\]$/
13
+ end
14
+ end
15
+
16
+ describe "searching by one date" do
17
+ it "should search w/i a single date when only 1 full date is given" do
18
+ range_query = DateRangeSolrQuery.new(:start_date => "2013-03-21").range_query
19
+ range_query.should match /\[2013-03-21.* TO 2013-03-21.*\]/
20
+ end
21
+
22
+ it "should search w/i a month when a single year and month is given" do
23
+ range_query = DateRangeSolrQuery.new(:start_date => "2013-03").range_query
24
+ range_query.should match /\[2013-03-01.* TO 2013-03-31.*\]/
25
+ end
26
+
27
+ it "should add the appopriate ranges for a single year" do
28
+ range_query = DateRangeSolrQuery.new(:start_date => "2013").range_query
29
+ range_query.should match /\[2013-01-01.* TO 2013-12-31.*\]/
30
+ end
31
+ end
32
+
33
+ describe "searching a range of dates" do
34
+ it "should search w/i the appropriate dates" do
35
+ range_query = DateRangeSolrQuery.new(:start_date => "2013-03-21", :end_date => "2014-08-12").range_query
36
+ range_query.should match /\[2013-03-21.* TO 2014-08-12.*\]/
37
+ end
38
+
39
+ it "should search from the beginning of the first given month to the end of the last given month" do
40
+ range_query = DateRangeSolrQuery.new(:start_date => "2013-03", :end_date => "2014-08").range_query
41
+ range_query.should match /\[2013-03-01.* TO 2014-08-31.*\]/
42
+ end
43
+
44
+ it "should search from the beginning of the first given year to the end of the last given year" do
45
+ range_query = DateRangeSolrQuery.new(:start_date => "2013", :end_date => "2014").range_query
46
+ range_query.should match /\[2013-01-01.* TO 2014-12-31.*\]/
47
+ end
48
+
49
+ describe "mixed formats" do
50
+ it "should handle mixed month and year ranges" do
51
+ range_query = DateRangeSolrQuery.new(:start_date => "2013-02", :end_date => "2014").range_query
52
+ range_query.should match /\[2013-02-01.* TO 2014-12-31.*\]/
53
+ end
54
+
55
+ it "should handle mixed full and month ranges" do
56
+ range_query = DateRangeSolrQuery.new(:start_date => "2013-02", :end_date => "2014-04-15").range_query
57
+ range_query.should match /\[2013-02-01.* TO 2014-04-15.*\]/
58
+ end
59
+ end
60
+ end
61
+
62
+ describe "date oddities" do
63
+ it "should handle the end of the month dates properly" do
64
+ range_query = DateRangeSolrQuery.new(:start_date => "2013-02", :end_date => "2014-04").range_query
65
+ range_query.should match /\[2013-02-01.* TO 2014-04-30.*\]/
66
+ end
67
+ it "should handle when the end date is nil" do
68
+ range_query = DateRangeSolrQuery.new(:start_date => "2013-03", :end_date => nil).range_query
69
+ range_query.should match /\[2013-03-01.* TO 2013-03-31.*\]/
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: date_range_solr_query
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.beta1
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Jessie Keck
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: i18n
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Generates a date range query to send to Solr.
95
+ email:
96
+ - jessie.keck@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - .travis.yml
104
+ - Gemfile
105
+ - LICENSE.txt
106
+ - README.md
107
+ - Rakefile
108
+ - date_range_solr_query.gemspec
109
+ - lib/date_range_solr_query.rb
110
+ - lib/date_range_solr_query/version.rb
111
+ - spec/lib/date_range_solr_query_spec.rb
112
+ - spec/spec_helper.rb
113
+ homepage: ''
114
+ licenses:
115
+ - MIT
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>'
130
+ - !ruby/object:Gem::Version
131
+ version: 1.3.1
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 1.8.24
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Given a start date, end date, and solr field this gem will return a string
138
+ that can be sent to Solr as a range query.
139
+ test_files:
140
+ - spec/lib/date_range_solr_query_spec.rb
141
+ - spec/spec_helper.rb