Class: Tube::Station

Inherits:
Object
  • Object
show all
Defined in:
lib/station.rb

Overview

Models the data gathered on a tube station from the tfl.gov.uk “Live travel news” page.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, message = nil) ⇒ Station

:call-seq: Station.new(name, message=nil)

Create a new Station.



14
15
16
17
# File 'lib/station.rb', line 14

def initialize( name, message=nil )
  @name = name
  @message = message
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



8
9
10
# File 'lib/station.rb', line 8

def message
  @message
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/station.rb', line 7

def name
  @name
end

Instance Method Details

#to_hashObject

:call-seq: station.to_hash -> hash

Returns a hash representation of the object.

{"name" => "Bank", "message" => "Undergoing escalator refurbishment."}


24
25
26
27
28
# File 'lib/station.rb', line 24

def to_hash
  instance_variables.inject( {} ) do |memo, var|
    memo.merge( {var[1..-1] => instance_variable_get( var )} )
  end
end

#to_json(*args) ⇒ Object

:call-seq: station.to_json -> string

Returns a string of JSON representing the object.

'{"name":"Bank", "message":"Undergoing escalator refurbishment."}'


35
36
37
# File 'lib/station.rb', line 35

def to_json( *args )
  to_hash.to_json( *args )
end

#to_xml(as_string = true) ⇒ Object

:call-seq: station.to_xml -> string station.to_xml(false) -> rexml_document.

Returns a string of XML representing the object.

<station>
  <name>Bank</name>
  <message>Undergoing escalator refurbishment.</message>
</station>

Alternately pass false as the only argument to get an instance of REXML::Document.



51
52
53
54
55
56
57
58
# File 'lib/station.rb', line 51

def to_xml( as_string=true )
  doc = REXML::Document.new
  root = doc.add_element( "station" )
  instance_variables.each do |var|
    root.add_element( var[1..-1] ).add_text( instance_variable_get( var ) )
  end
  if as_string then doc.to_s else doc end
end