Class: Tube::Line
- Inherits:
-
Object
- Object
- Tube::Line
- Defined in:
- lib/line.rb
Overview
Models the data gathered on a tube line from the tfl.gov.uk “Live travel news” page.
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#message ⇒ Object
Returns the value of attribute message.
-
#name ⇒ Object
Returns the value of attribute name.
-
#problem ⇒ Object
(also: #problem?)
Returns the value of attribute problem.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
-
#initialize(id, status, problem = false, message = nil, name = nil) ⇒ Line
constructor
:call-seq: Line.new(id, status, problem=false, message=nil, name=nil).
-
#to_hash ⇒ Object
:call-seq: line.to_hash -> hash.
-
#to_json(*args) ⇒ Object
:call-seq: line.to_json -> string.
-
#to_xml(as_string = true) ⇒ Object
:call-seq: line.to_xml -> string line.to_xml(false) -> rexml_document.
Constructor Details
#initialize(id, status, problem = false, message = nil, name = nil) ⇒ Line
:call-seq: Line.new(id, status, problem=false, message=nil, name=nil)
Create a new Line. If name is ommited it will be set to id.
15 16 17 18 19 20 21 |
# File 'lib/line.rb', line 15 def initialize( id, status, problem=false, =nil, name=nil ) @id = id @status = status @problem = problem @message = @name = name || @id end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/line.rb', line 7 def id @id end |
#message ⇒ Object
Returns the value of attribute message.
8 9 10 |
# File 'lib/line.rb', line 8 def @message end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/line.rb', line 8 def name @name end |
#problem ⇒ Object Also known as: problem?
Returns the value of attribute problem.
8 9 10 |
# File 'lib/line.rb', line 8 def problem @problem end |
#status ⇒ Object
Returns the value of attribute status.
8 9 10 |
# File 'lib/line.rb', line 8 def status @status end |
Instance Method Details
#to_hash ⇒ Object
:call-seq: line.to_hash -> hash
Returns a hash representation of the object.
{"id" => "central", "status" => "Good service", "problem" => false,
"message" => nil, "name" => "Central"}
30 31 32 33 34 |
# File 'lib/line.rb', line 30 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: line.to_json -> string
Returns a string of JSON representing the object.
'{"id":"central", "status":"Good service", "problem":false,
"message":null, "name":"Central"}'
42 43 44 |
# File 'lib/line.rb', line 42 def to_json( *args ) to_hash.to_json( *args ) end |
#to_xml(as_string = true) ⇒ Object
:call-seq: line.to_xml -> string line.to_xml(false) -> rexml_document.
Returns a string of XML representing the object.
<line>
<id>central</id>
<status>Good service</status>
<problem>false</problem>
<message/>
<name>Central</name>
</line>
Alternately pass false as the only argument to get an instance of REXML::Document.
61 62 63 64 65 66 67 68 69 |
# File 'lib/line.rb', line 61 def to_xml( as_string=true ) doc = REXML::Document.new root = doc.add_element( "line" ) instance_variables.each do |var| el = root.add_element( var[1..-1] ) el.add_text( instance_variable_get( var ).to_s ) end if as_string then doc.to_s else doc end end |