ratis 3.1.0 → 3.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.
@@ -0,0 +1,230 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ratis::NextBus do
4
+ before do
5
+ Ratis.reset
6
+ Ratis.configure do |config|
7
+ config.endpoint = 'http://soap.valleymetro.org/cgi-bin-soap-web-252/soap.cgi'
8
+ config.namespace = 'PX_WEB'
9
+ end
10
+
11
+ @stop_id = 10050
12
+ @time = Chronic.parse('tomorrow at 6am')
13
+ end
14
+
15
+ describe '#where' do
16
+ before do
17
+ # appid
18
+ # a short string that can be used to separate requests from different applications or different modules with
19
+ # Optional (highly recommended)
20
+ @conditions = {:stop_id => @stop_id,
21
+ :app_id => 'ratis-specs',
22
+ :type => 'N',
23
+ :datetime => @time }
24
+ end
25
+
26
+ it 'returns the next 4 bus times' do
27
+ # raises exception when no runs available:
28
+ # Ratis::Errors::SoapError:
29
+ # SOAP - no runs available
30
+ response = Ratis::NextBus.where(@conditions.dup)
31
+ expect(response.runs).to have(4).items
32
+ end
33
+
34
+ it 'only makes one request' do
35
+ # false just to stop further processing of response
36
+ Ratis::Request.should_receive(:get).once.and_call_original
37
+ Ratis::NextBus.where(@conditions.dup)
38
+ end
39
+
40
+ it 'requests the correct SOAP action' do
41
+ Ratis::Request.should_receive(:get) do |action, options|
42
+ action.should eq('Nextbus')
43
+ options["Stopid"].should eq(@stop_id)
44
+ options["Appid"].should eq('ratis-specs')
45
+ options["Date"].should eq(@time.strftime("%m/%d/%Y"))
46
+ options["Time"].should eq(@time.strftime("%I%M"))
47
+ options["Type"].should eq('N')
48
+
49
+ end.and_return(double('response', :success? => false)) # false only to stop further running
50
+
51
+ Ratis::NextBus.where(@conditions.dup)
52
+ end
53
+
54
+ it "should set all the service values to instance vars" do
55
+ response = Ratis::NextBus.where(@conditions.dup)
56
+ expect(response).to be_a(Ratis::NextBus)
57
+ expect(response.status).to eq('N')
58
+ expect(response.sign).to eq('0 CENTRAL North to Dunlap/3rd St.')
59
+ expect(response.routetype).to eq('B')
60
+ expect(response.times).to eq("05:49 AM, 06:09 AM, 06:29 AM, 06:49 AM")
61
+ expect(response.direction).to eq('N')
62
+ end
63
+
64
+ it "should raise error if datetime condition is not a DateTime or Time" do
65
+ lambda {
66
+ Ratis::NextBus.where(@conditions.dup.merge(:datetime => '01/01/2013'))
67
+ }.should raise_error(ArgumentError, 'If datetime supplied it should be a Time or DateTime instance, otherwise it defaults to Time.now')
68
+
69
+ lambda {
70
+ Ratis::NextBus.where(@conditions.dup.merge(:datetime => Time.now))
71
+ }.should_not raise_error(ArgumentError)
72
+
73
+ lambda {
74
+ Ratis::NextBus.where(@conditions.dup.merge(:datetime => DateTime.today))
75
+ }.should_not raise_error(ArgumentError)
76
+ end
77
+
78
+ it "should raise error if stop id is not provided" do
79
+ lambda {
80
+ Ratis::NextBus.where(@conditions.dup.merge(:stop_id => nil))
81
+ }.should raise_error(ArgumentError, 'You must provide a stop ID')
82
+ end
83
+
84
+ it "should return an empty array if the api request isn't successful" do
85
+ Ratis::Request.should_receive('get').
86
+ with('Nextbus', {"Time"=>@time.strftime("%I%M"), "Type"=>"N", "Stopid"=>10050, "Date"=>@time.strftime("%m/%d/%Y"), "Appid"=>"ratis-specs"}).
87
+ and_return(double('response', :success? => false))
88
+ expect(Ratis::NextBus.where(@conditions.dup)).to be_empty
89
+ end
90
+ end
91
+
92
+ end
93
+
94
+ # EXAMPLE RESPONSE
95
+ # {:envelope => {
96
+ # :"@xmlns:xsd" =>"http://www.w3.org/1999/XMLSchema",
97
+ # :"@xmlns:xsi" =>"http://www.w3.org/1999/XMLSchema-instance",
98
+ # :"@xmlns:soap_env" =>"http://schemas.xmlsoap.org/soap/envelope/",
99
+ # :"@xmlns:soap_enc" =>"http://schemas.xmlsoap.org/soap/encoding/",
100
+ # :"@soap_env:encoding_style" =>"http://schemas.xmlsoap.org/soap/encoding/",
101
+ # :body => {
102
+ # :nextbus_response => {
103
+ # :atstop => {
104
+ # :side => "Far",
105
+ # :stopid => "10050",
106
+ # :heading => "NB",
107
+ # :lat => "33.363692",
108
+ # :area => "Phoenix",
109
+ # :service => {
110
+ # :status => "N",
111
+ # :sign => "0 CENTRAL North to Dunlap/3rd St.",
112
+ # :routetype => "B",
113
+ # :times => "10:29 AM, 10:49 AM, 11:09 AM, 11:29 AM",
114
+ # :direction => "N",
115
+ # :tripinfo => [
116
+ # {:tripid => "8374-8",
117
+ # :exception => "N",
118
+ # :skedtripid => nil,
119
+ # :estimatedtime => "10:29 AM",
120
+ # :triptime => "10:29 AM",
121
+ # :adherence => nil,
122
+ # :realtime => {
123
+ # :valid => nil,
124
+ # :reliable => nil,
125
+ # :estimatedtime => "10:29 AM",
126
+ # :stopped => nil,
127
+ # :lat => nil,
128
+ # :estimatedminutes => nil,
129
+ # :vehicleid => nil,
130
+ # :polltime => nil,
131
+ # :long => nil,
132
+ # :adherence => nil,
133
+ # :trend => nil,
134
+ # :speed => nil},
135
+ # :block => "3"
136
+ # },
137
+ # {:tripid => "8374-8",
138
+ # :exception => "N",
139
+ # :skedtripid => nil,
140
+ # :estimatedtime => "10:49 AM",
141
+ # :triptime => "10:49 AM",
142
+ # :adherence => nil,
143
+ # :realtime => {
144
+ # :valid => nil,
145
+ # :reliable => nil,
146
+ # :estimatedtime => "10:49 AM",
147
+ # :stopped => nil,
148
+ # :lat => nil,
149
+ # :estimatedminutes => nil,
150
+ # :vehicleid => nil,
151
+ # :polltime => nil,
152
+ # :long => nil,
153
+ # :adherence => nil,
154
+ # :trend => nil,
155
+ # :speed => nil
156
+ # },
157
+ # :block => "5"
158
+ # },
159
+ # {:tripid => "8374-9",
160
+ # :exception => "N",
161
+ # :skedtripid => nil,
162
+ # :estimatedtime => "11:09 AM",
163
+ # :triptime => "11:09 AM",
164
+ # :adherence => nil,
165
+ # :realtime => {
166
+ # :valid => nil,
167
+ # :reliable => nil,
168
+ # :estimatedtime => "11:09 AM",
169
+ # :stopped => nil,
170
+ # :lat => nil,
171
+ # :estimatedminutes => nil,
172
+ # :vehicleid => nil,
173
+ # :polltime => nil,
174
+ # :long => nil,
175
+ # :adherence => nil,
176
+ # :trend => nil,
177
+ # :speed => nil
178
+ # },
179
+ # :block => "8"},
180
+ # {:tripid => "8374-9",
181
+ # :exception => "N",
182
+ # :skedtripid => nil,
183
+ # :estimatedtime => "11:29 AM",
184
+ # :triptime => "11:29 AM",
185
+ # :adherence => nil,
186
+ # :realtime => {
187
+ # :valid => nil,
188
+ # :reliable => nil,
189
+ # :estimatedtime => "11:29 AM",
190
+ # :stopped => nil,
191
+ # :lat => nil,
192
+ # :estimatedminutes => nil,
193
+ # :vehicleid => nil,
194
+ # :polltime => nil,
195
+ # :long => nil,
196
+ # :adherence => nil,
197
+ # :trend => nil,
198
+ # :speed => nil
199
+ # },
200
+ # :block => "11"}
201
+ # ],
202
+ # :servicetype => "W",
203
+ # :route => "0",
204
+ # :operator => "AP"
205
+ # },
206
+ # :description => "CENTRAL AVE & DOBBINS RD",
207
+ # :atisstopid => "3317",
208
+ # :stopposition => "O",
209
+ # :long => "-112.073191",
210
+ # :stopstatustype => "N",
211
+ # :walkdist => "0.00"
212
+ # },
213
+ # :version => "1.26",
214
+ # :responsecode => "0",
215
+ # :input => {
216
+ # :time => "10:40 AM",
217
+ # :stopid => "10050",
218
+ # :date => "06/05/2013",
219
+ # :locationlat => nil,
220
+ # :landmarkid => nil,
221
+ # :locationlong => nil,
222
+ # :atisstopid => "0",
223
+ # :locationtext => "Location",
224
+ # :route => nil
225
+ # },
226
+ # :host => "s-rpta-soap",
227
+ # :"@xmlns:namesp1" => "PX_WEB",
228
+ # :copyright => "XML schema Copyright (c) 2003-2012 Trapeze Software, Inc., all rights reserved.",
229
+ # :soapversion => "2.5.2 - 06/07/12"}}}}
230
+
@@ -0,0 +1,122 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ratis::Pattern do
4
+ before do
5
+ Ratis.reset
6
+ Ratis.configure do |config|
7
+ config.endpoint = 'http://soap.valleymetro.org/cgi-bin-soap-web-252/soap.cgi'
8
+ config.namespace = 'PX_WEB'
9
+ end
10
+ end
11
+
12
+ describe '#all' do
13
+
14
+ before do
15
+ @today = Chronic.parse('tomorrow at 8am') # Time.now.strftime("%m/%d/%Y")
16
+ @conditions = {:route_short_name => '0',
17
+ :direction => 'N',
18
+ :date => @today,
19
+ :service_type => 'W',
20
+ :longest => 'N'}
21
+ end
22
+
23
+ it 'only makes one request' do
24
+ # false just to stop further processing of response
25
+ Ratis::Request.should_receive(:get).once.and_call_original
26
+ Ratis::Pattern.all(@conditions.dup)
27
+ end
28
+
29
+ it 'should return no data error when date is in the past' do
30
+ old_date_conditions = @conditions.merge(:date => '01/28/2013')
31
+ expect do
32
+ Ratis::Pattern.all(old_date_conditions)
33
+ end.to raise_error(Ratis::Errors::SoapError, "error #1007--Record not found or no more data")
34
+ end
35
+
36
+ it 'requests the correct SOAP action with correct args' do
37
+ Ratis::Request.should_receive(:get) do |action, options|
38
+ action.should eq('Getpatterns')
39
+ options["Route"].should eq('0')
40
+ options["Direction"].should eq('N')
41
+ options["Date"].should eq(@today)
42
+ options["Servicetype"].should eq('W')
43
+ options["Longest"].should eq('N')
44
+
45
+ end.and_return(double('response', :success? => false))
46
+
47
+ Ratis::Pattern.all(@conditions.dup)
48
+ end
49
+
50
+ it 'should return a collection Pattern::RouteInfo(s)' do
51
+
52
+ pattern = Ratis::Pattern.all(@conditions.dup)
53
+ pattern.routeinfos.each do |obj|
54
+ expect(obj).to be_a(Ratis::Pattern::RouteInfo)
55
+ end
56
+
57
+ end
58
+
59
+ it "should parse the route info fields" do
60
+ pattern = Ratis::Pattern.all(@conditions.dup)
61
+ routeinfo = pattern.routeinfos.first
62
+
63
+ expect(routeinfo.operate).to eq('AP')
64
+ expect(routeinfo.routetype).to eq('B')
65
+ expect(routeinfo.headsign).to eq("0 CENTRAL North to Dunlap/3rd St.")
66
+ expect(routeinfo.routeid).to eq('83720')
67
+ expect(routeinfo.route).to eq('0')
68
+ expect(routeinfo.school).to be_nil
69
+ expect(routeinfo.effective).to eq('05/20/13')
70
+ expect(routeinfo.tripcount).to eq('51')
71
+
72
+ end
73
+
74
+ it "should raise error for missing arg route_short_name" do
75
+ conditions = @conditions.dup
76
+ conditions.delete(:route_short_name)
77
+
78
+ expect do
79
+ Ratis::Pattern.all(conditions)
80
+ end.to raise_error(ArgumentError, 'You must provide a route_short_name')
81
+ end
82
+
83
+ it "should raise error for missing arg direction" do
84
+ conditions = @conditions.dup
85
+ conditions.delete(:direction)
86
+
87
+ expect do
88
+ Ratis::Pattern.all(conditions)
89
+ end.to raise_error(ArgumentError, 'You must provide a direction')
90
+ end
91
+
92
+ it "should raise error for missing arg date" do
93
+ conditions = @conditions.dup
94
+ conditions.delete(:date)
95
+
96
+ expect do
97
+ Ratis::Pattern.all(conditions)
98
+ end.to raise_error(ArgumentError, 'You must provide a date')
99
+ end
100
+
101
+ it "should raise error for missing arg service_type" do
102
+ conditions = @conditions.dup
103
+ conditions.delete(:service_type)
104
+
105
+ expect do
106
+ Ratis::Pattern.all(conditions)
107
+ end.to raise_error(ArgumentError, 'You must provide a service_type')
108
+ end
109
+
110
+ it "should raise error for missing arg longest" do
111
+ conditions = @conditions.dup
112
+ conditions.delete(:longest)
113
+
114
+ expect do
115
+ Ratis::Pattern.all(conditions)
116
+ end.to raise_error(ArgumentError, 'You must provide a longest')
117
+ end
118
+
119
+ end
120
+
121
+ end
122
+
@@ -0,0 +1,223 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ratis::Point2Point do
4
+ before do
5
+ Ratis.reset
6
+ Ratis.configure do |config|
7
+ config.endpoint = 'http://soap.valleymetro.org/cgi-bin-soap-web-252/soap.cgi'
8
+ config.namespace = 'PX_WEB'
9
+ end
10
+ end
11
+
12
+ describe 'Routesonly => Y' do
13
+ describe '#where' do
14
+ describe 'services from origin to destination', {:vcr => {:cassette_name => "Point2Point"}} do
15
+ before do
16
+ @today = Time.now.strftime("%m/%d/%Y")
17
+ @conditions = {:routes_only => true,
18
+ :origin_lat => 33.446931,
19
+ :origin_long => -112.097903,
20
+ :destination_lat => 33.447098,
21
+ :destination_long => -112.077213,
22
+ :date => @today,
23
+ :start_time => '1700',
24
+ :end_time => '1800'}
25
+
26
+ end
27
+
28
+ it 'returns all matching services that fit the origin/destination for a given time frame' do
29
+ services = Ratis::Point2Point.where(@conditions.dup)
30
+ services.should have(20).items
31
+ end
32
+
33
+ it 'only makes one request' do
34
+ # false just to stop further processing of response
35
+ Ratis::Request.should_receive(:get).once.and_call_original
36
+ Ratis::Point2Point.where(@conditions.dup)
37
+ end
38
+
39
+ it 'requests the correct SOAP action' do
40
+ Ratis::Request.should_receive(:get) do |action, options|
41
+ action.should eq('Point2point')
42
+ options["Date"].should eq(@today)
43
+ options["Destinationlong"].should eq(-112.077213)
44
+ options["Starttime"].should eq("1700")
45
+ options["Endtime"].should eq("1800")
46
+ options["Destinationlat"].should eq(33.447098)
47
+ options["Routesonly"].should eq("Y")
48
+ options["Originlong"].should eq(-112.097903)
49
+ options["Originlat"].should eq(33.446931)
50
+
51
+ end.and_return(double('response', :success? => false)) # false only to stop further running
52
+
53
+ Ratis::Point2Point.where(@conditions.dup)
54
+ end
55
+
56
+ it 'returns a routes only response for each matched service' do
57
+ services = Ratis::Point2Point.where(@conditions.dup)
58
+ services.should be_a( Array )
59
+ services.each{|response| response.should be_a( Ratis::Point2Point::RoutesOnlyResponse ) }
60
+ end
61
+
62
+ it 'parses out service fields' do
63
+ services = Ratis::Point2Point.where(@conditions.dup)
64
+ service = services.first
65
+
66
+ service.route.should eql '562'
67
+ service.direction.should eql 'O'
68
+ service.service_type.should eql 'W'
69
+ service.signage.should eql "562 Goodyear To Goodyear"
70
+ service.route_type.should eql 'X'
71
+ end
72
+
73
+ it "should NOT filter by passed in routes" do
74
+ # According to Marc Ferguson of Trapeze group, route filter only works when Routesonly = 'N'
75
+ services = Ratis::Point2Point.where(@conditions.dup.merge(:routes => ['1']))
76
+ services.should have(20).items
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ describe 'Routesonly => N' do
83
+
84
+ describe '#where' do
85
+
86
+ before do
87
+ @today = Time.now.strftime("%m/%d/%Y")
88
+ @conditions = {:routes_only => false,
89
+ :origin_lat => 33.446931,
90
+ :origin_long => -112.097903,
91
+ :destination_lat => 33.447098,
92
+ :destination_long => -112.077213,
93
+ :date => @today,
94
+ :start_time => '1700',
95
+ :end_time => '1800'}
96
+
97
+ end
98
+
99
+ it 'only makes one request' do
100
+ Ratis::Request.should_receive(:get).once.and_call_original
101
+ Ratis::Point2Point.where(@conditions.dup)
102
+ end
103
+
104
+ it 'requests the correct SOAP action with correct args' do
105
+ Ratis::Request.should_receive(:get) do |action, options|
106
+ action.should eq('Point2point')
107
+ options["Date"].should eq(@today)
108
+ options["Destinationlong"].should eq(-112.077213)
109
+ options["Starttime"].should eq("1700")
110
+ options["Endtime"].should eq("1800")
111
+ options["Destinationlat"].should eq(33.447098)
112
+ options["Routesonly"].should eq("N")
113
+ options["Originlong"].should eq(-112.097903)
114
+ options["Originlat"].should eq(33.446931)
115
+
116
+ end.and_return(double('response', :success? => false)) # false only to stop further running
117
+
118
+ Ratis::Point2Point.where(@conditions.dup)
119
+ end
120
+
121
+ it 'gets the groups' do
122
+ schedule = Ratis::Point2Point.where(@conditions.dup)
123
+ schedule.should have(3).groups
124
+ schedule.groups.each{|group| group.should be_a(Ratis::Point2Point::Group) }
125
+ end
126
+
127
+ it 'gets the trips within each group' do
128
+ schedule = Ratis::Point2Point.where(@conditions.dup)
129
+ schedule.groups[0].should have(18).trips
130
+ schedule.groups[1].should have(16).trips
131
+ schedule.groups[2].should have(4).trips
132
+ end
133
+
134
+ it 'parses out the on stop fields' do
135
+ schedule = Ratis::Point2Point.where(@conditions.dup)
136
+ group = schedule.groups.first
137
+ on_stop = group.on_stop
138
+
139
+ on_stop.should be_a(Ratis::Point2Point::Stop)
140
+
141
+ on_stop.description.should eql('JEFFERSON ST & 18TH AVE')
142
+ on_stop.latitude.should eql(33.446931)
143
+ on_stop.longitude.should eql(-112.097903)
144
+ on_stop.atis_stop_id.should eql(9469)
145
+ on_stop.walk_dist.should eql(0.01)
146
+ on_stop.walk_dir.should eql('E')
147
+ on_stop.walk_hint.should eql('N')
148
+ end
149
+
150
+ it 'parses out the off stop fields' do
151
+ schedule = Ratis::Point2Point.where(@conditions.dup)
152
+ group = schedule.groups.first
153
+ off_stop = group.off_stop
154
+
155
+ off_stop.should be_a(Ratis::Point2Point::Stop)
156
+
157
+ off_stop.description.should eql('JEFFERSON ST & 3RD AVE')
158
+ off_stop.latitude.should eql(33.447098)
159
+ off_stop.longitude.should eql(-112.077213)
160
+ off_stop.atis_stop_id.should eql(1463)
161
+ off_stop.walk_dist.should eql(0.007)
162
+ off_stop.walk_dir.should eql('E')
163
+ off_stop.walk_hint.should eql('N')
164
+ end
165
+
166
+ it 'parses out the trip' do
167
+ schedule = Ratis::Point2Point.where(@conditions.dup)
168
+ group = schedule.groups.first
169
+ trip = group.trips.first
170
+
171
+ trip.should be_a(Ratis::Point2Point::Trip)
172
+
173
+ trip.on_time.should eql('05:00 PM')
174
+ trip.off_time.should eql('05:06 PM')
175
+ end
176
+
177
+ it 'parses out the service' do
178
+ schedule = Ratis::Point2Point.where(@conditions.dup)
179
+ group = schedule.groups.first
180
+ service = group.trips.first.service
181
+
182
+ service.should be_a(Ratis::Point2Point::Service)
183
+
184
+ service.route.should eql('451')
185
+ service.direction.should eql('S')
186
+ service.service_type.should eql('W')
187
+ service.signage.should eql('CSM RAPID To 27th Av/Bsln PNR')
188
+ service.route_type.should eql('X')
189
+ service.exception.should eql('N')
190
+ end
191
+
192
+ it "should only return result groups for filtered route" do
193
+ schedule = Ratis::Point2Point.where(@conditions.dup.merge(:routes => ['451']))
194
+ schedule.groups.size.should eq(1)
195
+ end
196
+
197
+ it "should only return result groups for filtered routes" do
198
+ schedule = Ratis::Point2Point.where(@conditions.dup.merge(:routes => ['1', '451']))
199
+ schedule.groups.size.should eq(2)
200
+ end
201
+
202
+ it "should raise error if the trip is NOT possible for a route being attempted to filter on" do
203
+ # According to Marc Ferguson of Trapeze group, route filter only works when Routesonly = 'N'
204
+ lambda {
205
+ Ratis::Point2Point.where(@conditions.dup.merge(:routes => ['GAL']))
206
+ }.should raise_error(Ratis::Errors::SoapError)
207
+ end
208
+
209
+ it "should raise error if the trip is NOT possible for a route being attempted to filter on" do
210
+ # According to Marc Ferguson of Trapeze group, route filter only works when Routesonly = 'N'
211
+ begin
212
+ Ratis::Point2Point.where(@conditions.dup.merge(:routes => ['GAL']))
213
+ rescue Exception => e
214
+ e.message.should eq('Trip not possible')
215
+ end
216
+ end
217
+
218
+
219
+ end
220
+ end
221
+
222
+ end
223
+