Class: Enrico::Country

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/enrico/country.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(country_code, region = nil) ⇒ Country

Returns a new instance of Country.



12
13
14
15
# File 'lib/enrico/country.rb', line 12

def initialize(country_code, region = nil)
  self.country_code = country_code
  self.region = region
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



45
46
47
48
# File 'lib/enrico/country.rb', line 45

def method_missing(name, *args, &block)
  method_name = name.to_s.camelize(:lower)
  details.has_key?(method_name) ? details[method_name] : super
end

Instance Attribute Details

#country_codeObject

Returns the value of attribute country_code.



6
7
8
# File 'lib/enrico/country.rb', line 6

def country_code
  @country_code
end

#regionObject

Returns the value of attribute region.



6
7
8
# File 'lib/enrico/country.rb', line 6

def region
  @region
end

Class Method Details

.allObject



17
18
19
# File 'lib/enrico/country.rb', line 17

def self.all
  self.get_countries
end

.get_countriesObject



50
51
52
# File 'lib/enrico/country.rb', line 50

def self.get_countries
  self.get("/?action=getSupportedCountries")
end

Instance Method Details

#country_parameters(params) ⇒ Object



62
63
64
65
66
# File 'lib/enrico/country.rb', line 62

def country_parameters(params)
  base = { country: self.country_code, region: self.region }
  base.merge!(params)
  base.to_query
end

#detailsObject



21
22
23
# File 'lib/enrico/country.rb', line 21

def details
  self.class.all.select{|country| country["countryCode"] == self.country_code }.first
end

#get_public_holidays_for_date_range(from_date, to_date) ⇒ Object



83
84
85
86
# File 'lib/enrico/country.rb', line 83

def get_public_holidays_for_date_range(from_date, to_date)
  params = country_parameters({fromDate: from_date.strftime("%d-%m-%Y"), toDate: to_date.strftime("%d-%m-%Y")})
  self.class.get("/?action=getPublicHolidaysForDateRange&#{params}")
end

#get_public_holidays_for_month(date) ⇒ Object



73
74
75
76
# File 'lib/enrico/country.rb', line 73

def get_public_holidays_for_month(date)
  params = country_parameters({month: date.month, year: date.year})
  self.class.get("/?action=getPublicHolidaysForMonth&#{params}")
end

#get_public_holidays_for_year(date) ⇒ Object



78
79
80
81
# File 'lib/enrico/country.rb', line 78

def get_public_holidays_for_year(date)
  params = country_parameters({year: date.year})
  self.class.get("/?action=getPublicHolidaysForYear&#{params}")
end

#is_public_holiday(date) ⇒ Object



68
69
70
71
# File 'lib/enrico/country.rb', line 68

def is_public_holiday(date)
  params = country_parameters({date: date.strftime("%d-%m-%Y")})
  self.class.get("/?action=isPublicHoliday&#{params}")
end

#is_public_holiday?(date) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/enrico/country.rb', line 40

def is_public_holiday?(date)
  response = self.is_public_holiday(date)
  response["isPublicHoliday"]
end

#public_holidays_for_date_range(from_date, to_date) ⇒ Object



35
36
37
38
# File 'lib/enrico/country.rb', line 35

def public_holidays_for_date_range(from_date, to_date)
  response = self.get_public_holidays_for_date_range(from_date, to_date)
  self.vacation_days_from_response(response)
end

#public_holidays_for_month(date) ⇒ Object



25
26
27
28
# File 'lib/enrico/country.rb', line 25

def public_holidays_for_month(date)
  response = self.get_public_holidays_for_month(date)
  self.vacation_days_from_response(response)
end

#public_holidays_for_year(date) ⇒ Object



30
31
32
33
# File 'lib/enrico/country.rb', line 30

def public_holidays_for_year(date)
  response = self.get_public_holidays_for_year(date)
  self.vacation_days_from_response(response)
end

#vacation_days_from_response(response) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/enrico/country.rb', line 54

def vacation_days_from_response(response)
  vacation_days = []
  response.each do |vacation_day|
    vacation_days.push( Enrico::VacationDay.new(vacation_day) )
  end
  vacation_days
end