skanetrafiken 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/skanetrafiken.rb +29 -0
- data/lib/skanetrafiken/get_departure_arrival.rb +44 -0
- data/lib/skanetrafiken/get_journey.rb +35 -0
- data/lib/skanetrafiken/line.rb +33 -0
- data/lib/skanetrafiken/point.rb +21 -0
- data/lib/skanetrafiken/query_station.rb +31 -0
- metadata +50 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA1:
         | 
| 3 | 
            +
              metadata.gz: cde5d0547b7b4f3a410b3ced1a5469613efa731a
         | 
| 4 | 
            +
              data.tar.gz: 19277f7976103a6be5ee23d321d0eafb93e85379
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: 4562d34730f458aba3201554a42e7a6a8e46ff0ea4d825d3966653a2bb0b6df8f24cc11ba3d2df4f9ad2b97b664619b3d17ced5c1ca48b65278822e3733b81b8
         | 
| 7 | 
            +
              data.tar.gz: 38037b61cbca6cf23246cb6a45b5c6347feef7b24080959df46d4b6a8d739b76a4d457a739c04e5d772e787ae17570fd4f93797b2e3e3d42ed60d6d7d0f0ad71
         | 
| @@ -0,0 +1,29 @@ | |
| 1 | 
            +
            #!/opt/local/bin/ruby1.9
         | 
| 2 | 
            +
            require "rubygems"
         | 
| 3 | 
            +
            require 'net/http'
         | 
| 4 | 
            +
            require 'rexml/document'
         | 
| 5 | 
            +
            require "crack"
         | 
| 6 | 
            +
            require "json"
         | 
| 7 | 
            +
            Dir[File.join(File.dirname(__FILE__),'skanetrafiken','*.rb')].each {|file| require file }
         | 
| 8 | 
            +
            module Skanetrafiken
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              class XmlToJson
         | 
| 11 | 
            +
                def convert(xml)
         | 
| 12 | 
            +
                  return Crack::XML.parse(xml).to_json
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
                
         | 
| 16 | 
            +
              class UriHelper
         | 
| 17 | 
            +
                def parameters_from_hash(hash)
         | 
| 18 | 
            +
                  return hash.map{ |key,value| 
         | 
| 19 | 
            +
                        "#{key.to_s}=#{URI.escape(value.to_s)}" 
         | 
| 20 | 
            +
                    }.join("&")
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
              
         | 
| 24 | 
            +
              def self.to_ruby_convention(name)
         | 
| 25 | 
            +
                return name.split(/(?=[A-Z])/).map do |word|
         | 
| 26 | 
            +
                  word.downcase
         | 
| 27 | 
            +
                end.join('_')
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
            end
         | 
| @@ -0,0 +1,44 @@ | |
| 1 | 
            +
            module Skanetrafiken
         | 
| 2 | 
            +
              class GetDepartureArrival
         | 
| 3 | 
            +
                def initialize opts={}
         | 
| 4 | 
            +
                    @xmltojson = opts[:xml_to_json] || XmlToJson.new()
         | 
| 5 | 
            +
                end
         | 
| 6 | 
            +
                def render_url(point)
         | 
| 7 | 
            +
                  return "http://www.labs.skanetrafiken.se/v2.2/stationresults.asp?selPointFrKey=#{point.id}"
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
                
         | 
| 10 | 
            +
                def get_lines(html)
         | 
| 11 | 
            +
                  doc = REXML::Document.new(html)
         | 
| 12 | 
            +
                  el = []
         | 
| 13 | 
            +
                  v = doc.elements["soap:Envelope/soap:Body/GetDepartureArrivalResponse/GetDepartureArrivalResult/Lines"]
         | 
| 14 | 
            +
                  v.elements.each("Line") { |j|
         | 
| 15 | 
            +
                    dic = {}
         | 
| 16 | 
            +
                    $properties_line.each{ |key,value|
         | 
| 17 | 
            +
                      xval = j.elements[value].text
         | 
| 18 | 
            +
                      dic[key] = xval
         | 
| 19 | 
            +
                      #puts "#{key} = #{value} = #{xval}"
         | 
| 20 | 
            +
                    }
         | 
| 21 | 
            +
                    #deviation 
         | 
| 22 | 
            +
                    info = j.elements["RealTime/RealTimeInfo"]
         | 
| 23 | 
            +
                    if info 
         | 
| 24 | 
            +
                      dic[:dep_time_deviation] = info.elements["DepTimeDeviation"].text
         | 
| 25 | 
            +
                      dic[:dep_deviation_affect] = info.elements["DepDeviationAffect"].text
         | 
| 26 | 
            +
                    end
         | 
| 27 | 
            +
                    #<RealTime>
         | 
| 28 | 
            +
                    #<RealTimeInfo>
         | 
| 29 | 
            +
                    #<DepTimeDeviation>40</DepTimeDeviation>
         | 
| 30 | 
            +
                    #<DepDeviationAffect>CRITICAL</DepDeviationAffect>
         | 
| 31 | 
            +
                    
         | 
| 32 | 
            +
                    #puts dic.map{ |k,v| "#{k}, #{v}" }.join('; ')
         | 
| 33 | 
            +
                    el.push(Line.new(dic))
         | 
| 34 | 
            +
                  }
         | 
| 35 | 
            +
                  return el
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
                
         | 
| 38 | 
            +
                def json(html)
         | 
| 39 | 
            +
                  doc = REXML::Document.new(html)
         | 
| 40 | 
            +
                  xml = doc.elements["soap:Envelope/soap:Body/GetDepartureArrivalResponse/GetDepartureArrivalResult/Lines"]
         | 
| 41 | 
            +
                  return @xmltojson.convert( xml.to_s )
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
              end
         | 
| 44 | 
            +
            end
         | 
| @@ -0,0 +1,35 @@ | |
| 1 | 
            +
            module Skanetrafiken
         | 
| 2 | 
            +
              class GetJourney
         | 
| 3 | 
            +
                def initialize opts = {}
         | 
| 4 | 
            +
                  @uri = UriHelper.new
         | 
| 5 | 
            +
                  @xmltojson = opts[:xml_to_json] || XmlToJson.new()
         | 
| 6 | 
            +
                end
         | 
| 7 | 
            +
                def render_url(pointFrom,pointTo,lastStart)
         | 
| 8 | 
            +
                  lastStartText = lastStart.strftime("%Y-%m-%d %H:%M")
         | 
| 9 | 
            +
                  to = pointTo.render()
         | 
| 10 | 
            +
                  from = pointFrom.render()
         | 
| 11 | 
            +
                  parameters = {
         | 
| 12 | 
            +
                    :cmdaction =>:next,
         | 
| 13 | 
            +
                    :selPointFr =>pointFrom.render(),
         | 
| 14 | 
            +
                    :selPointTo =>pointTo.render(),
         | 
| 15 | 
            +
                    :LastStart =>lastStartText
         | 
| 16 | 
            +
                  }
         | 
| 17 | 
            +
                  return "http://www.labs.skanetrafiken.se/v2.2/resultspage.asp?" + @uri.parameters_from_hash(parameters)
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
                
         | 
| 20 | 
            +
                def get_times(html)
         | 
| 21 | 
            +
                    doc = REXML::Document.new(html)
         | 
| 22 | 
            +
                    el = []
         | 
| 23 | 
            +
                    doc.elements["soap:Envelope/soap:Body/GetJourneyResponse/GetJourneyResult/Journeys"]\
         | 
| 24 | 
            +
                    .elements.each("Journey") { |j|
         | 
| 25 | 
            +
                        el.push(j.elements["DepDateTime"].text)
         | 
| 26 | 
            +
                    }
         | 
| 27 | 
            +
                    return el
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
                def json(html)
         | 
| 30 | 
            +
                    doc = REXML::Document.new(html)
         | 
| 31 | 
            +
                    xml = doc.elements["soap:Envelope/soap:Body/GetJourneyResponse/GetJourneyResult/Journeys"]
         | 
| 32 | 
            +
                    return @xmltojson.convert( xml.to_s )
         | 
| 33 | 
            +
                end
         | 
| 34 | 
            +
              end
         | 
| 35 | 
            +
            end
         | 
| @@ -0,0 +1,33 @@ | |
| 1 | 
            +
            module Skanetrafiken
         | 
| 2 | 
            +
              $properties_line = {:name=>'Name',:no=>'No',:journey_date_time=>'JourneyDateTime',:is_timing_point=>'IsTimingPoint',\
         | 
| 3 | 
            +
                :stop_point=>'StopPoint',:line_type_id=>'LineTypeId',:line_type_id=>'LineTypeId',:line_type_name=>'LineTypeName',\
         | 
| 4 | 
            +
                :towards=>'Towards'}
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              class Line
         | 
| 7 | 
            +
                attr_reader :name,:no,:journey_date_time,:is_timing_point,\
         | 
| 8 | 
            +
                  :stop_point,:line_type_id,:line_type_id,:line_type_name,\
         | 
| 9 | 
            +
                  :towards
         | 
| 10 | 
            +
                def initialize dic
         | 
| 11 | 
            +
                  $properties_line.each{ |key,value|
         | 
| 12 | 
            +
                     val = dic[key]
         | 
| 13 | 
            +
                     instance_variable_set("@#{key.to_s}", val)
         | 
| 14 | 
            +
                  }
         | 
| 15 | 
            +
                  #puts @name
         | 
| 16 | 
            +
                  #puts dic.map{ |k,v| "#{k}: #{v}" }.join('; ')
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
                def ==(another)
         | 
| 19 | 
            +
                  return $properties_line.map{ |key,value|
         | 
| 20 | 
            +
                    k = "@#{key.to_s}"
         | 
| 21 | 
            +
                    instance_variable_get(k) == another.instance_variable_get(k)
         | 
| 22 | 
            +
                  }.inject(true){ |result, element| result and element}
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
                def to_s
         | 
| 25 | 
            +
                  a = []
         | 
| 26 | 
            +
                  $properties_line.each{ |key,value|
         | 
| 27 | 
            +
                    val = instance_variable_get("@#{key.to_s}")
         | 
| 28 | 
            +
                    a.push(":#{key.to_s}=> '#{val}'")
         | 
| 29 | 
            +
                  }
         | 
| 30 | 
            +
                  return a.join(", ")
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
              end
         | 
| 33 | 
            +
            end
         | 
| @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            module Skanetrafiken
         | 
| 2 | 
            +
              
         | 
| 3 | 
            +
              $point_indexes = [:stop, :address, :poi, :unknown]
         | 
| 4 | 
            +
              
         | 
| 5 | 
            +
              class Point
         | 
| 6 | 
            +
                attr_reader :name, :id, :type
         | 
| 7 | 
            +
                def initialize(name,id,type)
         | 
| 8 | 
            +
                  @name=name
         | 
| 9 | 
            +
                  @id=id
         | 
| 10 | 
            +
                  @type=type
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
                def ==(another_point)
         | 
| 13 | 
            +
                  self.name == another_point.name && self.id == another_point.id && self.type == another_point.type
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
                def render()
         | 
| 16 | 
            +
                    idx = $point_indexes.index(@type)
         | 
| 17 | 
            +
                   return "#{@name}|#{@id}|#{idx}"
         | 
| 18 | 
            +
                end    
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            end
         | 
| @@ -0,0 +1,31 @@ | |
| 1 | 
            +
            module Skanetrafiken
         | 
| 2 | 
            +
              
         | 
| 3 | 
            +
              $point_string_totype ={ "STOP_AREA" => :stop, "ADDRESS" => :address, "POI" => :poi, "UNKNOWN" => :unknown}
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              class QueryStation
         | 
| 6 | 
            +
                def initialize opts={}
         | 
| 7 | 
            +
                    @xmltojson = opts[:xml_to_json] || XmlToJson.new()
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
                def render_url(name)
         | 
| 10 | 
            +
                    return "http://www.labs.skanetrafiken.se/v2.2/querystation.asp?inpPointfr=#{URI.escape(name)}"
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
                def get_stations(html)
         | 
| 13 | 
            +
                    doc = REXML::Document.new(html)
         | 
| 14 | 
            +
                    el = []
         | 
| 15 | 
            +
                    v = doc.elements["soap:Envelope/soap:Body/GetStartEndPointResponse/GetStartEndPointResult/StartPoints"]
         | 
| 16 | 
            +
                    v.elements.each("Point") { |j|
         | 
| 17 | 
            +
                        el.push(Point.new(
         | 
| 18 | 
            +
                          j.elements["Name"].text,
         | 
| 19 | 
            +
                          j.elements["Id"].text,
         | 
| 20 | 
            +
                          $point_string_totype[j.elements["Type"].text]
         | 
| 21 | 
            +
                        ))
         | 
| 22 | 
            +
                    }
         | 
| 23 | 
            +
                    return el
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
                def json(html)
         | 
| 26 | 
            +
                  doc = REXML::Document.new(html)
         | 
| 27 | 
            +
                  xml = doc.elements["soap:Envelope/soap:Body/GetStartEndPointResponse/GetStartEndPointResult/StartPoints"]
         | 
| 28 | 
            +
                  return @xmltojson.convert( xml.to_s )
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
              end
         | 
| 31 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,50 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: skanetrafiken
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.1.1
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Oskar Gewalli
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: bin
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2013-09-21 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies: []
         | 
| 13 | 
            +
            description: Using ruby to query some of the API for Skanetrafiken. It can be found
         | 
| 14 | 
            +
              at http://www.labs.skanetrafiken.se/api.asp
         | 
| 15 | 
            +
            email:
         | 
| 16 | 
            +
            - oskar@gewalli.se
         | 
| 17 | 
            +
            executables: []
         | 
| 18 | 
            +
            extensions: []
         | 
| 19 | 
            +
            extra_rdoc_files: []
         | 
| 20 | 
            +
            files:
         | 
| 21 | 
            +
            - lib/skanetrafiken/get_departure_arrival.rb
         | 
| 22 | 
            +
            - lib/skanetrafiken/get_journey.rb
         | 
| 23 | 
            +
            - lib/skanetrafiken/line.rb
         | 
| 24 | 
            +
            - lib/skanetrafiken/point.rb
         | 
| 25 | 
            +
            - lib/skanetrafiken/query_station.rb
         | 
| 26 | 
            +
            - lib/skanetrafiken.rb
         | 
| 27 | 
            +
            homepage: https://github.com/wallymathieu/skanetrafiken
         | 
| 28 | 
            +
            licenses: []
         | 
| 29 | 
            +
            metadata: {}
         | 
| 30 | 
            +
            post_install_message: 
         | 
| 31 | 
            +
            rdoc_options: []
         | 
| 32 | 
            +
            require_paths:
         | 
| 33 | 
            +
            - lib
         | 
| 34 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 35 | 
            +
              requirements:
         | 
| 36 | 
            +
              - - '>='
         | 
| 37 | 
            +
                - !ruby/object:Gem::Version
         | 
| 38 | 
            +
                  version: '0'
         | 
| 39 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 40 | 
            +
              requirements:
         | 
| 41 | 
            +
              - - '>='
         | 
| 42 | 
            +
                - !ruby/object:Gem::Version
         | 
| 43 | 
            +
                  version: '0'
         | 
| 44 | 
            +
            requirements: []
         | 
| 45 | 
            +
            rubyforge_project: 
         | 
| 46 | 
            +
            rubygems_version: 2.0.3
         | 
| 47 | 
            +
            signing_key: 
         | 
| 48 | 
            +
            specification_version: 4
         | 
| 49 | 
            +
            summary: Using ruby to query the API provided by Skanetrafiken
         | 
| 50 | 
            +
            test_files: []
         |