Class: Tube::Station
- Inherits:
-
Object
- Object
- Tube::Station
- 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
-
#message ⇒ Object
Returns the value of attribute message.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(name, message = nil) ⇒ Station
constructor
:call-seq: Station.new(name, message=nil).
-
#to_hash ⇒ Object
:call-seq: station.to_hash -> hash.
-
#to_json(*args) ⇒ Object
:call-seq: station.to_json -> string.
-
#to_xml(as_string = true) ⇒ Object
:call-seq: station.to_xml -> string station.to_xml(false) -> rexml_document.
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, =nil ) @name = name @message = end |
Instance Attribute Details
#message ⇒ Object
Returns the value of attribute message.
8 9 10 |
# File 'lib/station.rb', line 8 def @message end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/station.rb', line 7 def name @name end |
Instance Method Details
#to_hash ⇒ Object
: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 |