Skip to content

Commit

Permalink
[#2] Adding reservation parsing proxy method
Browse files Browse the repository at this point in the history
This is what I don't like. But for now, let's make it work and fix
tech debt in the future.
  • Loading branch information
nhocki committed Jan 16, 2013
1 parent c8596ad commit 0bd2ac9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions deustorb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Gem::Specification.new do |gem|

gem.add_dependency "rest-client", "~> 1.6.7"
gem.add_dependency "json", "~> 1.7.5"
gem.add_dependency "activesupport"

gem.add_development_dependency 'rack', '~> 1.4.1'
gem.add_development_dependency 'rake', '~> 10.0.2'
Expand Down
46 changes: 46 additions & 0 deletions lib/deustorb/reservation.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'active_support'
require 'active_support/core_ext/string'

require 'deustorb/reservations/waiting'
require 'deustorb/reservations/confirmed'
require 'deustorb/reservations/post_reservation'
Expand All @@ -11,5 +14,48 @@ module Reservation
POST_RESERVATION = 'post_reservation'
WAITING_INSTANCES = 'waiting_instances'
WAITING_CONFIRMATION = 'waiting_confirmation'

TOKEN = 'Reservation::'
def self.new_from_hash(data = {})
reservation_type = data.delete('status'){ raise ::KeyError.new('key not found: "status"') }
reservation_type.sub!(TOKEN, "")
data = normalize_data_hash(data)
unless valid_types.include?(reservation_type)
raise InvalidReservation.new("Invalid reservation type: '#{reservation_type}'\n\nValid Types: #{valid_types.join(", ")}")
end
if reservation_type == WAITING_INSTANCES
WaitingInstances.new data
else
"Deustorb::Reservation::#{reservation_type.classify}".constantize.new(data)
end
end

def self.valid_types
[
WAITING,
CONFIRMED,
POST_RESERVATION,
WAITING_INSTANCES,
WAITING_CONFIRMATION
]
end

private
def self.normalize_data_hash(data)
# force both keys to be present
data['id'] = data.fetch("reservation_id").fetch("id")
data.delete("reservation_id")
simbolize_hash_keys(data)
end

def self.simbolize_hash_keys(hash)
hash.keys.each do |key|
if key.is_a?(String)
hash[key.to_sym] = hash[key]
hash.delete(key)
end
end
hash
end
end
end
27 changes: 27 additions & 0 deletions spec/deustorb/reservation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,32 @@

module Deustorb
describe Reservation do
describe ".new_from_hash" do
let(:params) do
{
url: 'example.com',
time: 'some-time-somewhen',
finished: true,
position: 3,
end_data: 'end data',
initial_data: 'initial data',
initial_configuration: 'initial configuration',
remote_reservation_id: 'remote-reservation-id',
"reservation_id" => {"id" => 'some-reservation-id'}
}
end

Reservation.valid_types.each do |type|
it "instantiates a #{type} reservation" do
params['status'] = type
expect(Reservation.new_from_hash(params.clone).status).to eql(type)
end
end

it "raises an exception for an invalid type" do
params['status'] = "An invalid type"
expect{Reservation.new_from_hash(params.clone)}.to raise_error(Deustorb::InvalidReservation)
end
end
end
end

0 comments on commit 0bd2ac9

Please sign in to comment.