Module: RandomDataDespegar::Dates

Included in:
Random
Defined in:
lib/random_data_despegar/dates.rb

Overview

Defines methods for random date generation

Instance Method Summary collapse

Instance Method Details

#date(dayrange = 45) ⇒ Object

Returns a random date. If dayrange is an Integer, then the date returned will be at least what you specify from today. If dayrange is a range, you’ll get a date between dayrange.min from today and dayrange.max from today. dayrange will be an integer or a range



18
19
20
21
22
23
24
25
# File 'lib/random_data_despegar/dates.rb', line 18

def date(dayrange=45)
  if dayrange.is_a?(Range)
    offset = rand(dayrange.max-dayrange.min) + dayrange.min
  else
    offset = rand(dayrange*2) + dayrange
  end
  Date.today + offset
end

#date_between(range) ⇒ Object

Returns a date within the specified Range. The Range can be Date or String objects.

Example: min = Date.parse(‘1966-11-15’) max = Date.parse(‘1990-01-01’) Random.date(min..max) # => a Date between 11/15/1996 and 1/1/1990 Random.date(‘1966-11-15’..‘1990-01-01’) # => a Date between 11/15/1996 and 1/1/1990



35
36
37
38
39
40
# File 'lib/random_data_despegar/dates.rb', line 35

def date_between(range)
  min_date = range.min.is_a?(Date) ? range.min : Date.parse(range.min)
  max_date = range.max.is_a?(Date) ? range.max : Date.parse(range.max)
  diff = (max_date - min_date).to_i
  min_date + rand(diff)
end

#departure_and_return_dates(departure_date_range = 60..90) ⇒ Object

Returns valid departure and return dates (Date and formated) Dates will be about 2/3 month from now, and stay will be between 5 and 20 days. departure_date_range will be a range



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/random_data_despegar/dates.rb', line 47

def departure_and_return_dates (departure_date_range=60..90)
  stay = rand(5..20)
  departure_date = Date.today + rand(departure_date_range)
  return_date = departure_date + stay
  {
  :departure_date => departure_date,
  :return_date => return_date,
  :formated_departure_date => departure_date.strftime("%F"),
  :formated_return_date => return_date.strftime("%F")
  }
end

#passenger_birthdate(passenger_type) ⇒ Object

Returns a hash with birhdate for a specific passenger type: adult, child or infant. Each passenger has: an age, month of birth and day of birth.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/random_data_despegar/dates.rb', line 62

def passenger_birthdate (passenger_type)
  present_year = Date.today.year
  month = rand(1..12)
  case
    when passenger_type.eql?("adult") then
      adt_age = rand(19..60)
      month_adt_days = Date.new(present_year-adt_age,month,-1).day
      adult = {
        :age => adt_age,
        :month => month,
        :day => rand(1..month_adt_days)
               }
    when passenger_type.eql?("child") then
      chd_age = rand(3..5)
      month_chd_days = Date.new(present_year-chd_age,month,-1).day
      child = {
        :age => chd_age,
        :month => month,
        :day => rand(1..month_chd_days)
               }
    when passenger_type.eql?("infant") then
      inf_age = rand(0..2)
      month_inf_days = Date.new(present_year-inf_age,month,-1).day
      infant = {
        :age => inf_age,
        :month => month,
        :day => rand(1..month_inf_days)
                }
  end
end

#passenger_birthdate_parsed(passenger_type) ⇒ Object

Returns 2016-02-26T09:16:13Z



94
95
96
97
98
99
# File 'lib/random_data_despegar/dates.rb', line 94

def passenger_birthdate_parsed(passenger_type)
  pax_data = passenger_birthdate(passenger_type)
  now = Time.now.utc.to_date
  pax_data[:b_year] = now.year - pax_data[:age]
  DateTime.new(pax_data[:b_year], pax_data[:month], pax_data[:day]).strftime('%FT%TZ')
end