Class: HostConnect::Coercion
- Inherits:
-
Object
- Object
- HostConnect::Coercion
- Defined in:
- lib/hostconnect/coercion.rb
Overview
Coercion
Possible Types:
-
String
-
Strings with multiple lines (don’t know yet if these must be handled specifically)
-
Boolean (Y/N)
-
Date (yyyy-mm-dd)
-
Time (hhmm)
-
Integer
-
Integer list (In an integer list, a single space is used to separate integer values.)
-
Price (Prices returned are Tourplan prices multiplied by 100, + currency stuff..)
It must be possible to convert this data from/to native Ruby objects
Class Method Summary collapse
-
.from_hc(data) ⇒ Object
Convert HostConnect formatted data to Ruby objects.
- .price(string) ⇒ Object
-
.time(string) ⇒ Object
Time strings are i.e “2310”.
-
.to_hc(data) ⇒ Object
Convert Ruby objects to HostConnect formatted data.
Class Method Details
.from_hc(data) ⇒ Object
Convert HostConnect formatted data to Ruby objects
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/hostconnect/coercion.rb', line 17 def self.from_hc(data) case data when /^[0-9]+\d*$/ then data.to_i when /^(\d+\s){2,}[0-9 ]*$/ then data.split.collect { |i| i.to_i } when /^\s*$/ then nil when /\d{4}-\d\d-\d\d/ then Date.parse(data) when "Y" then true when "N" then false else data.strip end end |
.price(string) ⇒ Object
46 47 48 |
# File 'lib/hostconnect/coercion.rb', line 46 def self.price(string) (string.to_f / 100).round end |
.time(string) ⇒ Object
Time strings are i.e “2310”
42 43 44 |
# File 'lib/hostconnect/coercion.rb', line 42 def self.time(string) Time.parse(string[0,2] << ":" << string[2,4]) end |
.to_hc(data) ⇒ Object
Convert Ruby objects to HostConnect formatted data
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/hostconnect/coercion.rb', line 30 def self.to_hc(data) case data when Integer then data.to_s when Date then data.to_s when nil then "" when true then "Y" when false then "N" else data.strip end end |