Module: OGTraf

Defined in:
lib/ogtraf.rb,
lib/ogtraf/line.rb,
lib/ogtraf/stop.rb,
lib/ogtraf/journey.rb,
lib/ogtraf/version.rb,
lib/ogtraf/departure.rb,
lib/ogtraf/deviation.rb,
lib/ogtraf/connection.rb

Defined Under Namespace

Modules: Priority Classes: Connection, Departure, Deviation, Journey, Line, Stop

Constant Summary collapse

VERSION =
'0.1.0'.freeze

Class Method Summary collapse

Class Method Details

.debug!Object



131
132
133
# File 'lib/ogtraf.rb', line 131

def self.debug!
  logger.level = :debug
end

.departures(departure_start, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ogtraf.rb', line 34

def self.departures(departure_start, options = {})
  query = {
    date: nil,
    delay: 0,
    maxNumberOfResultPerColumn: 8,
    columnsPerPageCount: 1,
    pagesCount: 1,
    lines: nil,
    trafficTypes: nil,
    stopPoints: nil
  }.merge(options)

  departure_start = stops(departure_start).first unless departure_start.is_a? Stop

  raise 'Date must be a Time' unless query[:date].is_a? Time

  query.merge!(
    date: query[:date].strftime('%Y-%m-%d+%H:%M'),
    stopAreaId: departure_start.id
  )

  uri = URI('https://rest.ostgotatrafiken.se/stopdepartures/departures')
  uri.query = URI.encode_www_form(query)

  j = run_query(uri)
  j[:groups].first.map { |v| Departure.new v[:Line] }
end

.journey(journey_start, journey_end, options = {}) ⇒ Object



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
92
93
94
95
96
97
# File 'lib/ogtraf.rb', line 62

def self.journey(journey_start, journey_end, options = {})
  query = {
    date: Time.now,
    direction: 0,
    span: :default,
    traffictypes: 0,
    changetime: 0,
    priority: Priority::SHORTEST_TIME,
    walk: false
  }.merge(options)

  journey_start = stops(journey_start).first unless journey_start.is_a? Stop
  journey_end = stops(journey_end).first unless journey_end.is_a? Stop

  raise 'Date must be a Time' unless query[:date].is_a? Time

  query.merge!(
    time: query[:date].strftime('%H:%M'),
    date: query[:date].strftime('%Y-%m-%d'),

    startId: journey_start.id,
    startType: journey_start.type,
    startLl: journey_start.gps_ll,
    startName: journey_start.name,
    endId: journey_end.id,
    endType: journey_end.type,
    endLl: journey_end.gps_ll,
    endName: journey_end.name
  )

  uri = URI('https://rest.ostgotatrafiken.se/journey/Find')
  uri.query = URI.encode_www_form(query)

  j = run_query(uri, error: true)
  j[:Journeys].map { |v| Journey.new v }
end

.loggerObject



135
136
137
138
139
140
# File 'lib/ogtraf.rb', line 135

def self.logger
  @logger ||= Logging.logger[self].tap do |logger|
    logger.add_appenders Logging.appenders.stdout
    logger.level = :info
  end
end

.run_query(uri, _options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ogtraf.rb', line 99

def self.run_query(uri, _options = {})
  logger.debug uri

  h = Net::HTTP.new(uri.host, uri.port)
  h.use_ssl = uri.scheme == 'https'

  r = h.start do |http|
    http.request(Net::HTTP::Get.new(uri))
  end

  logger.debug r

  begin
    j = JSON.parse(r.body, symbolize_names: true)

    logger.debug j

    raise j unless r.is_a? Net::HTTPSuccess

    if j.is_a? Array
      first = j.first
      if first.key? :ErrorCode
        raise first[:ErrorText] unless first[:ErrorCode].zero?
      end
    end

    j
  rescue JSON::ParserError
    raise "HTTP #{r.code} Response with unparseable JSON."
  end
end

.stops(name, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ogtraf.rb', line 20

def self.stops(name, options = {})
  query = {
    pointType: nil
  }.merge(options)

  query[:q] = CGI.escape name.to_s

  uri = URI('https://rest.ostgotatrafiken.se/stops/Find')
  uri.query = URI.encode_www_form(query)

  j = run_query(uri)
  j.map { |v| Stop.new v }
end