Skip to content

Commit

Permalink
Making way for worldwide support
Browse files Browse the repository at this point in the history
  • Loading branch information
rthbound committed Nov 21, 2013
1 parent 892e9ca commit 8f8d230
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 37 deletions.
55 changes: 21 additions & 34 deletions lib/usno/transit.rb
Original file line number Diff line number Diff line change
@@ -1,51 +1,38 @@
require "pay_dirt"
require_relative "transit/version"
require_relative "transit/states"
require_relative "transit/bodies"

require_relative "transit/us_request"

module USNO
module Transit
BODIES = {
"Sun" => 10, "Moon" => 11, "Mercury" => 1,
"Venus" => 2,"Jupiter" => 5, "Mars" => 4,
"Saturn" => 6, "Uranus" => 7, "Neptune" => 8,
"Pluto" => 9, "Achernar" => -1, "Adhara" => -2,
"Aldebaran" => -3, "Altair" => -4, "Antares" => -5,
"Arcturus" => -6, "Betelgeuse" => -7, "Canopus" => -8,
"Capella" => -9, "Deneb" => -10, "Fomalhaut" => -11,
"Hadar" => -12, "Mimosa" => -13, "Polaris" => -14,
"Pollux" => -15, "Procyon" => -16, "Regulus" => -17,
"Rigel" => -18, "RigilKentaurus" => -19, "Vega" => -22,
"Sirius" => -20, "Spica" => -21,
}

class View < PayDirt::Base
def initialize(options = {})
raise "Cannot instantiate this class directly" if self.class.name == "View"
raise "Cannot instantiate this class directly" if self.class.name.to_s =~ /View/

# Default options
options = {
request_class: USNO::Transit::USRequest,
object: USNO::Transit::BODIES.fetch(self.class.name.split("::")[-1]) {
raise "Celestial object not recognized"
},
z_meters: 0,
date: Time.now,
days: 5,
}.merge(options)
options = form_options(options)

load_options(:city, :state, options)
load_options(options)
end

def call
result(true, @request_class.new({
obj: @object,
city: @city,
state: USNO::Transit::States.by_key_or_value(@state),
z_meters: @z_meters,
date: @date,
days: @days
}).call.data)
result(true, @request_class.new(@request_options).call.data)
end

private
def form_options(options)
{
request_class: USNO::Transit::USRequest,
object: USNO::Transit::Bodies.fetch(self.class.name.to_s.split("::")[-1]),
z_meters: 0,
date: Time.now,
days: 5,
}.merge(options).merge!({
request_options: options.dup.reject do |k,_|
k.to_s == "request_class"
end
})
end
end

Expand Down
28 changes: 28 additions & 0 deletions lib/usno/transit/bodies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module USNO
module Transit
module Bodies
def self.fetch(k)
self.hash.fetch(k) {
raise "Celestial object not recognized - #{k} not found in #{self.hash.keys.join(', ')}"
}
end


def self.hash
{
"Sun" => 10, "Moon" => 11, "Mercury" => 1,
"Venus" => 2,"Jupiter" => 5, "Mars" => 4,
"Saturn" => 6, "Uranus" => 7, "Neptune" => 8,
"Pluto" => 9, "Achernar" => -1, "Adhara" => -2,
"Aldebaran" => -3, "Altair" => -4, "Antares" => -5,
"Arcturus" => -6, "Betelgeuse" => -7, "Canopus" => -8,
"Capella" => -9, "Deneb" => -10, "Fomalhaut" => -11,
"Hadar" => -12, "Mimosa" => -13, "Polaris" => -14,
"Pollux" => -15, "Procyon" => -16, "Regulus" => -17,
"Rigel" => -18, "RigilKentaurus" => -19, "Vega" => -22,
"Sirius" => -20, "Spica" => -21,
}
end
end
end
end
4 changes: 2 additions & 2 deletions lib/usno/transit/us_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(options = {})
uri: URI.parse("http://aa.usno.navy.mil/cgi-bin/aa_mrst2.pl")
}.merge(options)

load_options(:city, :state, :days, :date, :obj, options)
load_options(:city, :state, :days, :date, :object, options)
end

def call
Expand Down Expand Up @@ -45,7 +45,7 @@ def request_body
ID=AA
#{start_date}
rep=#{@days}
obj=#{@obj}
obj=#{@object}
#{place}
ZZZ=END
}.join("&")
Expand Down
9 changes: 9 additions & 0 deletions lib/usno/transit/worldwide_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module USNO
module Transit
class WorldwideRequest
def initialize(options = {})
raise "Not yet implemented"
end
end
end
end
2 changes: 1 addition & 1 deletion test/unit/usno/transit/us_request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
count: 3,
date: Time.now,
z_meters: 0,
obj: "4"
object: "4"
}
end

Expand Down
15 changes: 15 additions & 0 deletions test/unit/usno/transit/view_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
require "test_helper"
include Minitest::Assertions

describe USNO::Transit::View do
it "cannot be instantiated directly" do
-> { USNO::Transit::View.new }.must_raise RuntimeError
end

describe "can be instantiated by a valid subclass" do
before do
@valid_subclasses = USNO::Transit::Bodies.hash.keys
end

it "instantiates for each valid subclass" do
@valid_subclasses.each do |k|
@subject = instance_eval("USNO::Transit::#{k}")
assert_instance_of(@subject, @subject.new)
end
end
end

end

0 comments on commit 8f8d230

Please sign in to comment.