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
Currently we use a separate method for times, since they come in a format which probably is too general for regexp.
-
.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{4}-\d\d-\d\d/ then Date.parse(data) when /^(\d+\s){2,}[0-9 ]*$/ then data.split.collect { |i| i.to_i } when /^\s*$/ then nil when "Y" then true when "N" then false else data.strip end end |
.price(string) ⇒ Object
47 48 49 |
# File 'lib/hostconnect/coercion.rb', line 47 def self.price(string) string.to_i / 100 end |
.time(string) ⇒ Object
Currently we use a separate method for times, since they come in a format which probably is too general for regexp. I.e “2310”
43 44 45 |
# File 'lib/hostconnect/coercion.rb', line 43 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 |