Class: Enrico::Country
- Inherits:
-
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_code ⇒ Object
Returns the value of attribute country_code.
6
7
8
|
# File 'lib/enrico/country.rb', line 6
def country_code
@country_code
end
|
#region ⇒ Object
Returns the value of attribute region.
6
7
8
|
# File 'lib/enrico/country.rb', line 6
def region
@region
end
|
Class Method Details
.all ⇒ Object
17
18
19
|
# File 'lib/enrico/country.rb', line 17
def self.all
self.get_countries
end
|
.get_countries ⇒ Object
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, holiday_type: nil) ⇒ Object
62
63
64
65
66
67
68
69
70
|
# File 'lib/enrico/country.rb', line 62
def country_parameters(params, holiday_type: nil)
{
country: self.country_code,
region: self.region
}
.merge(params)
.tap { |result| result[:holidayType] = holiday_type if holiday_type.present? }
.to_query
end
|
#details ⇒ Object
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_holidays_for_date_range(from_date, to_date, holiday_type: nil) ⇒ Object
87
88
89
90
|
# File 'lib/enrico/country.rb', line 87
def get_holidays_for_date_range(from_date, to_date, holiday_type: nil)
params = country_parameters({fromDate: from_date.strftime("%d-%m-%Y"), toDate: to_date.strftime("%d-%m-%Y")}, holiday_type: holiday_type)
self.class.get("/?action=getHolidaysForDateRange&#{params}")
end
|
#get_holidays_for_month(date, holiday_type: nil) ⇒ Object
77
78
79
80
|
# File 'lib/enrico/country.rb', line 77
def get_holidays_for_month(date, holiday_type: nil)
params = country_parameters({month: date.month, year: date.year}, holiday_type: holiday_type)
self.class.get("/?action=getHolidaysForMonth&#{params}")
end
|
#get_holidays_for_year(date, holiday_type: nil) ⇒ Object
82
83
84
85
|
# File 'lib/enrico/country.rb', line 82
def get_holidays_for_year(date, holiday_type: nil)
params = country_parameters({year: date.year}, holiday_type: holiday_type)
self.class.get("/?action=getHolidaysForYear&#{params}")
end
|
#holidays_for_date_range(from_date, to_date) ⇒ Object
35
36
37
38
|
# File 'lib/enrico/country.rb', line 35
def holidays_for_date_range(from_date, to_date)
response = self.get_holidays_for_date_range(from_date, to_date)
self.vacation_days_from_response(response)
end
|
#holidays_for_month(date) ⇒ Object
25
26
27
28
|
# File 'lib/enrico/country.rb', line 25
def holidays_for_month(date)
response = self.get_holidays_for_month(date)
self.vacation_days_from_response(response)
end
|
#holidays_for_year(date) ⇒ Object
30
31
32
33
|
# File 'lib/enrico/country.rb', line 30
def holidays_for_year(date)
response = self.get_holidays_for_year(date)
self.vacation_days_from_response(response)
end
|
#is_public_holiday(date) ⇒ Object
72
73
74
75
|
# File 'lib/enrico/country.rb', line 72
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
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
|
#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
|