ns-yapi 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ns-yapi (0.2.0)
4
+ ns-yapi (0.2.1)
5
5
  httpclient
6
6
  nokogiri
7
7
  nori
data/lib/ns_client.rb CHANGED
@@ -74,44 +74,83 @@ class NSClient
74
74
  raise InvalidStationNameError, message
75
75
  end
76
76
 
77
- (xdoc/'/Storingen').each { |disruption|
77
+ (xdoc/'/Storingen').each do |disruption|
78
78
 
79
- (disruption/'Ongepland/Storing').each { |unplanned|
79
+ (disruption/'Ongepland/Storing').each do |unplanned|
80
80
  unplanned_disruption = UnplannedDisruption.new
81
81
  unplanned_disruption.id = (unplanned/'./id').text
82
82
  unplanned_disruption.trip = (unplanned/'./Traject').text
83
83
  unplanned_disruption.reason = (unplanned/'./Reden').text
84
84
  unplanned_disruption.message = (unplanned/'./Bericht').text
85
85
  unplanned_disruption.datetime_string = (unplanned/'./Datum').text
86
+ unplanned_disruption.cause = (unplanned/'./Oorzaak').text
86
87
  result[:unplanned] << unplanned_disruption
87
- }
88
+ end
88
89
 
89
- (disruption/'Gepland/Storing').each { |planned|
90
+ (disruption/'Gepland/Storing').each do |planned|
90
91
  planned_disruption = PlannedDisruption.new
91
92
  planned_disruption.id = (planned/'./id').text
92
93
  planned_disruption.trip = (planned/'./Traject').text
93
94
  planned_disruption.reason = (planned/'./Reden').text
94
95
  planned_disruption.advice = (planned/'./Advies').text
95
96
  planned_disruption.message = (planned/'./Bericht').text
97
+ planned_disruption.cause = (planned/'./Oorzaak').text
96
98
  result[:planned] << planned_disruption
97
- }
98
- }
99
+ end
100
+ end
99
101
  result
100
102
  end
101
103
 
104
+ def prices (opts = {from: nil, to: nil, via: nil, date: nil})
105
+ date = opts[:date]
106
+ response = @client.get "http://@webservices.ns.nl/ns-api-prijzen-v2?from=#{opts[:from]}&to=#{opts[:to]}&via=#{opts[:via]}&date=#{date.strftime("%d%m%Y")}"
107
+ xdoc = Nokogiri.XML(response.content)
108
+ prices_response = PricesResponse.new
109
+ (xdoc/'/Producten').each do |products|
110
+ prices_response.tariff_units = (products/'./Tariefeenheden').text.to_i
111
+
112
+ (products/'Product').each do |product|
113
+ prices = []
114
+ (product/'Prijs').each do |price_element|
115
+ product_price = ProductPrice.new
116
+ product_price.amount = price_element.text
117
+ prices << product_price
118
+ end
119
+ name = product.attr('naam')
120
+ prices_response.products[name] = prices
121
+ end
122
+
123
+ end
124
+ prices_response
125
+ end
126
+
127
+
102
128
  def disruption_url(query)
103
129
  if query
104
130
  return "http://webservices.ns.nl/ns-api-storingen?station=#{query}"
105
131
  end
106
- "http://webservices.ns.nl/ns-api-storingen?"
132
+ "http://webservices.ns.nl/ns-api-storingen?actual=true"
133
+ end
134
+
135
+ class PricesResponse
136
+ attr_accessor :tariff_units, :products
137
+
138
+ def initialize
139
+ @products = {}
140
+ @tariff_units = 0
141
+ end
142
+ end
143
+
144
+ class ProductPrice
145
+ attr_accessor :discount, :class, :amount
107
146
  end
108
147
 
109
148
  class UnplannedDisruption
110
- attr_accessor :id, :trip, :reason, :message, :datetime_string
149
+ attr_accessor :id, :trip, :reason, :message, :datetime_string, :cause
111
150
  end
112
151
 
113
152
  class PlannedDisruption
114
- attr_accessor :id, :trip, :reason, :advice, :message
153
+ attr_accessor :id, :trip, :reason, :advice, :message, :cause
115
154
  end
116
155
 
117
156
  class Station
data/ns.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "ns-yapi"
7
- gem.version = '0.2.0'
7
+ gem.version = '0.2.1'
8
8
  gem.authors = ["Stefan Hendriks"]
9
9
  gem.email = ["stefanhen83@gmail.com"]
10
10
  gem.description = %q{Yet Another (Ruby) NS API client}
@@ -6,6 +6,7 @@
6
6
  <Reden>beperkingen op last van de politie</Reden>
7
7
  <Bericht>Another test message</Bericht>
8
8
  <Datum>2010-12-16T11:16:00+0100</Datum>
9
+ <Oorzaak>oorzaak</Oorzaak>
9
10
  </Storing>
10
11
  </Ongepland>
11
12
  <Gepland>
@@ -18,6 +19,7 @@
18
19
  plaats van de trein tussen Almere Centrum en Lelystad Centrum rijden vier Sprinters per uur reis tussen Almere
19
20
  Muziekwijk en Naarden-Bussum via Weesp</Advies>
20
21
  <Bericht>Test message</Bericht>
22
+ <Oorzaak>oorzaak</Oorzaak>
21
23
  </Storing>
22
24
  </Gepland>
23
25
  </Storingen>
@@ -37,7 +37,7 @@ describe NSClient do
37
37
  context "Disruptions" do
38
38
 
39
39
  it "should retrieve planned and unplanned disruptions" do
40
- stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-storingen?", load_fixture('disruptions.xml')
40
+ stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-storingen?actual=true", load_fixture('disruptions.xml')
41
41
  disruptions = @client.disruptions
42
42
  disruptions.size.should == 2
43
43
  disruptions[:planned].size.should == 1
@@ -45,7 +45,7 @@ describe NSClient do
45
45
  end
46
46
 
47
47
  it "should retrieve expected planned disruption" do
48
- stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-storingen?", load_fixture('disruptions.xml')
48
+ stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-storingen?actual=true", load_fixture('disruptions.xml')
49
49
  disruptions = @client.disruptions
50
50
  disruptions.size.should == 2
51
51
  planned_disruption = disruptions[:planned].first
@@ -58,10 +58,11 @@ describe NSClient do
58
58
  plaats van de trein tussen Almere Centrum en Lelystad Centrum rijden vier Sprinters per uur reis tussen Almere
59
59
  Muziekwijk en Naarden-Bussum via Weesp"
60
60
  planned_disruption.message.should == "Test message"
61
+ planned_disruption.cause.should == "oorzaak"
61
62
  end
62
63
 
63
64
  it "should retrieve expected unplanned disruption" do
64
- stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-storingen?", load_fixture('disruptions.xml')
65
+ stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-storingen?actual=true", load_fixture('disruptions.xml')
65
66
  disruptions = @client.disruptions
66
67
  disruptions.size.should == 2
67
68
  unplanned_disruption = disruptions[:unplanned].first
@@ -70,12 +71,13 @@ describe NSClient do
70
71
  unplanned_disruption.id.should == "prio-13345"
71
72
  unplanned_disruption.trip.should == "'s-Hertogenbosch-Nijmegen"
72
73
  unplanned_disruption.reason.should == "beperkingen op last van de politie"
74
+ unplanned_disruption.cause.should == "oorzaak"
73
75
  unplanned_disruption.message.should == "Another test message"
74
76
  unplanned_disruption.datetime_string == "2010-12-16T11:16:00+0100" #intentional, give raw data. Let user parse if needed.
75
77
  end
76
78
 
77
79
  it "should not return disruption when empty in response" do
78
- stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-storingen?", load_fixture('no_disruptions.xml')
80
+ stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-storingen?actual=true", load_fixture('no_disruptions.xml')
79
81
  disruptions = @client.disruptions
80
82
  disruptions.size.should == 2
81
83
  disruptions[:planned].size.should == 0
@@ -103,7 +105,25 @@ describe NSClient do
103
105
  end
104
106
 
105
107
  context "Prices" do
106
- xit "should retrieve prices for a trip"
108
+ it "should retrieve prices for a trip" do
109
+ stub_ns_client_request "http://username:password@webservices.ns.nl/ns-api-prijzen-v2?from=Purmerend&to=Amsterdam&via=Zaandam&date=17062013", load_fixture('prices.xml')
110
+ date = Date.strptime('17-06-2013', '%d-%m-%Y')
111
+ response = @client.prices from: "Purmerend", to: "Amsterdam", via: "Zaandam", date: date
112
+ response.class.should == NSClient::PricesResponse
113
+ response.tariff_units.should == 10
114
+ response.products.size.should == 2
115
+
116
+ enkele_reis = response.products["Enkele reis"]
117
+ enkele_reis.size.should == 6
118
+ end
119
+
120
+ xit "should retrieve expected price data"
121
+
122
+ xit "assumes date is now, when not given"
123
+ xit "should treat via as optional parameter"
124
+ xit "should raise error when from is not given"
125
+ xit "should raise error when to is not given"
126
+
107
127
  end
108
128
 
109
129
  def stub_ns_client_request(url, response)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ns-yapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-17 00:00:00.000000000 Z
12
+ date: 2013-06-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httpclient
@@ -98,7 +98,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
98
98
  version: '0'
99
99
  segments:
100
100
  - 0
101
- hash: 2729208027955836065
101
+ hash: -4220924222430246748
102
102
  required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  none: false
104
104
  requirements:
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  version: '0'
108
108
  segments:
109
109
  - 0
110
- hash: 2729208027955836065
110
+ hash: -4220924222430246748
111
111
  requirements: []
112
112
  rubyforge_project:
113
113
  rubygems_version: 1.8.24