ruby-sun-times 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7ff22355b53b8e849bdb6620d1e51552a72840e0
4
+ data.tar.gz: ce1cccf30fa5f6461918853588010f534a2f3ffc
5
+ SHA512:
6
+ metadata.gz: 6c9c521a94cf297ab45fd32ba1f87dfe64b0beac36b1ad89c6d870e477fbb3c7a3d7248070b04d23f4abce20d188a6d61baa873d10dceb5d9c44b97fe2fe42b9
7
+ data.tar.gz: 96b2cec093ae213b771179cdc9939b03d151a7c0d0e3b2093be39dbb90c4d58c3100b876014970acdd8b67be000de2d66fce9947986d21177da47ee0505d1cd0
@@ -0,0 +1,3 @@
1
+ doc/*
2
+ pkg/*
3
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Rakefile CHANGED
@@ -1,48 +1,9 @@
1
1
  require 'rubygems'
2
- require 'rake/gempackagetask'
3
- require 'rake/rdoctask'
4
- require 'rake/testtask'
5
- require 'rake/clean'
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
6
4
 
7
- $:.unshift(File.dirname(__FILE__) + '/lib')
8
- require 'sun_times'
5
+ task :default => :spec
9
6
 
10
- RDOC_OPTS = ['--quiet', '--title', 'Sun Times Calculator', '--main', 'README', '--inline-source']
11
- CLEAN.include 'doc'
12
-
13
- task :default => :test
14
-
15
- spec = Gem::Specification.new do |s|
16
- s.name = 'ruby-sun-times'
17
- s.summary = 'Module which calculates sunrise and sunset times'
18
- s.version = SunTimes::VERSION::STRING
19
-
20
- s.homepage = 'http://github.com/joeyates/ruby-sun-times'
21
- s.author = 'Joe Yates'
22
- s.email = 'joe.g.yates@gmail.com'
23
-
24
- s.files = ['README', 'COPYING', 'Rakefile'] + FileList['{lib,test}/**/*.rb']
25
- s.require_paths = ['lib']
26
-
27
- s.has_rdoc = true
28
- s.rdoc_options += RDOC_OPTS
29
- s.extra_rdoc_files = ['README', 'COPYING']
30
-
31
- s.test_file = 'test/test_all.rb'
32
- end
33
-
34
- Rake::TestTask.new do |t|
35
- t.libs << 'test'
36
- t.test_files = FileList['test/*_test.rb']
37
- t.verbose = true
38
- end
39
-
40
- Rake::RDocTask.new do |rdoc|
41
- rdoc.rdoc_dir = 'doc/rdoc'
42
- rdoc.options += RDOC_OPTS
43
- rdoc.main = "README"
44
- rdoc.rdoc_files.add ['README', 'COPYING', 'lib/**/*.rb']
45
- end
46
-
47
- Rake::GemPackageTask.new(spec) do |pkg|
7
+ RSpec::Core::RakeTask.new do |t|
8
+ t.pattern = 'spec/**/*_spec.rb'
48
9
  end
@@ -26,26 +26,17 @@
26
26
  require 'date'
27
27
 
28
28
  module SunTimes
29
-
30
- module VERSION #:nodoc:
31
- MAJOR = 0
32
- MINOR = 1
33
- TINY = 2
34
-
35
- STRING = [MAJOR, MINOR, TINY].join('.')
36
- end
37
-
38
29
  DEFAULT_ZENITH = 90.83333
39
30
  KNOWN_EVENTS = [:rise, :set]
40
31
  DEGREES_PER_HOUR = 360.0 / 24.0
41
32
 
42
33
  # Helper method: calculates sunrise, with the same parameters as calculate
43
- def SunTimes.rise(date, latitude, longitude, options = {})
34
+ def self.rise(date, latitude, longitude, options = {})
44
35
  calculate(:rise, date, latitude, longitude, options)
45
36
  end
46
37
 
47
38
  # Helper method: calculates sunset, with the same parameters as calculate
48
- def SunTimes.set(date, latitude, longitude, options = {})
39
+ def self.set(date, latitude, longitude, options = {})
49
40
  calculate(:set, date, latitude, longitude, options)
50
41
  end
51
42
 
@@ -53,7 +44,7 @@ module SunTimes
53
44
  #
54
45
  # ==== Parameters
55
46
  # * +event+ - One of :rise, :set.
56
- # * +date+ - An object that responds to yday.
47
+ # * +date+ - An object that responds to :to_datetime.
57
48
  # * +latitude+ - The latitude of the location in degrees.
58
49
  # * +longitude+ - The longitude of the location in degrees.
59
50
  # * +options+ - Additional option is <tt>:zenith</tt>.
@@ -61,7 +52,8 @@ module SunTimes
61
52
  # ==== Example
62
53
  # SunTimes.calculate(:rise, Date.new(2010, 3, 8), 43.779, 11.432)
63
54
  # > Mon Mar 08 05:39:53 UTC 2010
64
- def SunTimes.calculate(event, date, latitude, longitude, options = {})
55
+ def self.calculate(event, date, latitude, longitude, options = {})
56
+ datetime = date.to_datetime
65
57
  raise "Unknown event '#{ event }'" if KNOWN_EVENTS.find_index(event).nil?
66
58
  zenith = options.delete(:zenith) || DEFAULT_ZENITH
67
59
 
@@ -70,7 +62,7 @@ module SunTimes
70
62
 
71
63
  # t
72
64
  base_time = event == :rise ? 6.0 : 18.0
73
- approximate_time = date.yday + (base_time - longitude_hour) / 24.0
65
+ approximate_time = datetime.yday + (base_time - longitude_hour) / 24.0
74
66
 
75
67
  # M
76
68
  mean_sun_anomaly = (0.9856 * approximate_time) - 3.289
@@ -126,24 +118,15 @@ module SunTimes
126
118
  gmt_hours -= 24.0 if gmt_hours > 24
127
119
  gmt_hours += 24.0 if gmt_hours < 0
128
120
 
129
- case
130
- when date.respond_to?( :offset )
131
- offset_hours = date.offset * 24
132
- when date.respond_to?( :gmt_offset )
133
- offset_hours = date.gmt_offset / 3600
134
- else
135
- offset_hours = nil
136
- end
121
+ offset_hours = datetime.offset * 24.0
137
122
 
138
- if ! offset_hours.nil?
139
- if gmt_hours + offset_hours < 0
140
- next_day = Date.new(date.year, date.month, date.day + 1)
141
- return calculate(event, next_day, latitude, longitude, options = {})
142
- end
143
- if gmt_hours + offset_hours > 24
144
- previous_day = Date.new(date.year, date.month, date.day + 1)
145
- return calculate(event, previous_day, latitude, longitude, options = {})
146
- end
123
+ if gmt_hours + offset_hours < 0
124
+ next_day = datetime.next_day
125
+ return calculate(event, next_day.new_offset, latitude, longitude, options = {})
126
+ end
127
+ if gmt_hours + offset_hours > 24
128
+ previous_day = datetime.prev_day
129
+ return calculate(event, previous_day.new_offset, latitude, longitude, options = {})
147
130
  end
148
131
 
149
132
  hour = gmt_hours.floor
@@ -151,20 +134,20 @@ module SunTimes
151
134
  minute = hour_remainder.floor
152
135
  seconds = (hour_remainder - minute) * 60.0
153
136
 
154
- Time.gm(date.year, date.month, date.day, hour, minute, seconds)
137
+ Time.gm(datetime.year, datetime.month, datetime.day, hour, minute, seconds)
155
138
  end
156
139
 
157
140
  private
158
141
 
159
- def SunTimes.degrees_to_radians(d)
142
+ def self.degrees_to_radians(d)
160
143
  d.to_f / 360.0 * 2.0 * Math::PI
161
144
  end
162
145
 
163
- def SunTimes.radians_to_degrees(r)
146
+ def self.radians_to_degrees(r)
164
147
  r.to_f * 360.0 / (2.0 * Math::PI)
165
148
  end
166
149
 
167
- def SunTimes.coerce_degrees(d)
150
+ def self.coerce_degrees(d)
168
151
  if d < 0
169
152
  d += 360
170
153
  return coerce_degrees(d)
@@ -175,5 +158,4 @@ module SunTimes
175
158
  end
176
159
  d
177
160
  end
178
-
179
161
  end
@@ -0,0 +1,9 @@
1
+ module SunTimes
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 3
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.unshift(File.expand_path('../lib/', __FILE__))
3
+
4
+ require 'sun_times/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'ruby-sun-times'
8
+ s.summary = 'Module which calculates sunrise and sunset times'
9
+ s.version = SunTimes::VERSION::STRING
10
+
11
+ s.homepage = 'https://github.com/joeyates/ruby-sun-times'
12
+ s.author = 'Joe Yates'
13
+ s.email = 'joe.g.yates@gmail.com'
14
+
15
+ s.files = `git ls-files`.split($\)
16
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ s.test_files = s.files.grep(%r{^spec/})
18
+ s.require_paths = ['lib']
19
+
20
+ s.add_development_dependency 'pry'
21
+ s.add_development_dependency 'rspec'
22
+ end
@@ -0,0 +1,10 @@
1
+ $:.unshift(File.expand_path('../lib', File.dirname(__FILE__)))
2
+
3
+ require 'sun_times'
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+ config.order = 'random'
10
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe SunTimes do
4
+ let(:day) { Date.new(2010, 3, 8) }
5
+ let(:latitude) { 43.779 }
6
+ let(:longitude) { 11.432 }
7
+ let(:rise) { Time.gm(2010, 3, 8, 5, 39, 53) }
8
+ let(:set) { Time.gm(2010, 3, 8, 17, 11, 16) }
9
+
10
+ describe '#calculate' do
11
+ context ':rise' do
12
+ it 'returns the sunrise time' do
13
+ result = SunTimes.calculate(:rise, day, latitude, longitude)
14
+ expect(result).to be_within(1).of(rise)
15
+ end
16
+ end
17
+
18
+ context ':set' do
19
+ it 'returns the sunset time' do
20
+ result = SunTimes.calculate(:set, day, latitude, longitude)
21
+ expect(result).to be_within(1).of(set)
22
+ end
23
+ end
24
+
25
+ context 'unknown event' do
26
+ it 'fails' do
27
+ expect {
28
+ SunTimes.calculate(:foo, day, latitude, longitude)
29
+ }.to raise_error(RuntimeError, /unknown event/i)
30
+ end
31
+ end
32
+
33
+ context 'with timezone' do
34
+ let(:zone) { Rational(-8, 24) }
35
+ let(:day_with_zone) { DateTime.new(2011, 12, 13, 0, 0, 0, zone) }
36
+
37
+ it 'calculates according to the supplied value' do
38
+ set = SunTimes.calculate(:set, day_with_zone, 45.52, -122.681944)
39
+ result_datetime = DateTime.new(set.year, set.month, set.day, set.hour, set.min, set.sec, 0)
40
+
41
+ expect(result_datetime).to be > day_with_zone
42
+ expect(result_datetime).to be <= day_with_zone + 1
43
+ end
44
+ end
45
+
46
+ context 'midnight sun' do
47
+ # North Cape
48
+ let(:midsummer) { Date.new(2010, 6, 21) }
49
+ let(:latitude) { 71.170219 }
50
+ let(:longitude) { 25.785556 }
51
+
52
+ it 'rise is nil' do
53
+ result = SunTimes.calculate(:rise, midsummer, latitude, longitude)
54
+ expect(result).to be_nil
55
+ end
56
+
57
+ it 'set is nil' do
58
+ result = SunTimes.calculate(:set, midsummer, latitude, longitude)
59
+ expect(result).to be_nil
60
+ end
61
+ end
62
+
63
+ context 'last day of the year' do
64
+ let(:zone) { Rational(-8, 24) }
65
+ let(:day) { DateTime.new(2013, 12, 31, 8, 59, 5, zone) }
66
+
67
+ it 'calculates correctly' do
68
+ SunTimes.calculate(:set, day, 47.5, -122)
69
+ end
70
+ end
71
+ end
72
+
73
+ describe '#rise' do
74
+ it 'returns the sunrise time' do
75
+ result = SunTimes.rise(day, latitude, longitude)
76
+ expect(result).to be_within(1).of(rise)
77
+ end
78
+ end
79
+
80
+ describe '#set' do
81
+ it 'returns the sunset time' do
82
+ result = SunTimes.set(day, latitude, longitude)
83
+ expect(result).to be_within(1).of(set)
84
+ end
85
+ end
86
+ end
@@ -6,37 +6,7 @@ require 'date'
6
6
 
7
7
  class SunTimesTest < Test::Unit::TestCase
8
8
 
9
- def test_rise_20100308_pontassieve
10
- rise = SunTimes.calculate(:rise, Date.new(2010, 3, 8), 43.779, 11.432)
11
- assert_equal(Time.gm(2010, 3, 8, 5, 39, 53), rise)
12
- end
13
-
14
- def test_set_20100308_pontassieve
15
- set = SunTimes.calculate(:set, Date.new(2010, 3, 8), 43.779, 11.432)
16
- assert_equal(Time.gm(2010, 3, 8, 17, 11, 16), set)
17
- end
18
-
19
- def test_rise_helper
20
- rise = SunTimes.rise(Date.new(2010, 3, 8), 43.779, 11.432)
21
- assert_equal(Time.gm(2010, 3, 8, 5, 39, 53), rise)
22
- end
23
-
24
- def test_set_helper
25
- set = SunTimes.set(Date.new(2010, 3, 8), 43.779, 11.432)
26
- assert_equal(Time.gm(2010, 3, 8, 17, 11, 16), set)
27
- end
28
-
29
- def test_midnight_sun_on_20100621_north_cape
30
- rise = SunTimes.calculate(:rise, Date.new(2010, 6, 21), 71.170219, 25.785556)
31
- assert_nil(rise)
32
- set = SunTimes.calculate(:set, Date.new(2010, 6, 21), 71.170219, 25.785556)
33
- assert_nil(set)
34
- end
35
-
36
- def test_unknown_event
37
- assert_raise(RuntimeError) { SunTimes.calculate(:foo, Date.new(2010, 3, 8), 43.779, 11.432) }
38
- end
39
-
9
+ =begin
40
10
  def test_time
41
11
  datetime = Time.gm(2010, 6, 13, 0, 0, 0)
42
12
  set = SunTimes.calculate(:set, datetime, 43.779, 11.432)
@@ -49,12 +19,7 @@ class SunTimesTest < Test::Unit::TestCase
49
19
  assert_equal(Time.gm(2010, 6, 13, 18, 56, 55), set)
50
20
  end
51
21
 
52
- def test_respects_timezone_if_supplied
53
- pst = DateTime.new(2011, 12, 13, 0, 0, 0, Rational(-8, 24))
54
- set = SunTimes.calculate(:set, pst, 45.52, -122.681944)
55
- result_datetime = DateTime.new(set.year, set.month, set.day, set.hour, set.min, set.sec, 0)
56
- assert(pst < result_datetime)
57
- assert(pst + 1 > result_datetime)
22
+ def test_31st_december
58
23
  end
59
-
24
+ =end
60
25
  end
metadata CHANGED
@@ -1,66 +1,86 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ruby-sun-times
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
5
  platform: ruby
6
- authors:
6
+ authors:
7
7
  - Joe Yates
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
-
12
- date: 2011-12-20 00:00:00 +00:00
13
- default_executable:
14
- dependencies: []
15
-
11
+ date: 2014-01-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: rspec
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'
16
41
  description:
17
42
  email: joe.g.yates@gmail.com
18
43
  executables: []
19
-
20
44
  extensions: []
21
-
22
- extra_rdoc_files:
23
- - README
45
+ extra_rdoc_files: []
46
+ files:
47
+ - .gitignore
48
+ - .rspec
24
49
  - COPYING
25
- files:
50
+ - Gemfile
26
51
  - README
27
- - COPYING
28
52
  - Rakefile
29
53
  - lib/sun_times.rb
54
+ - lib/sun_times/version.rb
55
+ - ruby-sun-times.gemspec
56
+ - spec/spec_helper.rb
57
+ - spec/unit/ruby_sun_times_spec.rb
30
58
  - test/calculate_test.rb
31
59
  - test/test_all.rb
32
- has_rdoc: true
33
- homepage: http://github.com/joeyates/ruby-sun-times
60
+ homepage: https://github.com/joeyates/ruby-sun-times
34
61
  licenses: []
35
-
62
+ metadata: {}
36
63
  post_install_message:
37
- rdoc_options:
38
- - --quiet
39
- - --title
40
- - Sun Times Calculator
41
- - --main
42
- - README
43
- - --inline-source
44
- require_paths:
64
+ rdoc_options: []
65
+ require_paths:
45
66
  - lib
46
- required_ruby_version: !ruby/object:Gem::Requirement
47
- requirements:
48
- - - ">="
49
- - !ruby/object:Gem::Version
50
- version: "0"
51
- version:
52
- required_rubygems_version: !ruby/object:Gem::Requirement
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: "0"
57
- version:
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
58
77
  requirements: []
59
-
60
78
  rubyforge_project:
61
- rubygems_version: 1.3.5
79
+ rubygems_version: 2.0.3
62
80
  signing_key:
63
- specification_version: 3
81
+ specification_version: 4
64
82
  summary: Module which calculates sunrise and sunset times
65
- test_files:
66
- - test/test_all.rb
83
+ test_files:
84
+ - spec/spec_helper.rb
85
+ - spec/unit/ruby_sun_times_spec.rb
86
+ has_rdoc: