rome2rio 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 03ec9441a72831df297c2ac4281c433169c7d08e
4
+ data.tar.gz: 9629577e4d586cbd3522ab82dcd7216903418063
5
+ SHA512:
6
+ metadata.gz: 8b34778ea682a760038375d3e3d5929eede320dcac96d1bd73a04ce7f7cdccd28942e2f63e765799c0f9959a0699e4ddc73cda79ddbb4e9f1912ee1aa54acb34
7
+ data.tar.gz: f97f41bd12a419cb0cdbde70f993fc4f5bc9da8c9513857e49cf2f5c874653af9d445e07361fa14ad1c0447f27b9fd0d5ba999d057c9bd3e4465eae49c59518b
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Alex Beregszaszi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ Rome2rio
2
+ ========
3
+
4
+ A Ruby wrapper for the Rome2rio API. See http://www.rome2rio.com/documentation/search for the official documentation.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ ```
10
+ gem install rome2rio
11
+ ```
12
+
13
+
14
+ Usage
15
+ -----
16
+
17
+ ```
18
+ require 'rome2rio'
19
+ results = Rome2rio::Connection.new.search(search options)
20
+ puts results.routes[0].duration
21
+ ```
22
+
23
+ All field names (both request and response) use the original naming as in the API documentation.
24
+
25
+ The API key and endpoint URL both can be set when creating an instance, but they can be overwritten by passing them for a request.
26
+
27
+ Complex datatypes (such as Position, Size, Offset, DayFlags) will be parsed.
28
+
29
+ Using Position for input:
30
+
31
+ ```
32
+ Rome2rio::Connection.new.search({:oPos => Rome2rio::Position.new(41.79443,12.25108), :dPos => Rome2rio::Position.new(-22.81215,-43.24721)})
33
+ ```
34
+
35
+ The original JSON response is also available in the ```verbatim``` field.
36
+
37
+ Copyright
38
+ ---------
39
+
40
+ Made for Soundtravel (http://soundtravel.co/).
41
+
42
+ Copyright (c) 2013 Alex Beregszaszi. See LICENSE for details.
@@ -0,0 +1,14 @@
1
+ require 'faraday'
2
+ require 'multi_json'
3
+
4
+ require 'rome2rio/connection'
5
+
6
+ require 'rome2rio/helper/encoded_number_pair'
7
+ require 'rome2rio/helper/offset'
8
+ require 'rome2rio/helper/position'
9
+ require 'rome2rio/helper/size'
10
+ require 'rome2rio/helper/encoded_flags'
11
+ require 'rome2rio/helper/day_flags'
12
+ require 'rome2rio/helper/search_request_flags'
13
+
14
+ Dir[File.dirname(__FILE__) + '/rome2rio/response/*.rb'].each { |file| require file }
@@ -0,0 +1,37 @@
1
+ module Rome2rio
2
+ class Connection
3
+ def initialize(opts = {})
4
+ @apikey = opts[:apikey]
5
+ @endpoint = opts[:endpoint] || "http://evaluate.rome2rio.com"
6
+ end
7
+
8
+ def handle_response(response)
9
+ if response.status == 200
10
+ SearchResponse.new(MultiJson.decode(response.body))
11
+ else
12
+ {:status => response.status, :body => response.body}
13
+ end
14
+ end
15
+
16
+ def encode_params(params)
17
+ URI.encode_www_form(params)
18
+ end
19
+
20
+ def search(opts)
21
+ opts[:key] ||= @apikey if @apikey
22
+
23
+ # format SearchFlags
24
+ flags = opts[:flags]
25
+ opts[:flags] = SearchRequestFlags.new(flags) if flags.is_a?(Symbol) or flags.is_a?(Array)
26
+
27
+ request = "/api/1.2/json/Search?#{encode_params(opts)}"
28
+
29
+ handle_response(Faraday.new(:url => @endpoint).get(request))
30
+
31
+ # handle_response(conn.get do |req|
32
+ # req.headers['Content-Type'] = 'application/json'
33
+ # req.url request_url
34
+ # end)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ module Rome2rio
2
+ class DayFlags < EncodedFlags
3
+ FLAG_VALUES = {
4
+ :sunday => 0x01,
5
+ :sun => 0x01,
6
+ :monday => 0x02,
7
+ :mon => 0x02,
8
+ :tuesday => 0x04,
9
+ :tue => 0x04,
10
+ :wednesday => 0x08,
11
+ :wed => 0x08,
12
+ :thursday => 0x10,
13
+ :thu => 0x10,
14
+ :friday => 0x20,
15
+ :fri => 0x20,
16
+ :saturday => 0x40,
17
+ :sat => 0x40,
18
+ :weekdays => 0x3E,
19
+ :weekends => 0x41,
20
+ :all => 0x7F
21
+ }
22
+ end
23
+ end
@@ -0,0 +1,51 @@
1
+ module Rome2rio
2
+ class EncodedFlags
3
+ attr_accessor :flags
4
+
5
+ def initialize(flags = nil)
6
+ @flags = flags
7
+ end
8
+
9
+ def self.parse(str)
10
+ value = Integer(str)
11
+ ret = new
12
+ ret.flags = []
13
+ self::FLAG_VALUES.each { |key, flag| ret.flags << key if (value & flag) == flag }
14
+ ret
15
+ end
16
+
17
+ def to_a
18
+ return flags.map { |k, v| k if v == true || v == 1 }.select { |k| k != nil } if flags.is_a?(Hash)
19
+ return [ flags.to_sym ] if flags.is_a?(String)
20
+ return [ flags ] if flags.is_a?(Symbol)
21
+ flags
22
+ end
23
+
24
+ def method_missing(meth, *args, &block)
25
+ if meth.to_s.end_with?("?") then
26
+ key = meth.to_s.chop.to_sym
27
+
28
+ return true if to_a.include?(key)
29
+
30
+ # return flags.include?(key) if flags.is_a?(Array)
31
+ # return flags[key] if flags.is_a?(Hash)
32
+ # return true if flags == key
33
+
34
+ end
35
+
36
+ super(meth, *args, &block)
37
+ end
38
+
39
+ def to_s
40
+ to_i.to_s
41
+ end
42
+
43
+ def to_i
44
+ keys = to_a
45
+
46
+ ret = 0
47
+ self.class::FLAG_VALUES.each { |key, flag| ret |= flag if keys.include?(key) }
48
+ ret
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,23 @@
1
+ module Rome2rio
2
+ class EncodedNumberPair
3
+ attr_accessor :a, :b
4
+
5
+ def initialize(a = nil, b = nil)
6
+ @a = a
7
+ @b = b
8
+ end
9
+
10
+ def self.parse(str)
11
+ # failsafe for empty data
12
+ return if not str or str.strip.length == 0
13
+
14
+ ret = new
15
+ str.scan(/([\d\.]+), ([\d\.]+)/).collect { |a, b| ret.a, ret.b = a, b }
16
+ ret
17
+ end
18
+
19
+ def to_s
20
+ "#{a}, #{b}"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ module Rome2rio
2
+ class Offset < EncodedNumberPair
3
+ attr_accessor :x, :y
4
+ alias :x :a
5
+ alias :y :b
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Rome2rio
2
+ class Position < EncodedNumberPair
3
+ attr_accessor :lat, :lng
4
+ alias :lat :a
5
+ alias :lng :b
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ module Rome2rio
2
+ class SearchRequestFlags < EncodedFlags
3
+ FLAG_VALUES = {
4
+ :all => 0x00000000,
5
+ :no_segments => 0x000fffff,
6
+ :no_flight_segments => 0x00000001,
7
+ :no_flight_itineraries => 0x00000002,
8
+ :no_train_segments => 0x00000010,
9
+ :no_train_itineraries => 0x00000020,
10
+ :no_bus_segments => 0x00000100,
11
+ :no_bus_itineraries => 0x00000200,
12
+ :no_ferry_segments => 0x00001000,
13
+ :no_ferry_itineraries => 0x00002000,
14
+ :no_car_segments => 0x00010000,
15
+ :no_commuter_hops => 0x00100000,
16
+ :no_special_hops => 0x00200000,
17
+ :no_minor_start_segments => 0x00400000,
18
+ :no_minor_end_segments => 0x00800000,
19
+ :no_paths => 0x01000000,
20
+ :no_prices => 0x04000000,
21
+ :no_scoring => 0x10000000,
22
+ }
23
+ end
24
+ end
@@ -0,0 +1,7 @@
1
+ module Rome2rio
2
+ class Size < EncodedNumberPair
3
+ attr_accessor :w, :h
4
+ alias :w :a
5
+ alias :h :b
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ module Rome2rio
2
+ class Agency
3
+ attr_reader :code, :name, :url, :iconPath, :iconSize, :iconOffset
4
+ def initialize(json)
5
+ @code = json["code"]
6
+ @name = json["name"]
7
+ @url = json["url"]
8
+ @iconPath = json["iconPath"]
9
+ @iconSize = Size.parse(json["iconSize"])
10
+ @iconOffset = Offset.parse(json["iconOffset"])
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Rome2rio
2
+ class Airline
3
+ attr_reader :code, :name, :url, :iconPath, :iconSize, :iconOffset
4
+ def initialize(json)
5
+ @code = json["code"]
6
+ @name = json["name"]
7
+ @url = json["url"]
8
+ @iconPath = json["iconPath"]
9
+ @iconSize = Size.parse(json["iconSize"])
10
+ @iconOffset = Offset.parse(json["iconOffset"])
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module Rome2rio
2
+ class Airport
3
+ attr_reader :code, :name, :pos
4
+ def initialize(json)
5
+ @code = json["code"]
6
+ @name = json["name"]
7
+ @pos = Position.parse(json["pos"])
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+ module Rome2rio
2
+ class FlightHop
3
+ attr_reader :sCode, :tCode, :sTime, :tTime, :airline, :flight,
4
+ :duration, :dayChange, :lDuration, :lDayChange
5
+ def initialize(json)
6
+ @sCode = json["sCode"]
7
+ @tCode = json["tCode"]
8
+ @sTime = json["sTime"]
9
+ @tTime = json["tTime"]
10
+ @airline = json["airline"]
11
+ @flight = json["flight"]
12
+ @duration = json["duration"]
13
+ @dayChange = json["dayChange"] if json["dayChange"]
14
+ @lDuration = json["lDuration"] if json["lDuration"]
15
+ @lDayChange = json["lDayChange"] if json["lDayChange"]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ module Rome2rio
2
+ class FlightItinerary
3
+ attr_reader :legs
4
+ def initialize(json)
5
+ @legs = []
6
+ json["legs"].each { |leg| @legs << FlightLeg.new(leg) }
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module Rome2rio
2
+ class FlightLeg
3
+ attr_reader :days, :hops
4
+ def initialize(json)
5
+ @days = DayFlags.parse(json["days"])
6
+ @hops = []
7
+ json["hops"].each { |hop| @hops << FlightHop.new(hop) }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ module Rome2rio
2
+ class IndicativePrice
3
+ attr_reader :price, :currency, :nativePrice, :nativeCurrency, :isFreeTransfer
4
+ def initialize(json)
5
+ @price = json["price"] if json["price"]
6
+ @currency = json["currency"] if json["currency"]
7
+ @nativePrice = json["nativePrice"] if json["nativePrice"]
8
+ @nativeCurrency = json["nativeCurrency"] if json["nativeCurrency"]
9
+ @isFreeTransfer = json["isFreeTransfer"] == 1 if json["isFreeTransfer"]
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ module Rome2rio
2
+ class Route
3
+ attr_reader :name, :distance, :duration, :stops, :segments
4
+ def initialize(json)
5
+ @name = json["name"]
6
+ @distance = json["distance"]
7
+ @duration = json["duration"]
8
+ @stops = []
9
+ json["stops"].each { |stop| @stops << Stop.new(stop) }
10
+ @segments = []
11
+ json["segments"].each { |segment| @segments << Segment.new(segment) }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ module Rome2rio
2
+ class SearchResponse
3
+ attr_reader :agencies, :airlines, :airports, :routes, :verbatim
4
+ def initialize(json)
5
+ @verbatim = json
6
+
7
+ @agencies = []
8
+ json["agencies"].each { |agency| @agencies << Agency.new(agency) }
9
+
10
+ @airlines = []
11
+ json["airlines"].each { |airline| @airlines << Airline.new(airline) }
12
+
13
+ @airports = []
14
+ json["airports"].each { |airport| @airports << Airport.new(airport) }
15
+
16
+ @routes = []
17
+ json["routes"].each { |route| @routes << Route.new(route) }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,33 @@
1
+ module Rome2rio
2
+ class Segment
3
+ attr_reader :kind, :isMajor, :distance, :duration, :indicativePrice, :isImperial,
4
+ :sName, :sPos, :sCode, :tName, :tPos, :tCode, :path, :itineraries
5
+ def initialize(json)
6
+ # Possible kinds: walk, car, train, bus, ferry, flight
7
+ @kind = json["kind"]
8
+ @isMajor = json["isMajor"]
9
+ @distance = json["distance"]
10
+ @duration = json["duration"]
11
+ @indicativePrice = IndicativePrice.new(json["indicativePrice"])
12
+
13
+ if @kind != "flight" then
14
+ @isImperial = json["isImperial"]
15
+ @sName = json["sName"]
16
+ @sPos = Position.parse(json["sPos"])
17
+ @tName = json["tName"]
18
+ @tPos = Position.parse(json["tPos"])
19
+ @path = json["path"]
20
+ else
21
+ @sCode = json["sCode"]
22
+ @tCode = json["tCode"]
23
+ @itineraries = []
24
+ json["itineraries"].each { |itinerary| @itineraries << FlightItinerary.new(itinerary) }
25
+ end
26
+
27
+ if [ "train", "bus", "ferry" ].include?(@kind) then
28
+ @itineraries = []
29
+ json["itineraries"].each { |itinerary| @itineraries << TransitItinerary.new(itinerary) }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ module Rome2rio
2
+ class Stop
3
+ attr_reader :kind, :name, :pos, :code
4
+ def initialize(json)
5
+ @kind = json["kind"]
6
+ @name = json["name"]
7
+ @pos = Position.parse(json["pos"])
8
+ @code = json["code"]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ module Rome2rio
2
+ class TransitHop
3
+ attr_reader :sName, :sPos, :tName, :tPos, :frequency, :duration, :indicativePrice, :lines
4
+ def initialize(json)
5
+ @sName = json["sName"]
6
+ @sPos = Position.parse(json["sPos"])
7
+ @tName = json["tName"]
8
+ @tPos = Position.parse(json["tPos"])
9
+ @frequency = json["frequency"]
10
+ @duration = json["duration"]
11
+ @indicativePrice = IndicativePrice.new(json["indicativePrice"])
12
+ @lines = []
13
+ json["lines"].each { |line| @lines << TransitLine.new(line) }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ module Rome2rio
2
+ class TransitItinerary
3
+ attr_reader :legs
4
+ def initialize(json)
5
+ @legs = []
6
+ json["legs"].each { |leg| @legs << TransitLeg.new(leg) }
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module Rome2rio
2
+ class TransitLeg
3
+ attr_reader :url, :hops
4
+ def initialize(json)
5
+ @url = json["url"]
6
+ @hops = []
7
+ json["hops"].each { |hop| @hops << TransitHop.new(hop) }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ module Rome2rio
2
+ class TransitLine
3
+ attr_reader :name, :vehicle, :code, :agency, :frequency, :duration
4
+ def initialize(json)
5
+ @name = json["name"]
6
+ @vehicle = json["vehicle"]
7
+ @code = json["code"]
8
+ @agency = json["agency"]
9
+ @frequency = json["frequency"]
10
+ @duration = json["duration"]
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = "rome2rio"
5
+ gem.version = "0.1"
6
+ gem.authors = ["Alex Beregszaszi"]
7
+ gem.email = ["alex@rtfs.hu"]
8
+ gem.description = "A Ruby wrapper for the Rome2rio API. See http://www.rome2rio.com/documentation/search for the official documentation."
9
+ gem.summary = "A Ruby wrapper for the Rome2rio API."
10
+ gem.homepage = "http://github.com/axic/rome2rio"
11
+
12
+ gem.add_dependency 'faraday'
13
+ gem.add_dependency 'multi_json'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.extra_rdoc_files = [
21
+ "LICENSE",
22
+ "README.md"
23
+ ]
24
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rome2rio
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Alex Beregszaszi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A Ruby wrapper for the Rome2rio API. See http://www.rome2rio.com/documentation/search
42
+ for the official documentation.
43
+ email:
44
+ - alex@rtfs.hu
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files:
48
+ - LICENSE
49
+ - README.md
50
+ files:
51
+ - .gitignore
52
+ - LICENSE
53
+ - README.md
54
+ - lib/rome2rio.rb
55
+ - lib/rome2rio/connection.rb
56
+ - lib/rome2rio/helper/day_flags.rb
57
+ - lib/rome2rio/helper/encoded_flags.rb
58
+ - lib/rome2rio/helper/encoded_number_pair.rb
59
+ - lib/rome2rio/helper/offset.rb
60
+ - lib/rome2rio/helper/position.rb
61
+ - lib/rome2rio/helper/search_request_flags.rb
62
+ - lib/rome2rio/helper/size.rb
63
+ - lib/rome2rio/response/agency.rb
64
+ - lib/rome2rio/response/airline.rb
65
+ - lib/rome2rio/response/airport.rb
66
+ - lib/rome2rio/response/flight_hop.rb
67
+ - lib/rome2rio/response/flight_itinerary.rb
68
+ - lib/rome2rio/response/flight_leg.rb
69
+ - lib/rome2rio/response/indicative_price.rb
70
+ - lib/rome2rio/response/route.rb
71
+ - lib/rome2rio/response/search_response.rb
72
+ - lib/rome2rio/response/segment.rb
73
+ - lib/rome2rio/response/stop.rb
74
+ - lib/rome2rio/response/transit_hop.rb
75
+ - lib/rome2rio/response/transit_itinerary.rb
76
+ - lib/rome2rio/response/transit_leg.rb
77
+ - lib/rome2rio/response/transit_line.rb
78
+ - rome2rio.gemspec
79
+ homepage: http://github.com/axic/rome2rio
80
+ licenses: []
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.0.3
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: A Ruby wrapper for the Rome2rio API.
102
+ test_files: []