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
@@ -1,34 +1,18 @@
1
1
  module VWorkApp
2
-
3
- class Location
4
- include AttributeEquality
5
- attr_accessor :formatted_address, :lat, :lng
6
-
7
- def initialize(formatted_address, lat, lng)
8
- @formatted_address = formatted_address
9
- @lat = lat
10
- @lng = lng
11
- end
2
+ class Location < Base
3
+ hattr_accessor :formatted_address, :lat, :lng
4
+ self.include_root_in_json = false
12
5
 
13
6
  def self.from_address(address, region = :us)
14
7
  loc = Location.geocode(address, region).first.geometry.location
15
- self.new(address, loc.lat, loc.lng)
16
- end
17
-
18
- def to_hash
19
- { :formatted_address => formatted_address, :lat => lat, :lng => lng }
20
- end
21
-
22
- def self.from_hash(attributes)
23
- return nil unless attributes
24
- Location.new(attributes["formatted_address"], attributes["lat"], attributes["lng"])
8
+ self.new(:formmated_address => address, :lat => loc.lat, :lng => loc.lng)
25
9
  end
26
10
 
27
11
  def ==(other)
28
- attributes_eql?(other, [:lat, :lng])
12
+ attributes_eql?(other, :lat, :lng)
29
13
  end
30
14
 
31
- private
15
+ private
32
16
 
33
17
  def self.geocode(address, region)
34
18
  @gecoder ||= GCoder.connect(:storage => :heap)
@@ -36,5 +20,4 @@ module VWorkApp
36
20
  end
37
21
 
38
22
  end
39
-
40
23
  end
@@ -0,0 +1,11 @@
1
+ module VWorkApp
2
+ class Step < Base
3
+ hattr_accessor :name, {:location => VWorkApp::Location}, :completed_at
4
+ self.include_root_in_json = false
5
+
6
+ def ==(other)
7
+ attributes_eql?(other, :name, :location, :completed_at)
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ module VWorkApp
2
+ class Telemetry < Base
3
+ hattr_accessor :lat, :lng, :recorded_at, :heading, :speed
4
+ self.include_root_in_json = false
5
+ end
6
+ end
@@ -1,88 +1,11 @@
1
1
  module VWorkApp
2
+ class Worker < Resource
3
+ hattr_accessor :id, :name, :email, :third_party_id
4
+ hattr_reader :latest_telemetry => VWorkApp::Telemetry
2
5
 
3
- class Worker < Base
4
- attr_accessor :id, :name, :email, :latest_telemetry, :third_party_id
5
-
6
- #---------------
7
- # Object Methods
8
- #---------------
9
-
10
- def initialize(name, email, id = nil, third_party_id = nil)
11
- @name = name
12
- @email = email
13
- @id = id
14
- @third_party_id = third_party_id
15
- end
16
-
17
- def self.from_hash(attributes)
18
- worker = Worker.new(attributes["name"], attributes["email"], attributes["id"], attributes["third_party_id"])
19
- worker.latest_telemetry = Telemetry.from_hash(attributes["latest_telemetry"]) if attributes["latest_telemetry"]
20
- worker
21
- end
22
-
23
- def to_hash
24
- worker_hash = {
25
- :worker => {
26
- :name => self.name,
27
- :email => self.email
28
- }
29
- }
30
- worker_hash[:worker][:third_party_id] = self.third_party_id if third_party_id
31
- worker_hash
32
- end
33
-
34
6
  def ==(other)
35
- attributes_eql?(other, [:id, :name, :email, :third_party_id])
36
- end
37
-
38
- #---------------
39
- # REST Methods
40
- #---------------
41
-
42
- def create
43
- perform(:post, "/workers", {}, self.to_hash) do |res|
44
- Worker.from_hash(res["worker"])
45
- end
46
- end
47
-
48
- def update(use_third_party_id = false)
49
- perform(:put, "/workers/#{id}.xml", { :use_third_party_id => use_third_party_id }, self.to_hash)
50
- end
51
-
52
- def delete(use_third_party_id = false)
53
- perform(:delete, "/workers/#{id}.xml", { :use_third_party_id => use_third_party_id })
54
- end
55
-
56
- def self.show(id, use_third_party_id = false)
57
- perform(:get, "/workers/#{id}.xml", :use_third_party_id => use_third_party_id) do |res|
58
- Worker.from_hash(res["worker"])
59
- end
60
- end
61
-
62
- def self.find
63
- self.perform(:get, "/workers.xml") do |res|
64
- res["workers"].map { |h| Worker.from_hash(h) }
65
- end
7
+ attributes_eql?(other, :id, :name, :email, :third_party_id)
66
8
  end
67
9
 
68
10
  end
69
-
70
- class Telemetry
71
- attr_accessor :lat, :lng, :recorded_at, :heading, :speed
72
-
73
- def initialize (lat, lng, recorded_at, heading, speed)
74
- @lat = lat
75
- @lng = lng
76
- @recorded_at = recorded_at
77
- @heading = heading
78
- @speed = speed
79
- end
80
-
81
- def self.from_hash(attributes)
82
- Telemetry.new(attributes["lat"], attributes["lng"], attributes["recorded_at"], attributes["heading"], attributes["speed"])
83
- end
84
-
85
- end
86
-
87
-
88
11
  end
data/spec/base_spec.rb ADDED
@@ -0,0 +1,174 @@
1
+ require "vworkapp_ruby"
2
+
3
+ describe VW::Base do
4
+
5
+ before(:all) do
6
+ end
7
+
8
+ describe "#New" do
9
+
10
+ it "Creates a base object from a given hash" do
11
+ t = B1.new(
12
+ :a => "a",
13
+ :b => "b",
14
+ :c => {
15
+ :one => "one",
16
+ :two => "two"
17
+ }
18
+ )
19
+ t.a.should == "a"
20
+ t.b.should == "b"
21
+ t.c.should be_instance_of B2
22
+ t.c.one.should == "one"
23
+ t.c.two.should == "two"
24
+ t.d.should be_nil
25
+ end
26
+
27
+ it "Doesn't create setters for readonly attributes" do
28
+ t = B1.new(:d => 'd')
29
+ t.d.should == "d"
30
+ lambda { t.d = 'ahem' }.should raise_error(NoMethodError)
31
+ end
32
+
33
+ it "Works with string keys too" do
34
+ t = B1.new(:a => "a", "b" => "b")
35
+ t.a.should == "a"
36
+ t.b.should == "b"
37
+ end
38
+
39
+ it "Populates readonly attributes too" do
40
+ t = B1.new(:d => "d")
41
+ t.d.should == "d"
42
+ end
43
+
44
+ it "Works with array's of nested items" do
45
+ t = B1.new(:es => [
46
+ {:one => "one", :two => "two"},
47
+ {:one => "three", :two => "four"}
48
+ ])
49
+ t.es.should be_instance_of Array
50
+ t.es.size.should == 2
51
+ t.es.first.should be_instance_of B2
52
+ t.es.first.one.should == "one"
53
+ end
54
+
55
+ end
56
+
57
+ describe "#Attributes" do
58
+
59
+ it "Retuns the resources's attributes" do
60
+ t = B1.new(:a => "a", :b => "b", :c => {:one => "one", :two => "two" })
61
+ t.attributes.should == {
62
+ "a" => "a",
63
+ "b" => "b",
64
+ "c" => B2.new(:one => "one", :two => "two"),
65
+ "d" => nil,
66
+ "es" => nil
67
+ }
68
+ end
69
+
70
+ it "Retuns the resources's readable attributes" do
71
+ t = B1.new(:a => "a", :b => "b", :d => 'd')
72
+ t.read_attributes.should == {"a" => "a", "b" => "b", "c" => nil, "es" => nil, "d" => "d"}
73
+ end
74
+
75
+ it "Retuns the resources's writeable attributes" do
76
+ t = B1.new(:a => "a", :b => "b", :d => 'd')
77
+ t.write_attributes.should == {"a" => "a", "b" => "b", "c" => nil, "es" => nil}
78
+ end
79
+
80
+ it "Overwritten hattr setters get called" do
81
+ t = B1.new
82
+ class << t
83
+ attr_reader :test
84
+ def a=(value)
85
+ @test = value
86
+ end
87
+ end
88
+ t.attributes = {:a => "a"}
89
+ t.test.should == "a"
90
+ end
91
+
92
+ end
93
+
94
+ describe "#Equals" do
95
+ it "Is == if key attributes match" do
96
+ t1 = B1.new(:a => "a", :b => "b", :es => [{:one => "one", :two => "two"}, {:one => "three", :two => "four"}])
97
+ t2 = B1.new(:a => "a", :b => "b", :es => [{:one => "one", :two => "two"}, {:one => "three", :two => "four"}])
98
+ t1.should == t2
99
+ end
100
+ end
101
+
102
+ describe "#Valdiations" do
103
+ it "Has error(s) if required fields are missing" do
104
+ @job = B1.new
105
+ @job.valid?.should be_false
106
+ @job.errors.keys.should == [:a]
107
+ end
108
+ end
109
+
110
+ describe "Serialization" do
111
+
112
+ # XXX fails intermittently because of hash ordering.
113
+ it "Serializes into json" do
114
+ t = B1.new(:a => "a", :b => "b", :es => [{:one => "one", :two => "two"}, {:one => "three", :two => "four"}])
115
+ t.to_json.should == '{"b1":{"a":"a","b":"b","c":null,"es":[{"two":"two","one":"one"},{"two":"four","one":"three"}]}}'
116
+ end
117
+
118
+ # XXX fails intermittently because of hash ordering.
119
+ it "Serializes into xml" do
120
+ t = B1.new(:a => "a", :b => "b", :es => [{:one => "one", :two => "two"}, {:one => "three", :two => "four"}], :d => nil)
121
+ t.to_xml.should == <<-eol
122
+ <?xml version="1.0" encoding="UTF-8"?>
123
+ <b1>
124
+ <a>a</a>
125
+ <b>b</b>
126
+ <es type="array">
127
+ <e>
128
+ <two>two</two>
129
+ <one>one</one>
130
+ </e>
131
+ <e>
132
+ <two>four</two>
133
+ <one>three</one>
134
+ </e>
135
+ </es>
136
+ <c nil="true"></c>
137
+ </b1>
138
+ eol
139
+ end
140
+
141
+ it "Serializes into xml but doesn't include readonly items" do
142
+ t = B1.new(:a => "a", :b => "b", :d => "d")
143
+ t.to_xml.should_not include "<d>d</d>"
144
+ end
145
+
146
+ it "Serializes into json but doesn't include readonly items" do
147
+ t = B1.new(:a => "a", :b => "b", :d => "d")
148
+ t.to_json.should_not include '"d":"d"'
149
+ end
150
+
151
+ end
152
+
153
+ end
154
+
155
+ class B2 < VW::Base
156
+ hattr_accessor :one, :two
157
+ self.include_root_in_json = false
158
+
159
+ def ==(other)
160
+ attributes_eql?(other, :one, :two)
161
+ end
162
+ end
163
+
164
+ class B1 < VW::Base
165
+ hattr_accessor :a, :b, {:c => B2}, {:es => Array(B2)}
166
+ hattr_reader :d
167
+
168
+ validates_presence_of :a
169
+
170
+ def ==(other)
171
+ attributes_eql?(other, :a, :b, :c, :es)
172
+ end
173
+
174
+ end
@@ -3,67 +3,51 @@ require "vworkapp_ruby"
3
3
  describe VW::Customer do
4
4
 
5
5
  before(:all) do
6
- VW::Customer.base_uri 'api.staging.vworkapp.com/api/2.0'
6
+ VW::Customer.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 customer" do
11
11
 
12
- describe "#Equals" do
13
-
14
- it "Is equal to another customer if it has the same key attributes" do
15
- customer_1 = VW::Customer.new("Joe's Baked Goods", 100, 200,
16
- VW::Contact.new("Joe", "Fisher", "415-465-8888", "415-465-9999", "joe@bakary.com"),
17
- VW::Contact.new("Bob", "Jones", "021-465-2342", "233-233-1242", "bob.jones@jones.com")
18
- )
19
- customer_2 = VW::Customer.new("Joe's Baked Goods", 100, 200,
20
- VW::Contact.new("Joe", "Fisher", "415-465-8888", "415-465-9999", "joe@bakary.com"),
21
- VW::Contact.new("Bob", "Jones", "021-465-2342", "233-233-1242", "bob.jones@jones.com")
22
- )
23
- customer_1.should == customer_2
24
- end
25
-
26
- it "Isn't equal to another worker if doesn't share the key attributes" do
27
- customer_1 = VW::Customer.new("Joe's Baked Goods", 100, 200,
28
- VW::Contact.new("Joe", "Fisher", "415-465-8888", "415-465-9999", "joe@bakary.com"),
29
- VW::Contact.new("Bob", "Jones", "021-465-2342", "233-233-1242", "bob.jones@jones.com")
30
- )
31
- customer_2 = VW::Customer.new("Joe's Baked Goods", 100, 200,
32
- VW::Contact.new("Joe", "Fisher", "415-465-8888", "415-465-9999", "joe@bakary.com"),
33
- VW::Contact.new("Jeff", "Jones", "021-465-2342", "233-233-1242", "bob.jones@jones.com")
34
- )
35
- customer_1.should_not == customer_2
36
- end
37
-
38
- end
39
-
40
12
  describe "#Create" do
41
13
 
42
14
  it "Assigns the new customer an id" do
43
- @customer = VW::Customer.new("Joe's Baked Goods")
15
+ @customer = VW::Customer.new(:name => "Joe's Baked Goods")
44
16
  @customer = @customer.create
45
17
  @customer.id.should_not be_nil
46
18
  end
47
19
 
48
20
  it "Creates a customer" do
49
- @customer = VW::Customer.new("Joe's Baked Goods", nil, "My ID")
21
+ @customer = VW::Customer.new(:name => "Joe's Baked Goods", :third_party_id => "My ID")
50
22
  @customer = @customer.create
51
-
23
+
52
24
  r_cust = VW::Customer.show(@customer.id)
53
25
  r_cust.name.should == @customer.name
54
26
  r_cust.third_party_id.should == @customer.third_party_id
55
27
  end
56
28
 
57
29
  it "Creates a customer with a billing and delivery contact" do
58
- site_contact = VW::Contact.new("Joe", "Fisher", "415-465-8888", "415-465-9999", "joe@bakary.com")
59
- billing_contact = VW::Contact.new("Felix", "Smith")
60
- @customer = VW::Customer.new("Joe's Baked Goods", nil, "My ID", site_contact, billing_contact)
30
+ @customer = VW::Customer.new(
31
+ :name => "Joe's Baked Goods",
32
+ :third_party_id => "My ID",
33
+ :delivery_contact => {
34
+ :first_name => "Joe",
35
+ :last_name => "Fisher",
36
+ :phone => "415-465-8888",
37
+ :mobile => "415-465-9999",
38
+ :email => "joe@bakary.com"
39
+ },
40
+ :billing_contact => {
41
+ :first_name => "Felix",
42
+ :last_name => "Smith"
43
+ }
44
+ )
61
45
  @customer = @customer.create
62
46
 
63
47
  r_cust = VW::Customer.show(@customer.id)
64
48
 
65
- r_cust.site_contact.should == site_contact
66
- r_cust.billing_contact.should == billing_contact
49
+ r_cust.delivery_contact.should == @customer.delivery_contact
50
+ r_cust.billing_contact.should == @customer.billing_contact
67
51
  end
68
52
 
69
53
  it "Creates a customer with a contact location" do
@@ -73,7 +57,7 @@ describe VW::Customer do
73
57
  @customer = @customer.create
74
58
 
75
59
  r_cust = VW::Customer.show(@customer.id)
76
- r_cust.site_contact.location.should == site
60
+ r_cust.delivery_contact.location.should == site
77
61
  end
78
62
 
79
63
  end
@@ -86,7 +70,7 @@ describe VW::Customer do
86
70
 
87
71
  context "With a known customer" do
88
72
  before(:each) do
89
- @customer = VW::Customer.new("Joe's Baked Goods")
73
+ @customer = VW::Customer.new(:name => "Joe's Baked Goods")
90
74
  @customer = @customer.create
91
75
  end
92
76
 
@@ -117,20 +101,20 @@ describe VW::Customer do
117
101
  r_cust.third_party_id.should == "My Other ID"
118
102
  end
119
103
 
120
- it "Updates a customers site and billing contacts" do
121
- site_contact = VW::Contact.new("Joe", "Fisher", "415-465-8888", "415-465-9999", "joe@bakary.com")
122
- billing_contact = VW::Contact.new("Felix", "Smith")
123
- @customer.site_contact = site_contact
104
+ it "Updates a customers delivery and billing contacts" do
105
+ delivery_contact = VW::Contact.new(:first_name => "Joe", :last_name => "Fisher", :phone => "415-465-8888", :mobile => "415-465-9999", :email => "joe@bakary.com")
106
+ billing_contact = VW::Contact.new(:first_name => "Felix", :last_name => "Smith")
107
+ @customer.delivery_contact = delivery_contact
124
108
  @customer.billing_contact = billing_contact
125
109
  @customer.update
126
110
 
127
111
  r_cust = VW::Customer.show(@customer.id)
128
- r_cust.site_contact.should == site_contact
112
+ r_cust.delivery_contact.should == delivery_contact
129
113
  r_cust.billing_contact.should == billing_contact
130
114
  end
131
115
 
132
116
  end
133
-
117
+
134
118
  describe "#Delete" do
135
119
  it "Deletes the customer" do
136
120
  @customer.delete
@@ -138,7 +122,7 @@ describe VW::Customer do
138
122
  r_customer.should be_nil
139
123
  end
140
124
  end
141
-
125
+
142
126
  describe "#Find" do
143
127
  it "Finds all customers" do
144
128
  results = VW::Customer.find