vworkapp_ruby 0.1.0 → 0.7.0

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.
Files changed (37) hide show
  1. data/Gemfile.lock +1 -1
  2. data/README.md +79 -12
  3. data/examples/example_1.rb +32 -0
  4. data/examples/example_2.rb +23 -0
  5. data/examples/example_3/Gemfile +5 -0
  6. data/examples/example_3/Gemfile.lock +35 -0
  7. data/examples/example_3/README.md +26 -0
  8. data/examples/example_3/public/style.css +25 -0
  9. data/examples/example_3/public/truck.png +0 -0
  10. data/examples/example_3/server.rb +41 -0
  11. data/examples/example_3/views/index.haml +47 -0
  12. data/lib/vworkapp_ruby.rb +13 -3
  13. data/lib/vworkapp_ruby/base/base.rb +173 -0
  14. data/lib/vworkapp_ruby/{httparty_monkey_patch.rb → base/httparty_monkey_patch.rb} +2 -4
  15. data/lib/vworkapp_ruby/base/resource.rb +105 -0
  16. data/lib/vworkapp_ruby/base/response_error.rb +26 -0
  17. data/lib/vworkapp_ruby/contact.rb +11 -0
  18. data/lib/vworkapp_ruby/custom_field.rb +11 -0
  19. data/lib/vworkapp_ruby/customer.rb +9 -97
  20. data/lib/vworkapp_ruby/job.rb +29 -146
  21. data/lib/vworkapp_ruby/location.rb +6 -23
  22. data/lib/vworkapp_ruby/step.rb +11 -0
  23. data/lib/vworkapp_ruby/telemetry.rb +6 -0
  24. data/lib/vworkapp_ruby/worker.rb +4 -81
  25. data/spec/base_spec.rb +174 -0
  26. data/spec/customer_spec.rb +30 -46
  27. data/spec/job_spec.rb +189 -143
  28. data/spec/resource_spec.rb +37 -0
  29. data/spec/support/active_model_lint.rb +19 -0
  30. data/spec/worker_spec.rb +6 -25
  31. data/vworkapp_ruby-0.1.0.gem +0 -0
  32. data/vworkapp_ruby.gemspec +5 -4
  33. metadata +46 -16
  34. data/examples/create_job.rb +0 -34
  35. data/examples/print_jobs.rb +0 -31
  36. data/lib/vworkapp_ruby/base.rb +0 -72
  37. data/lib/vworkapp_ruby/response_error.rb +0 -35
data/spec/job_spec.rb CHANGED
@@ -3,132 +3,120 @@ require "vworkapp_ruby"
3
3
  describe VW::Job do
4
4
 
5
5
  before(:all) do
6
- VW::Job.base_uri 'api.staging.vworkapp.com/api/2.0'
6
+ VW::Job.base_uri 'https://api.staging.vworkapp.com/api/2.0'
7
+ VW::Worker.base_uri 'https://api.staging.vworkapp.com/api/2.0'
8
+ VW::Customer.base_uri 'https://api.staging.vworkapp.com/api/2.0'
7
9
  VW.api_key = "AtuogECLCV2R7uT-fkPg"
8
10
  end
9
11
 
10
12
  context "Without a known job" do
11
13
 
12
- describe "#Equals" do
13
-
14
- it "Is equal to another job if it has the same attributes, steps, and custom_fields" do
15
- job_1 = VW::Job.new("Joe", "Std Delivery", 60,
16
- [
17
- VW::Step.new("Start", VWorkApp::Location.new("880 Harrison St", 37.779536, -122.401503)),
18
- VW::Step.new("End", VWorkApp::Location.new("Other Street", 38.779536, -123.401503))
19
- ],
20
- [
21
- VW::CustomField.new("Note", "Hi There!")
22
- ],
23
- 100, 202, 101, Date.new(2001,2,3)
24
- )
25
- job_2 = VW::Job.new("Joe", "Std Delivery", 60,
26
- [
27
- VW::Step.new("Start", VWorkApp::Location.new("880 Harrison St!", 37.779536, -122.401503)),
28
- VW::Step.new("End", VWorkApp::Location.new("Other Street", 38.779536, -123.401503))
29
- ],
30
- [
31
- VW::CustomField.new("Note", "Hi There!")
32
- ],
33
- 100, 202, 101, Date.new(2001,2,3)
34
- )
35
- job_1.should == job_2
36
- end
37
-
38
- it "Isn't equal to another job if doesn't share the key attributes" do
39
- job_1 = VW::Job.new("Joe", "Std Delivery", 60, nil, nil, 100, 202, 101, Date.new(2001,2,3))
40
- job_2 = VW::Job.new("Joe", "Std Delivery", 60, nil, nil, 100, 202, 101, Date.new(2001,4,3))
41
- job_1.should_not == job_2
42
- end
43
-
44
- it "Isn't equal to another job if doesn't share the same steps" do
45
- job_1 = VW::Job.new("Joe", "Std Delivery", 60, [
46
- VW::Step.new("Start", VWorkApp::Location.new("880 Harrison St", 37.779536, -122.401503)),
47
- VW::Step.new("End", VWorkApp::Location.new("Other Street", 38.779536, -122.401503))
48
- ])
49
- job_2 = VW::Job.new("Joe", "Std Delivery", 60, [
50
- VW::Step.new("Start", VWorkApp::Location.new("880 Harrison St", 37.779536, -122.401503)),
51
- VW::Step.new("End", VWorkApp::Location.new("DIFFERENT", 38.779536, 100))
52
- ])
53
- job_1.should_not == job_2
54
- end
55
-
56
- it "Isn't equal to another job if doesn't share the same custom fields" do
57
- job_1 = VW::Job.new("Joe", "Std Delivery", 60, nil, [
58
- VW::CustomField.new("Note1", "Value1"),
59
- VW::CustomField.new("Note2", "Value2"),
60
- ])
61
- job_2 = VW::Job.new("Joe", "Std Delivery", 60, nil, [
62
- VW::CustomField.new("Note1", "Value1"),
63
- VW::CustomField.new("Note2", "DIFFERENT"),
64
- ])
65
- job_1.should_not == job_2
66
- end
67
-
68
- end
69
-
70
14
  describe "#Create" do
71
15
 
72
16
  it "Assigns the new job an id" do
73
- @job = VW::Job.new("Joe", "Std Delivery", 60,
74
- [
75
- VW::Step.new("Start", VWorkApp::Location.new("880 Harrison St", 37.779536, -122.401503)),
76
- VW::Step.new("End", VWorkApp::Location.new("Other Street", 38.779536, -123.401503))
77
- ],
78
- [
79
- VW::CustomField.new("Note", "Hi There!")
17
+ @job = VW::Job.new(:customer_name => "Joe", :template_name => "Std Delivery", :planned_duration => 60,
18
+ :steps => [
19
+ {:name => "Start", :location => {:formatted_address => "880 Harrison St", :lat => 37.779536, :lng => -122.401503}},
20
+ {:name => "End", :location => {:formatted_address => "Other Street", :lat => 38.779536, :lng => -123.401503}},
80
21
  ],
81
- 100, 202, 101, Date.new(2001,2,3)
82
- )
83
-
22
+ :custom_fields => [
23
+ {:name => "Note", :value => "Hi There!"}
24
+ ])
25
+
84
26
  @job = @job.create
85
27
  @job.id.should_not be_nil
86
28
  end
87
29
 
88
30
  it "Creates an unassigned job" do
89
- @job = VW::Job.new("Joe", "Std Delivery", 60,
90
- [
91
- VW::Step.new("Start", VWorkApp::Location.new("880 Harrison St", 37.779536, -122.401503)),
92
- VW::Step.new("End", VWorkApp::Location.new("Other Street", 38.779536, -123.401503))
93
- ])
31
+ @job = VW::Job.new(:customer_name => "Joe", :template_name => "Std Delivery", :planned_duration => 60,
32
+ :steps => [
33
+ {:name => "Start", :location => {:formatted_address => "880 Harrison St", :lat => 37.779536, :lng => -122.401503}},
34
+ {:name => "End", :location => {:formatted_address => "Other Street", :lat => 38.779536, :lng => -123.401503}},
35
+ ])
36
+
94
37
  @job = @job.create
95
38
  r_job = VW::Job.show(@job.id)
96
39
  r_job.customer_name.should == @job.customer_name
97
40
  r_job.template_name.should == @job.template_name
98
41
  r_job.planned_duration.should == @job.planned_duration
42
+ r_job.state.should == "unallocated"
43
+ r_job.progress_state.should == "not_started"
99
44
  r_job.steps.should == @job.steps
100
45
  end
101
46
 
102
47
  it "Creates an unassigned job with Custom Fields" do
103
- @job = VW::Job.new("Joe", "Std Delivery", 60,
104
- [
105
- VW::Step.new("Start", VWorkApp::Location.new("880 Harrison St", 37.779536, -122.401503)),
106
- VW::Step.new("End", VWorkApp::Location.new("Other Street", 38.779536, -123.401503))
48
+ @job = VW::Job.new(:customer_name => "Joe", :template_name => "Std Delivery", :planned_duration => 60,
49
+ :steps => [
50
+ {:name => "Start", :location => {:formatted_address => "880 Harrison St", :lat => 37.779536, :lng => -122.401503}},
51
+ {:name => "End", :location => {:formatted_address => "Other Street", :lat => 38.779536, :lng => -123.401503}},
107
52
  ],
108
- [
109
- VW::CustomField.new("Note1", "Value1"),
110
- VW::CustomField.new("Note2", "Value2"),
111
- ]
112
- )
53
+ :custom_fields => [
54
+ {:name => "Note1", :value => "Value1"},
55
+ {:name => "Note2", :value => "Value2"}
56
+ ])
57
+
113
58
  @job = @job.create
114
59
  r_job = VW::Job.show(@job.id)
115
60
  r_job.custom_fields.should == @job.custom_fields
116
61
  end
117
62
 
118
63
  it "Creates and assigns a job" do
119
- @worker = VW::Worker.new("Joe", "joe@example.com")
64
+ @worker = VW::Worker.new(:name => "Joe", :email => "joe@example.com")
120
65
  @worker = @worker.create
66
+
67
+ planned_start_at = Time.parse("2012-12-25 16:30-8")
121
68
 
122
- @job = VW::Job.new("Joe", "Std Delivery", 60,
123
- [
124
- VW::Step.new("Start", VWorkApp::Location.new("880 Harrison St", 37.779536, -122.401503)),
125
- VW::Step.new("End", VWorkApp::Location.new("Other Street", 38.779536, -123.401503))
126
- ], nil, nil, @worker.id, DateTime.parse("2012-12-25 16:30"))
69
+ @job = VW::Job.new(:customer_name => "Joe", :template_name => "Std Delivery", :planned_duration => 60,
70
+ :worker_id => @worker.id, :planned_start_at => planned_start_at,
71
+ :steps => [
72
+ {:name => "Start", :location => {:formatted_address => "880 Harrison St", :lat => 37.779536, :lng => -122.401503}},
73
+ {:name => "End", :location => {:formatted_address => "Other Street", :lat => 38.779536, :lng => -123.401503}},
74
+ ])
127
75
  @job = @job.create
76
+
128
77
  r_job = VW::Job.show(@job.id)
78
+ r_job.worker_id.should == @worker.id
79
+ r_job.state.should == "assigned"
80
+ r_job.planned_start_at.should == planned_start_at
81
+ end
129
82
 
83
+ it "Creates a job with completed steps" do
84
+ pending("Broken until https://github.com/visfleet/vworkapp_ruby/issues/1 is resolved")
85
+ @worker = VW::Worker.new(:name => "Joe", :email => "joe@example.com")
86
+ @worker = @worker.create
87
+
88
+ planned_start_at = Time.parse("2012-12-25 16:30-8")
89
+ actual_start_at = Time.at(Time.now.to_i)
90
+
91
+ @job = VW::Job.new(:customer_name => "Joe", :template_name => "Std Delivery", :planned_duration => 60,
92
+ :worker_id => @worker.id, :planned_start_at => planned_start_at,
93
+ :steps => [
94
+ {:name => "Start", :location => {:formatted_address => "880 Harrison St", :lat => 37.779536, :lng => -122.401503}, :completed_at => actual_start_at},
95
+ {:name => "End", :location => {:formatted_address => "Other Street", :lat => 38.779536, :lng => -123.401503}},
96
+ ])
97
+ @job = @job.create
98
+
99
+ r_job = VW::Job.show(@job.id)
130
100
  r_job.worker_id.should == @worker.id
131
- r_job.planned_start_time.should == DateTime.parse("2012-12-25 16:30")
101
+ r_job.progress_state.should == "started"
102
+ r_job.actual_start_at == actual_start_at
103
+ r_job.planned_start_at.should == planned_start_at
104
+ end
105
+
106
+ it "Has should raise an exception if required fields are missing" do
107
+ @job = VW::Job.new
108
+ lambda { @job.create }.should raise_error
109
+ @job.errors.keys.should == [:template_name, :planned_duration, :steps, :customer_name, :customer_id]
110
+ end
111
+
112
+ it "Has errors if the server return an error" do
113
+ @job = VW::Job.new(:customer_name => "Joe", :template_name => "Std Delivery", :planned_duration => 60, :worker_id => -1,
114
+ :steps => [
115
+ {:name => "Start", :location => {:formatted_address => "880 Harrison St", :lat => 37.779536, :lng => -122.401503}},
116
+ {:name => "End", :location => {:formatted_address => "Other Street", :lat => 38.779536, :lng => -123.401503}},
117
+ ])
118
+
119
+ lambda { @job.create }.should raise_error(VW::ResponseError)
132
120
  end
133
121
 
134
122
  end
@@ -140,60 +128,118 @@ describe VW::Job do
140
128
 
141
129
  end
142
130
 
143
- # context "With a known job" do
144
- # before(:each) do
145
- # @worker = VW::Worker.new("Joe", "joe@example.com")
146
- # @worker = @worker.create
147
- # end
148
- #
149
- # after(:each) do
150
- # @worker.delete
151
- # end
152
- #
153
- # describe "#show" do
154
- # it "Returns the worker" do
155
- # r_worker = VW::Worker.show(@worker.id)
156
- # r_worker.name.should == "Joe"
157
- # r_worker.email.should == "joe@example.com"
158
- # end
159
- #
160
- # it "Returns nil if not found" do
161
- # pending("Bug in vWorkApp. Should be returning a 404 but isn't")
162
- # r_worker = VW::Worker.show(-1)
163
- # r_worker.should be_nil
164
- # end
165
- # end
166
- #
167
- # describe "#update" do
168
- # it "Updates the worker" do
169
- # @worker.name = "Joe2"
170
- # @worker.email = "joe2@example.com"
171
- # @worker.update
172
- # r_worker = VW::Worker.show(@worker.id)
173
- # r_worker.name.should == "Joe2"
174
- # r_worker.email.should == "joe2@example.com"
175
- # end
176
- # end
177
- #
178
- # describe "#delete" do
179
- # it "Deletes the worker" do
180
- # @worker.delete
181
- # # XXX Can't verify until #show is fixed
182
- # # r_worker = VW::Worker.show(@worker.id)
183
- # # r_worker.should be_nil
184
- # end
185
- # end
186
- #
187
- # describe "#find" do
188
- # it "Finds all workers" do
189
- # results = VW::Worker.find
190
- # results.should be_instance_of(Array)
191
- # results.should include(@worker)
192
- # end
193
- # end
194
- #
195
- # end
131
+ context "With a known job" do
132
+ before(:each) do
133
+ @job = VW::Job.new(:customer_name => "Joe", :template_name => "Std Delivery", :planned_duration => 60, :third_party_id => "my_id",
134
+ :steps => [
135
+ {:name => "Start", :location => {:formatted_address => "880 Harrison St", :lat => 37.779536, :lng => -122.401503}},
136
+ {:name => "End", :location => {:formatted_address => "Other Street", :lat => 38.779536, :lng => -123.401503}},
137
+ ],
138
+ :custom_fields => [
139
+ {:name => "Note", :value => "Hi There!"}
140
+ ])
141
+ @job = @job.create
142
+ end
143
+
144
+ after(:each) do
145
+ @job.delete if @job
146
+ @worker.delete if @worker
147
+ end
148
+
149
+ describe "#Show" do
150
+ it "Returns the job" do
151
+ r_job = VW::Job.show(@job.id)
152
+ r_job.customer_name.should == "Joe"
153
+ r_job.template_name.should == "Std Delivery"
154
+ r_job.state == "unallocated"
155
+ r_job.progress_state == "not_started"
156
+ end
157
+
158
+ it "Returns nil if not found" do
159
+ r_job = VW::Job.show(-1)
160
+ r_job.should be_nil
161
+ end
162
+ end
163
+
164
+ describe "#Update" do
165
+
166
+ it "Updates the standard attributes" do
167
+ @job.customer_name = "Joe2"
168
+ @job.template_name == "Not Standard"
169
+ @job.planned_duration == 64
170
+ @job.update
171
+
172
+ r_job = VW::Job.show(@job.id)
173
+ r_job.customer_name.should == @job.customer_name
174
+ r_job.template_name.should == @job.template_name
175
+ r_job.planned_duration.should == @job.planned_duration
176
+ end
177
+
178
+ it "Assigns a job" do
179
+ @worker = VW::Worker.new(:name => "Joe", :email => "joe@example.com")
180
+ @worker = @worker.create
181
+
182
+ start_at = Time.at(Time.now.to_i)
183
+
184
+ @job.planned_start_at = start_at
185
+ @job.worker_id = @worker.id
186
+ @job.update
187
+
188
+ r_job = VW::Job.show(@job.id)
189
+ r_job.worker_id.should == @worker.id
190
+ r_job.planned_start_at.should == start_at
191
+ r_job.published_at.should == start_at
192
+ r_job.state.should == "assigned"
193
+ end
194
+
195
+ it "Starts a job" do
196
+ pending("Broken until https://github.com/visfleet/vworkapp_ruby/issues/1 is resolved")
197
+ end
198
+
199
+ it "Completes a job" do
200
+ pending("Broken until https://github.com/visfleet/vworkapp_ruby/issues/1 is resolved")
201
+ end
202
+
203
+ end
204
+
205
+ describe "#Delete" do
206
+ it "Deletes the job" do
207
+ @job.delete
208
+ r_job = VW::Job.show(@job.id)
209
+ r_job.should be_nil
210
+ end
211
+ end
212
+
213
+ describe "#Find" do
214
+ it "Finds all jobs" do
215
+ results = VW::Job.find
216
+ results.should be_instance_of(Array)
217
+ results.should include(@job)
218
+ end
219
+ end
220
+
221
+ describe "#Assocation Connivence Methods" do
196
222
 
223
+ it "Loads the job's customer" do
224
+ r_customer = VW::Job.show(@job.id).customer
225
+ r_customer.should be_instance_of VW::Customer
226
+ r_customer.name.should == "Joe"
227
+ end
228
+
229
+ it "Loads the job's worker" do
230
+ @worker = VW::Worker.new(:name => "Joe", :email => "joe@example.com")
231
+ @worker = @worker.create
232
+ @job.planned_start_at = Time.now
233
+ @job.worker_id = @worker.id
234
+ @job.update
235
+
236
+ r_job = VW::Job.show(@job.id)
237
+ r_job.worker.should == @worker
238
+ end
239
+
240
+ end
241
+
242
+ end
197
243
 
198
244
 
199
245
  end
@@ -0,0 +1,37 @@
1
+ require "vworkapp_ruby"
2
+ require 'support/active_model_lint'
3
+
4
+ describe VW::Resource do
5
+
6
+ before(:all) do
7
+ end
8
+
9
+ subject { t = R1.new(
10
+ :a => "a", :b => "b",
11
+ :c => {
12
+ :one => "one",
13
+ :two => "two"
14
+ })
15
+ }
16
+
17
+ it_should_behave_like "ActiveModel"
18
+
19
+ end
20
+
21
+ class R2 < VW::Resource
22
+ hattr_accessor :one, :two
23
+ self.include_root_in_json = false
24
+
25
+ def ==(other)
26
+ attributes_eql?(other, :one, :two)
27
+ end
28
+ end
29
+
30
+ class R1 < VW::Resource
31
+ hattr_accessor :a, :b, {:c => R2}, {:es => Array(R2)}
32
+ hattr_reader :d
33
+
34
+ def ==(other)
35
+ attributes_eql?(other, :a, :b, :c, :es)
36
+ end
37
+ end
@@ -0,0 +1,19 @@
1
+ require 'test/unit/assertions'
2
+ require 'active_model/lint'
3
+
4
+ shared_examples_for "ActiveModel" do
5
+ include Test::Unit::Assertions
6
+ include ActiveModel::Lint::Tests
7
+
8
+ # to_s is to support ruby-1.9
9
+ ActiveModel::Lint::Tests.public_instance_methods.map{|m| m.to_s}.grep(/^test/).each do |m|
10
+ example m.gsub('_',' ') do
11
+ send m
12
+ end
13
+ end
14
+
15
+ def model
16
+ subject
17
+ end
18
+
19
+ end
data/spec/worker_spec.rb CHANGED
@@ -3,38 +3,21 @@ require "vworkapp_ruby"
3
3
  describe VW::Worker do
4
4
 
5
5
  before(:all) do
6
- VW::Worker.base_uri 'api.staging.vworkapp.com/api/2.0'
6
+ VW::Worker.base_uri 'https://api.staging.vworkapp.com/api/2.0'
7
7
  VW.api_key = "AtuogECLCV2R7uT-fkPg"
8
8
  end
9
9
 
10
10
  context "Without a known worker" do
11
11
 
12
- describe "#Equal" do
13
-
14
- it "Is equal to another worker if it has the same key attributes" do
15
- worker_1 = VW::Worker.new("Joe", "joe@example.com", 100, 200)
16
- worker_2 = VW::Worker.new("Joe", "joe@example.com", 100, 200)
17
- worker_1.should == worker_2
18
- end
19
-
20
- it "Isn't equal to another worker if doesn't share the key attributes" do
21
- worker_1 = VW::Worker.new("Joe", "joe@example.com", 100, 200)
22
- worker_2 = VW::Worker.new("Joe", "joe@example.com", 100, 202)
23
- worker_1.should_not == worker_2
24
- end
25
-
26
- end
27
-
28
- describe "#Create" do
29
-
12
+ describe "#Create" do
30
13
  it "Assigns the new worker an id" do
31
- @worker = VW::Worker.new("Joe", "joe@example.com")
14
+ @worker = VW::Worker.new(:name => "Joe", :email => "joe@example.com")
32
15
  @worker = @worker.create
33
16
  @worker.id.should_not be_nil
34
17
  end
35
18
 
36
19
  it "Creates a worker" do
37
- @worker = VW::Worker.new("Joe", "joe@example.com")
20
+ @worker = VW::Worker.new(:name => "Joe", :email => "joe@example.com")
38
21
  @worker = @worker.create
39
22
  r_worker = VW::Worker.show(@worker.id)
40
23
  r_worker.name.should == @worker.name
@@ -51,7 +34,7 @@ describe VW::Worker do
51
34
 
52
35
  context "With a known worker" do
53
36
  before(:each) do
54
- @worker = VW::Worker.new("Joe", "joe@example.com")
37
+ @worker = VW::Worker.new(:name => "Joe", :email => "joe@example.com")
55
38
  @worker = @worker.create
56
39
  end
57
40
 
@@ -95,7 +78,7 @@ describe VW::Worker do
95
78
 
96
79
  describe "#Find" do
97
80
  it "Finds all workers" do
98
- results = VW::Worker.find
81
+ results = VW::Worker.find()
99
82
  results.should be_instance_of(Array)
100
83
  results.should include(@worker)
101
84
  end
@@ -103,6 +86,4 @@ describe VW::Worker do
103
86
 
104
87
  end
105
88
 
106
-
107
-
108
89
  end