barometer-support 0.0.2

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: 7f3d9975aa980a8d852caa356dc9b1ab4e001cb8
4
+ data.tar.gz: 8a757382498ccd83a72554e3d641b955c5c5ca04
5
+ SHA512:
6
+ metadata.gz: 5a54d871a3798f06dc9e1b4e5f2909ae829790ac49d75a9d744e27bcbc024a86927a7cd09648b4a2e645bd48a93b140e5b3fcd680c97b82bca9db36a417de53c
7
+ data.tar.gz: f2a367aa7d6e677ec1fa7ce7463635c2e7742d7fec98ee5ced933a1599735b877caafd88baf2d30e50c83f7fd5a97e22465e84ba7f8197461aed032af7c0264a
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 barometer-support.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Mark G
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,52 @@
1
+ # Barometer::Support
2
+
3
+ Support files for running [Barometer](https://github.com/attack/barometer) related specs.
4
+
5
+ ## Usage
6
+
7
+ ### Matchers
8
+
9
+ Rpsec matchers for weather data specific assertions.
10
+
11
+ In your spec_helper.rb:
12
+ ```ruby
13
+ require 'barometer-support'
14
+ include Barometer::Support::Matchers
15
+ ```
16
+
17
+ ### API Key Configuration
18
+
19
+ Parses API keys from a standard ~/.barometer file.
20
+
21
+ Example ~/.barometer yaml file:
22
+ ```yaml
23
+ foo:
24
+ apikey: 'secret'
25
+ ```
26
+
27
+ Example usage:
28
+ ```ruby
29
+ FOO_APIKEY = Barometer::Support::KeyFileParser.find(:foo, :apikey)
30
+ ```
31
+
32
+ ## Installation
33
+
34
+ Add this line to your application's Gemfile:
35
+
36
+ gem 'barometer-support'
37
+
38
+ And then execute:
39
+
40
+ $ bundle
41
+
42
+ Or install it yourself as:
43
+
44
+ $ gem install barometer-support
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'barometer/support/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'barometer-support'
7
+ spec.version = Barometer::Support::VERSION
8
+ spec.platform = Gem::Platform::RUBY
9
+ spec.authors = ['Mark G']
10
+ spec.email = ['barometer@attackcorp.com']
11
+ spec.description = 'Barometer support files'
12
+ spec.summary = spec.description
13
+ spec.homepage = 'http://github.com/attack/barometer-support'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.test_files = spec.files.grep(%r{^spec/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_dependency 'barometer', '~> 0.9.2'
21
+ spec.add_dependency 'rspec', '>= 2.11'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake'
25
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'support/version'
2
+ require_relative 'support/key_file_parser'
3
+ require_relative 'support/matchers'
4
+ require_relative 'support/factory'
5
+
6
+ module Barometer
7
+ module Support
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ module Barometer
2
+ module Support
3
+ module Factory
4
+ def build_query
5
+ double(
6
+ :query,
7
+ q: 'foo',
8
+ format: :unknown,
9
+ units: :metric,
10
+ geo: nil,
11
+ metric?: true
12
+ )
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ module Barometer
2
+ KEY_FILE = File.expand_path(File.join('~', '.barometer'))
3
+
4
+ module Support
5
+ class KeyFileParser
6
+ def self.find(*paths)
7
+ if File.exists?(KEY_FILE)
8
+ keys = YAML.load_file(KEY_FILE)
9
+
10
+ paths.each do |path|
11
+ if keys && keys.has_key?(path.to_s)
12
+ keys = keys.fetch(path.to_s)
13
+ else
14
+ keys = nil
15
+ end
16
+ end
17
+ keys
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,10 @@
1
+ require_relative 'matchers/have_data'
2
+ require_relative 'matchers/have_field'
3
+ require_relative 'matchers/have_forecast'
4
+
5
+ module Barometer
6
+ module Support
7
+ module Matchers
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,38 @@
1
+ require 'rspec/expectations'
2
+
3
+ module Barometer
4
+ module Support
5
+ module Matchers
6
+ module Formats
7
+ def is_of_format?(format, value)
8
+ value.match(_find_regex(format))
9
+ end
10
+
11
+ def _find_regex(format)
12
+ case format
13
+ when :time
14
+ /^\d{1,4}-\d{1,2}-\d{1,2} \d{2}:\d{2}:\d{2} [\-\+\d]{5}$/i
15
+ when :optional_string
16
+ /^[\w \.-]*$/i
17
+ when :string
18
+ /^[\w \.,-]{2,}$/i
19
+ when :number
20
+ /^\d{1,3}$/i
21
+ when :float
22
+ /^[\d\.]{1,5}$/i
23
+ when :temperature
24
+ /^-?[0-9\.]{1,20}[ ]?[cfCF]?$/i
25
+ when :pressure
26
+ /^[0-9\.]{1,20}[ ]?[a-zA-Z]{0,3}$/i
27
+ when :vector
28
+ /^[0-9\.]{1,20} (?:mph|kph) (?:[nesw]{1,3}(?:orth|outh|t|ast)?|@ [0-9\.]{1,5} degrees)$/i
29
+ when :distance
30
+ /^[0-9\.]{1,20}[ ]?k?m?$/i
31
+ when Regexp
32
+ format
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,48 @@
1
+ require_relative 'walker'
2
+ require_relative 'formats'
3
+
4
+ module Barometer
5
+ module Support
6
+ module Matchers
7
+ def have_data(*paths)
8
+ HaveData.new(*paths)
9
+ end
10
+
11
+ class HaveData
12
+ include Barometer::Support::Matchers::Formats
13
+
14
+ def initialize(*paths)
15
+ @paths = paths
16
+ end
17
+
18
+ def matches?(subject)
19
+ @result = Walker.new(subject).follow(@paths)
20
+
21
+ if @format
22
+ is_of_format?(@format, @result)
23
+ else
24
+ @result == @value || @result.to_f == @value
25
+ end
26
+ end
27
+
28
+ def failure_message
29
+ "expected that '#{@result}' matches '#{@value || @format}'"
30
+ end
31
+
32
+ def description
33
+ "have correct value for #{@paths.join('.')}"
34
+ end
35
+
36
+ def as_value(value)
37
+ @value = value
38
+ self
39
+ end
40
+
41
+ def as_format(format)
42
+ @format = format
43
+ self
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,130 @@
1
+ require 'rspec/expectations'
2
+
3
+ module Barometer
4
+ module Support
5
+ module Matchers
6
+ def have_field(field)
7
+ HaveField.new(field)
8
+ end
9
+
10
+ class HaveField
11
+ def initialize(field)
12
+ @field = field
13
+ end
14
+
15
+ def matches?(subject)
16
+ @subject = subject
17
+ has_field? &&
18
+ type_casts_as_type? &&
19
+ sets_value?
20
+ end
21
+
22
+ def failure_message
23
+ "Expected #{expectation} (#{@problem})"
24
+ end
25
+
26
+ def description
27
+ "have data field \"#{@field}\""
28
+ end
29
+
30
+ def of_type(type)
31
+ @type = type
32
+ self
33
+ end
34
+
35
+ protected
36
+
37
+ def has_field?
38
+ assert @subject.respond_to?(@field), "does not have field '#{@field}'"
39
+ end
40
+
41
+ def type_casts_as_type?
42
+ if type_is_time?
43
+ set_value "2013-01-01 10:15:30 am"
44
+ elsif type_is_sun?
45
+ rise = Time.utc(2013,1,1,10,15,30)
46
+ set = Time.utc(2013,1,1,18,14,56)
47
+ set_value Data::Sun.new(rise: rise, set: set)
48
+ else
49
+ set_value 10
50
+ end
51
+ assert value.is_a?(@type), "#{@field} does not typecast as #{@type}"
52
+ end
53
+
54
+ def sets_value?
55
+ if type_is_time?
56
+ set_value "10 15 30 2013 01 01 am", "%H %M %S %Y %m %d %p"
57
+ assert value.to_i == Time.utc(2013,01,01,10,15,30).to_i, "expected value of '2013-01-01 10:15:30 am', got '#{print_value}'"
58
+ elsif type_is_sun?
59
+ rise = Time.utc(2013,1,1,10,15,30)
60
+ set = Time.utc(2013,1,1,18,14,56)
61
+ set_value Data::Sun.new(rise: rise, set: set)
62
+ assert print_value == "rise: 10:15, set: 18:14", "expected value of 'rise: 10:15, set: 18:14'', got '#{print_value}'"
63
+ else
64
+ set_value 10
65
+ assert value.to_i == 10, "expected value of '10', got '#{value.to_i}'"
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def value
72
+ @subject.send(@field)
73
+ end
74
+
75
+ def set_value(*value)
76
+ if type_is_time?
77
+ @subject.send("#{@field}=", value)
78
+ else
79
+ @subject.send("#{@field}=", *value)
80
+ end
81
+ end
82
+
83
+ def print_value
84
+ value.to_s
85
+ end
86
+
87
+ def metric_units
88
+ @type.send(:new, true).units
89
+ end
90
+
91
+ def imperial_units
92
+ @type.send(:new, false).units
93
+ end
94
+
95
+ def expectation
96
+ "\"#{@field}\" to be a #{@type}"
97
+ end
98
+
99
+ def value_responds_to_metric?
100
+ if type_is_time?
101
+ false
102
+ elsif @type == Float
103
+ # rubinius does not like Float.new being called on the next line
104
+ # so avoid it
105
+ false
106
+ else
107
+ @type.respond_to?(:new) && @type.new.respond_to?(:metric)
108
+ end
109
+ end
110
+
111
+ def type_is_time?
112
+ @type == Time
113
+ end
114
+
115
+ def type_is_sun?
116
+ @type == Data::Sun
117
+ end
118
+
119
+ def assert(test, failure_message)
120
+ if test
121
+ true
122
+ else
123
+ @problem = failure_message
124
+ false
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,48 @@
1
+ require_relative 'walker'
2
+ require_relative 'formats'
3
+
4
+ module Barometer
5
+ module Support
6
+ module Matchers
7
+ def have_forecast(*paths)
8
+ HaveForecast.new(*paths)
9
+ end
10
+
11
+ class HaveForecast
12
+ include Barometer::Support::Matchers::Formats
13
+
14
+ def initialize(*paths)
15
+ @paths = paths
16
+ end
17
+
18
+ def matches?(subject)
19
+ @result = Walker.new(subject.forecast[0]).follow(@paths)
20
+
21
+ if @format
22
+ is_of_format?(@format, @result)
23
+ else
24
+ @result == @value || @result.to_f == @value
25
+ end
26
+ end
27
+
28
+ def failure_message
29
+ "expected that '#{@result}' matches '#{@value || @format}'"
30
+ end
31
+
32
+ def description
33
+ "have correct forecast value for #{@paths.join('.')}"
34
+ end
35
+
36
+ def as_value(value)
37
+ @value = value
38
+ self
39
+ end
40
+
41
+ def as_format(format)
42
+ @format = format
43
+ self
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,25 @@
1
+ module Barometer
2
+ module Support
3
+ module Matchers
4
+ class Walker
5
+ attr_reader :value
6
+
7
+ def initialize(value)
8
+ @value = value
9
+ end
10
+
11
+ def follow(paths)
12
+ path_value = value
13
+ paths.each do |path|
14
+ path_value = path_value.send(path)
15
+ end
16
+ if path_value.respond_to?(:strftime)
17
+ path_value.strftime("%Y-%m-%d %H:%M:%S %z")
18
+ else
19
+ path_value.to_s
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module Barometer
2
+ module Support
3
+ VERSION = '0.0.2'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: barometer-support
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Mark G
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: barometer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '2.11'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '2.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Barometer support files
70
+ email:
71
+ - barometer@attackcorp.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - barometer-support.gemspec
82
+ - lib/barometer/support.rb
83
+ - lib/barometer/support/factory.rb
84
+ - lib/barometer/support/key_file_parser.rb
85
+ - lib/barometer/support/matchers.rb
86
+ - lib/barometer/support/matchers/formats.rb
87
+ - lib/barometer/support/matchers/have_data.rb
88
+ - lib/barometer/support/matchers/have_field.rb
89
+ - lib/barometer/support/matchers/have_forecast.rb
90
+ - lib/barometer/support/matchers/walker.rb
91
+ - lib/barometer/support/version.rb
92
+ homepage: http://github.com/attack/barometer-support
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.0.3
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Barometer support files
116
+ test_files: []
117
+ has_rdoc: