Class: Transilien::Time

Inherits:
FakeMicroService show all
Defined in:
lib/transilien/time.rb

Overview

Represents Time in Transilien MS API Theorically, you NEVER had to instanciate them by yourself Note: total_seconds and day looks like mysteries for myself right now…

Constant Summary

Constants inherited from MicroService

MicroService::API_HOST, MicroService::API_URI, MicroService::Default_cache_duration

Instance Attribute Summary collapse

Attributes inherited from MicroService

#access_time, #external_code, #payload

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FakeMicroService

http

Methods inherited from MicroService

action, action_component, action_instance_xpath, action_param, add_filters, cache_it, cache_keys, errors, filters, filters=, find, find_from_full_query_cache, find_from_query_caches, http, params, #to_s

Instance Attribute Details

#dayObject

Returns the value of attribute day.



5
6
7
# File 'lib/transilien/time.rb', line 5

def day
  @day
end

#hourObject

Returns the value of attribute hour.



5
6
7
# File 'lib/transilien/time.rb', line 5

def hour
  @hour
end

#minuteObject

Returns the value of attribute minute.



5
6
7
# File 'lib/transilien/time.rb', line 5

def minute
  @minute
end

#total_secondsObject

Returns the value of attribute total_seconds.



5
6
7
# File 'lib/transilien/time.rb', line 5

def total_seconds
  @total_seconds
end

Class Method Details

.from_node(node, access_time) ⇒ Object

Build a ‘Transilien::Time` from a Nokogiri::Node



9
10
11
12
13
14
15
16
17
18
# File 'lib/transilien/time.rb', line 9

def from_node(node, access_time)
  item = new
  item.payload = node

  item.total_seconds = node.at('TotalSeconds').text
  item.day = node.at('Day').text.to_i
  item.hour = node.at('Hour').text.to_i
  item.minute = node.at('Minute').text.to_i
  item
end

Instance Method Details

#nameString

String conversion of the instance. It aims only to permit caching (see Transilien::Microservice class)

Returns:

  • (String)

    representation of that instance



24
25
26
# File 'lib/transilien/time.rb', line 24

def name
  "#{day}:#{hour}:#{minute}|#{total_seconds}"
end

#timeObject

Convert this object to ruby ‘::Time` Since the current time is not known, set it to `Time.new` year, month and day



30
31
32
33
34
35
36
# File 'lib/transilien/time.rb', line 30

def time
  now = Time.new
  this_month_days_count = Date.new(now.year, now.month, -1).day
  return  Time.local(now.year + 1, 1, 1, hour, minute) if day == 1 && now.month == 12 && now.day == this_month_days_count
  return  Time.local(now.year, now.month + 1, 1, hour, minute) if day == 1 && this_month_days_count == now.day
  Time.local(now.year, now.month, now.day + day, hour, minute)
end