libtad 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/lib/libtad.rb +123 -0
  3. data/lib/services/astronomy.rb +102 -0
  4. data/lib/services/date_calculator.rb +155 -0
  5. data/lib/services/holidays.rb +34 -0
  6. data/lib/services/onthisday.rb +42 -0
  7. data/lib/services/places.rb +26 -0
  8. data/lib/services/time.rb +143 -0
  9. data/lib/types/astronomy/astronomy_current.rb +58 -0
  10. data/lib/types/astronomy/astronomy_day.rb +32 -0
  11. data/lib/types/astronomy/astronomy_day_event.rb +76 -0
  12. data/lib/types/astronomy/astronomy_event.rb +24 -0
  13. data/lib/types/astronomy/astronomy_event_class.rb +37 -0
  14. data/lib/types/astronomy/astronomy_location.rb +31 -0
  15. data/lib/types/astronomy/astronomy_object.rb +27 -0
  16. data/lib/types/astronomy/astronomy_object_details.rb +32 -0
  17. data/lib/types/astronomy/astronomy_object_type.rb +36 -0
  18. data/lib/types/astronomy/moonphase.rb +37 -0
  19. data/lib/types/date_calculator/business_days_filter.rb +39 -0
  20. data/lib/types/date_calculator/business_holiday.rb +26 -0
  21. data/lib/types/date_calculator/period.rb +44 -0
  22. data/lib/types/date_calculator/weekdays.rb +54 -0
  23. data/lib/types/holidays/holiday.rb +93 -0
  24. data/lib/types/holidays/holiday_state.rb +35 -0
  25. data/lib/types/holidays/holiday_type.rb +107 -0
  26. data/lib/types/onthisday/event.rb +51 -0
  27. data/lib/types/onthisday/event_type.rb +15 -0
  28. data/lib/types/onthisday/name.rb +24 -0
  29. data/lib/types/onthisday/person.rb +39 -0
  30. data/lib/types/places/country.rb +20 -0
  31. data/lib/types/places/geo.rb +34 -0
  32. data/lib/types/places/location.rb +43 -0
  33. data/lib/types/places/location_ref.rb +24 -0
  34. data/lib/types/places/place.rb +24 -0
  35. data/lib/types/places/region.rb +35 -0
  36. data/lib/types/time/datetime.rb +93 -0
  37. data/lib/types/time/dst_entry.rb +48 -0
  38. data/lib/types/time/time.rb +30 -0
  39. data/lib/types/time/time_change.rb +49 -0
  40. data/lib/types/time/timezone.rb +51 -0
  41. metadata +137 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d0199d049a73b8fd060defe6dc08ab230bc3718e4b2ba9f38ade44babcfdf3d8
4
+ data.tar.gz: 714efc2a4105145b43b440bdb0eea3d21fbf341a269918a29d725df4eaa4a1ee
5
+ SHA512:
6
+ metadata.gz: 7c95e56ed533e92252e67250f637d48fb7fd938933a71476c0653722e7a3962c125c8ca0b756c0da3de8aa254fafbbf819de7fa18547e48804418f3b7374e6e8
7
+ data.tar.gz: ab7994a5b52a3fab73f0d00748fbae874b3e8c7d51b0b67bb4a2b6a0b2949700e6672792f71f46fdf525b171a3ca1c46d14040d28a197897d35730a55af494df
data/lib/libtad.rb ADDED
@@ -0,0 +1,123 @@
1
+ require 'services/astronomy'
2
+ require 'services/date_calculator'
3
+ require 'services/holidays'
4
+ require 'services/onthisday'
5
+ require 'services/places'
6
+ require 'services/time'
7
+
8
+ require 'types/astronomy/astronomy_current'
9
+ require 'types/astronomy/astronomy_day'
10
+ require 'types/astronomy/astronomy_day_event'
11
+ require 'types/astronomy/astronomy_event'
12
+ require 'types/astronomy/astronomy_event_class'
13
+ require 'types/astronomy/astronomy_location'
14
+ require 'types/astronomy/astronomy_object'
15
+ require 'types/astronomy/astronomy_object_details'
16
+ require 'types/astronomy/astronomy_object_type'
17
+ require 'types/astronomy/moonphase'
18
+
19
+ require 'types/date_calculator/business_days_filter'
20
+ require 'types/date_calculator/business_holiday'
21
+ require 'types/date_calculator/period'
22
+ require 'types/date_calculator/weekdays'
23
+
24
+ require 'types/holidays/holiday'
25
+ require 'types/holidays/holiday_state'
26
+ require 'types/holidays/holiday_type'
27
+
28
+ require 'types/onthisday/event'
29
+ require 'types/onthisday/event_type'
30
+ require 'types/onthisday/name'
31
+ require 'types/onthisday/person'
32
+
33
+ require 'types/places/country'
34
+ require 'types/places/geo'
35
+ require 'types/places/location'
36
+ require 'types/places/location_ref'
37
+ require 'types/places/place'
38
+ require 'types/places/region'
39
+
40
+ require 'types/time/datetime'
41
+ require 'types/time/dst_entry'
42
+ require 'types/time/time'
43
+ require 'types/time/time_change'
44
+ require 'types/time/timezone'
45
+
46
+ require 'base64'
47
+ require 'json'
48
+ require 'net/http'
49
+ require 'openssl'
50
+
51
+ module LibTAD
52
+ # Main endpoint for accessing the Time and Date APIs.
53
+ class Client
54
+
55
+ include LibTAD::Client::AstronomyService
56
+ include LibTAD::Client::DateCalculatorService
57
+ include LibTAD::Client::HolidaysService
58
+ include LibTAD::Client::OnThisDayService
59
+ include LibTAD::Client::PlacesService
60
+ include LibTAD::Client::TimeService
61
+
62
+ # The endpoint the client connects to.
63
+ ENDPOINT = 'https://api.xmltime.com/'.freeze
64
+
65
+ # Client user agent.
66
+ USER_AGENT = 'libtad-ruby-0.1.0'.freeze
67
+
68
+ # API version.
69
+ VERSION = 3.freeze
70
+
71
+ def initialize(access_key:, secret_key:)
72
+ @access_key = access_key
73
+ @secret_key = secret_key
74
+ end
75
+
76
+ private
77
+
78
+ def authenticate(service, args)
79
+ timestamp = ::Time.now.utc.strftime('%FT%T')
80
+ message = @access_key + service + timestamp
81
+
82
+ hmac = OpenSSL::HMAC.digest('sha1', @secret_key, message)
83
+ signature = Base64.encode64(hmac).chomp
84
+
85
+ args[:accesskey] = @access_key
86
+ args[:signature] = signature
87
+ args[:timestamp] = timestamp
88
+
89
+ args
90
+ end
91
+
92
+ def get(service, args)
93
+ args = args.transform_values! do |e|
94
+ if [true, false].include?(e)
95
+ e && 1 || 0
96
+ else
97
+ e
98
+ end
99
+ end
100
+
101
+ args[:version] = VERSION
102
+ args = authenticate(service, args)
103
+
104
+ uri = URI(ENDPOINT + service)
105
+ uri.query = URI.encode_www_form(args)
106
+
107
+ request = Net::HTTP::Get.new(uri)
108
+ request["User-Agent"] = USER_AGENT
109
+
110
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => (uri.scheme == 'https')) {|http|
111
+ http.request(request)
112
+ }
113
+
114
+ res = JSON.parse(res.body)
115
+
116
+ if (error = res.fetch('errors', nil))
117
+ raise Exception.new error
118
+ else
119
+ res
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,102 @@
1
+ require 'date'
2
+
3
+ module LibTAD
4
+ class Client
5
+ # Astronomy API.
6
+ module AstronomyService
7
+ # @return [Array<::LibTAD::Astronomy::AstronomyLocation>]
8
+ # @param object [Symbol] Specify an object type to retrieve information about.
9
+ # @param place_id [String] Specify the ID or a list of IDs of the location(s) you would like to retrieve information for.
10
+ # @param start_date [String] Specify the ISO 8601 date for the first date you are interested in. Defaults to current date.
11
+ # @param end_date [String] The last date you are interested in. The service can be used to calculate data for a maximum of 31 days in a row.
12
+ # If the end date is omitted, only one day is retrieved.
13
+ # @param types [Symbol or Array<Symbol>] Specify an astronomical event class or a list of classes to filter by.
14
+ # @param geo [Boolean] Return longitude and latitude for the geo object.
15
+ # @param isotime [Boolean] Adds time stamps (local time) in ISO 8601 format to all events.
16
+ # @param lang [String] Preferred language for texts.
17
+ # @param radius [Integer] Search radius for translating coordinates to locations.
18
+ # @param utctime [Boolean] Adds UTC time stamps in ISO 8601 format to all events.
19
+ # @see ::LibTAD::Astronomy::ASTRONOMY_OBJECT_TYPE
20
+ # @see ::LibTAD::Astronomy::ASTRONOMY_EVENT_CLASS
21
+ #
22
+ # The Astro Event service can be used retrieve the sunrise, sunset, moonrise, moonset, solar noon and twilight times for all
23
+ # locations in our database. The service also returns the azimuth of the events, the altitude, and the distance to the sun for the noon event.
24
+ def get_astro_events(
25
+ object:,
26
+ place_id:,
27
+ start_date: nil,
28
+ end_date: nil,
29
+ types: nil,
30
+ geo: nil,
31
+ isotime: nil,
32
+ lang: nil,
33
+ radius: nil,
34
+ utctime: nil
35
+ )
36
+ args = {
37
+ object: (object unless !::LibTAD::Astronomy::ASTRONOMY_OBJECT_TYPE.include?(object)),
38
+ placeid: place_id,
39
+ startdt: if start_date.nil? then ::Time.now.strftime('%Y-%m-%d') else start_date end,
40
+ enddt: end_date,
41
+ types: (types unless ![*types].all? { |e| ::LibTAD::Astronomy::ASTRONOMY_EVENT_CLASS.include?(e) }),
42
+ geo: geo,
43
+ isotime: isotime,
44
+ lang: lang,
45
+ radius: radius,
46
+ utctime: utctime
47
+ }.compact
48
+
49
+ response = get('astronomy', args)
50
+ astroevents = response.fetch('locations', [])
51
+
52
+ astroevents.collect do |e|
53
+ ::LibTAD::Astronomy::AstronomyLocation.new(e)
54
+ end
55
+ end
56
+
57
+
58
+ # @return [Array<::LibTAD::Astronomy::AstronomyLocation>]
59
+ # @param object [Symbol] Specify an object type to retrieve information about.
60
+ # @param place_id [String] Specify the ID of the location you would like to retrieve information for.
61
+ # @param interval [String or Array<String>] Specify the point or a list of points in time you would like to calculate data for.
62
+ # @param localtime [Boolean] Specify whether or not the intervals should be considered the local time for the place(s) or UTC time.
63
+ # @param utctime [Boolean] Adds UTC time stamps in ISO 8601 format to all events.
64
+ # @param isotime [Boolean] Adds time stamps (local time) in ISO 8601 format to all events.
65
+ # @param lang [String] Preferred language for texts.
66
+ # @param radius [Integer] Search radius for translating coordinates to locations.
67
+ # @see ::LibTAD::Astronomy::ASTRONOMY_OBJECT_TYPE
68
+ #
69
+ # The Astro Position service can be used to retrieve the altitude, azimuth and distance to the Moon and the Sun for all locations in our database.
70
+ # The service also returns the moon phase, the fraction of the Moon's illuminated side as well as the midpoint angle of the Moon's bright limb at any point in time.
71
+ # Unlike the Astro Event service, the Astro Position service can be queried on a specific point in time, down to the second.
72
+ def get_astro_position(
73
+ object:,
74
+ place_id:,
75
+ interval:,
76
+ localtime: nil,
77
+ utctime: nil,
78
+ isotime: nil,
79
+ lang: nil,
80
+ radius: nil
81
+ )
82
+ args = {
83
+ object: (object unless !::LibTAD::Astronomy::ASTRONOMY_OBJECT_TYPE.include?(object)),
84
+ placeid: place_id,
85
+ interval: interval,
86
+ localtime: localtime,
87
+ utctime: utctime,
88
+ isotime: isotime,
89
+ lang: lang,
90
+ radius: radius
91
+ }.compact
92
+
93
+ response = get('astrodata', args)
94
+ astropositions = response.fetch('locations', [])
95
+
96
+ astropositions.collect do |e|
97
+ ::LibTAD::Astronomy::AstronomyLocation.new(e)
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,155 @@
1
+ module LibTAD
2
+ class Client
3
+ # Date Calculator API.
4
+ module DateCalculatorService
5
+ # @return [::LibTAD::Places::Geo, Array<::LibTAD::DateCalculator::Period>]
6
+ # @param place_id [String] Specify the ID of the location you would like to calculate the business date.
7
+ # The ID is used to find what holidays are applicable for the given place so the calculation can exclude or include those results.
8
+ # @param country [String] Specify the country for which you would like to calculate the business date.
9
+ # @param state [String] Specify the state in the given country you want to calculate the business date.
10
+ # @param start_date [::LibTAD::TADTime::TADDateTime or String] Specify the start date. Takes a TADDateTime object or a ISO 8601 date string.
11
+ # @param days [Integer] Specify an amount or a list of amounts of business days to count.
12
+ # @param including [Boolean] Specify whether the result should be calculated by including instead of excluding the days.
13
+ # @param filter [Symbol] Specify a type or a list of types to filter by.
14
+ # @param repeat [Integer] Set how many times the calculation should be repeated.
15
+ # @param lang [String] The preferred language for the texts.
16
+ # @see ::LibTAD::DateCalculator::BUSINESS_DAYS_FILTER
17
+ #
18
+ # The Businessdate service can be used to find a business date from a specified number of days.
19
+ # By default the result will be filtered on excluding weekends and public holidays, but you can specify a custom filter to modify this.
20
+ #
21
+ # Either place_id or country is required.
22
+ def add_days(
23
+ place_id: nil,
24
+ country: nil,
25
+ state: nil,
26
+ start_date:,
27
+ days:,
28
+ including: nil,
29
+ filter: nil,
30
+ repeat: nil,
31
+ lang: nil
32
+ )
33
+ args = {
34
+ placeid: place_id,
35
+ country: country,
36
+ state: state,
37
+ startdt: if start_date.methods.include?(:to_iso8601) then start_date.to_iso8601 else start_date end,
38
+ days: days,
39
+ include: including,
40
+ filter: (filter unless ![*filter].all? { |e| ::LibTAD::DateCalculator::BUSINESS_DAYS_FILTER.include?(e) }),
41
+ repeat: repeat,
42
+ lang: lang
43
+ }.compact
44
+
45
+ call_business_date(args, 'add')
46
+ end
47
+
48
+ # @return [::LibTAD::Places::Geo, Array<::LibTAD::DateCalculator::Period>]
49
+ # @param place_id [String] Specify the ID of the location you would like to calculate the business date.
50
+ # The ID is used to find what holidays are applicable for the given place so the calculation can exclude or include those results.
51
+ # @param country [String] Specify the country for which you would like to calculate the business date.
52
+ # @param state [String] Specify the state in the given country you want to calculate the business date.
53
+ # @param start_date [::LibTAD::TADTime::TADDateTime or String] Specify the start date. Takes a TADDateTime object or a ISO 8601 date string.
54
+ # @param days [Integer] Specify an amount or a list of amounts of business days to count.
55
+ # @param including [Boolean] Specify whether the result should be calculated by including instead of excluding the days.
56
+ # @param filter [Symbol] Specify a type or a list of types to filter by.
57
+ # @param repeat [Integer] Set how many times the calculation should be repeated.
58
+ # @param lang [String] The preferred language for the texts.
59
+ # @see ::LibTAD::DateCalculator::BUSINESS_DAYS_FILTER
60
+ #
61
+ # The Businessdate service can be used to find a business date from a specified number of days.
62
+ # By default the result will be filtered on excluding weekends and public holidays, but you can specify a custom filter to modify this.
63
+ #
64
+ # Either place_id or country is required.
65
+ def subtract_days(
66
+ place_id: nil,
67
+ country: nil,
68
+ state: nil,
69
+ start_date:,
70
+ days:,
71
+ including: nil,
72
+ filter: nil,
73
+ repeat: nil,
74
+ lang: nil
75
+ )
76
+ args = {
77
+ placeid: place_id,
78
+ country: country,
79
+ state: state,
80
+ startdt: if start_date.methods.include?(:to_iso8601) then start_date.to_iso8601 else start_date end,
81
+ days: days,
82
+ include: including,
83
+ filter: (filter unless ![*filter].all? { |e| ::LibTAD::DateCalculator::BUSINESS_DAYS_FILTER.include?(e) }),
84
+ repeat: repeat,
85
+ lang: lang
86
+ }.compact
87
+
88
+ call_business_date(args, 'subtract')
89
+ end
90
+
91
+ # @return [::LibTAD::Places::Geo, ::LibTAD::DateCalculator::Period]
92
+ # @param place_id [String] Specify the ID of the location you would like to calculate the business duration.
93
+ # The ID is used to find what holidays are applicable for the given place so the calculation can exclude or include those results.
94
+ # @param country [String] Specify the country for which you would like to calculate the business date.
95
+ # @param state [String] Specify the state in the given country you want to calculate the business date.
96
+ # @param start_date [::LibTAD::TADTime::TADDateTime or String] Specify the start date. Takes a TADDateTime object or a ISO 8601 date string.
97
+ # @param end_date [::LibTAD::TADTime::TADDateTime or String] Specify the end date. Takes a TADDateTime object or a ISO 8601 date string.
98
+ # @param including [Boolean] Specify whether the result should be calculated by including instead of excluding the days.
99
+ # @param filter [Symbol] Specify a type or a list of types to filter by.
100
+ # @param include_last_date [Boolean] Whether or not the last date should be counted in the result.
101
+ # @param lang [String] The preferred language for the texts.
102
+ # @see ::LibTAD::DateCalculator::BUSINESS_DAYS_FILTER
103
+ #
104
+ # The Businessduration service can be used to calculate the number of business days between a specified start date and end date.
105
+ #
106
+ # When you query the Businessduration service with a placeid or a country, a start date and an end date the service will return the number
107
+ # of business days in that date range by excluding public holidays and weekends. Furthermore, you can apply additional filters
108
+ # such as individual days and whether or not the calculation should include the filter result or exclude it.
109
+ #
110
+ # Either place_id or country is required.
111
+ def get_duration(
112
+ place_id: nil,
113
+ country: nil,
114
+ state: nil,
115
+ start_date:,
116
+ end_date:,
117
+ including: nil,
118
+ filter: nil,
119
+ include_last_date: nil,
120
+ lang: nil
121
+ )
122
+ args = {
123
+ placeid: place_id,
124
+ country: country,
125
+ state: state,
126
+ startdt: if start_date.methods.include?(:to_iso8601) then start_date.to_iso8601 else start_date end,
127
+ enddt: if end_date.methods.include?(:to_iso8601) then end_date.to_iso8601 else end_date end,
128
+ include: including,
129
+ filter: (filter unless ![*filter].all? { |e| ::LibTAD::DateCalculator::BUSINESS_DAYS_FILTER.include?(e) }),
130
+ includelastdate: include_last_date,
131
+ lang: lang
132
+ }.compact
133
+
134
+ response = get('businessduration', args)
135
+ geo = ::LibTAD::Places::Geo.new response['geo'] unless !response.key?('geo')
136
+ period = ::LibTAD::DateCalculator::Period.new response['period'] unless !response.key?('period')
137
+
138
+ return geo, period
139
+ end
140
+
141
+ private
142
+
143
+ def call_business_date(args, operation)
144
+ args[:op] = operation
145
+
146
+ response = get('businessdate', args)
147
+ geo = ::LibTAD::Places::Geo.new response['geo'] unless !response.key?('geo')
148
+ period = response.fetch('periods', [])
149
+ .map { |e| ::LibTAD::DateCalculator::Period.new(e) }
150
+
151
+ return geo, period
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,34 @@
1
+ require 'date'
2
+
3
+ module LibTAD
4
+ class Client
5
+ # Holidays API.
6
+ module HolidaysService
7
+ # @return [Array<::LibTAD::Holidays::Holiday>]
8
+ # @param country [String] Specify the ISO3166-1-alpha-2 Country Code for which you would like to retrieve the list of holidays.
9
+ # @param year [Integer] The year for which the holidays should be retrieved. Defaults to current year.
10
+ # @param lang [String or Array<String>] Specify the ISO639 Language Code or a list of codes for the texts.
11
+ # @param types [Symbol or Array<Symbol>] Specify a holiday type or a list of holiday types to filter by.
12
+ # @param timezone [Boolean] Adds time zone information under the time object.
13
+ # @see ::LibTAD::Holidays::HOLIDAY_TYPE
14
+ #
15
+ # The Holidays service can be used to retrieve the list of holidays for a country.
16
+ def get_holidays(country:, year: nil, lang: nil, types: nil, timezone: nil)
17
+ args = {
18
+ country: country,
19
+ lang: lang,
20
+ types: (types unless ![*types].all? { |e| ::LibTAD::Holidays::HOLIDAY_TYPE.include?(e) }),
21
+ year: if year.nil? then ::Time.now.year else year end,
22
+ tz: timezone
23
+ }.compact
24
+
25
+ response = get('holidays', args)
26
+ holidays = response.fetch('holidays', [])
27
+
28
+ holidays.collect do |e|
29
+ ::LibTAD::Holidays::Holiday.new(e)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,42 @@
1
+ module LibTAD
2
+ class Client
3
+ # On This Day API.
4
+ module OnThisDayService
5
+ # @return [Array<::LibTAD::OnThisDay::Event>, Array<::LibTAD::OnThisDay::Person>, Array<::LibTAD::OnThisDay::Person>]
6
+ # @param month [Integer] The month for which the events should be retrieved. Defaults to current month.
7
+ # @param day [Integer] The day for which the events should be retrieved. Defaults to current day.
8
+ # @param lang [String or Array<String>] Specify the ISO639 Language Code or a list of codes for the texts.
9
+ # @param types [Symbol or Array<Symbol>] Specify an event type or a list of event types to filter by.
10
+ # @see ::LibTAD::OnThisDay::EVENT_TYPE
11
+ #
12
+ # The On This Day service can be used to retrieve events, births and deaths for a specific date.
13
+ def get_events_on_this_day(month: nil, day: nil, lang: nil, types: nil)
14
+ args = {
15
+ month: month,
16
+ day: day,
17
+ lang: lang,
18
+ types: (types unless ![*types].all? { |e| ::LibTAD::OnThisDay::EVENT_TYPE.include?(e) }),
19
+ }.compact
20
+
21
+ response = get('onthisday', args)
22
+ events = response.fetch('events', [])
23
+ births = response.fetch('births', [])
24
+ deaths = response.fetch('deaths', [])
25
+
26
+ events = events.collect do |e|
27
+ ::LibTAD::OnThisDay::Event.new(e)
28
+ end
29
+
30
+ births = births.collect do |e|
31
+ ::LibTAD::OnThisDay::Person.new(e)
32
+ end
33
+
34
+ deaths = deaths.collect do |e|
35
+ ::LibTAD::OnThisDay::Person.new(e)
36
+ end
37
+
38
+ return events, births, deaths
39
+ end
40
+ end
41
+ end
42
+ end