pipejump 0.2.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.
@@ -0,0 +1,159 @@
1
+ module Pipejump
2
+
3
+ # The Sources resource is represented by an instance of Pipejump::Source.
4
+ #
5
+ # *Note*: To access any resources, you need a valid Session instance, referred to as @session in the following examples.
6
+ #
7
+ # == Access
8
+ #
9
+ # === Collection
10
+ #
11
+ # To fetch a collection of Pipejump::Source instances, call
12
+ #
13
+ #
14
+ # @session.sources
15
+ # # => #<Pipejump::Collection resource: Pipejump::Source>
16
+ #
17
+ #
18
+ # This returns a Pipejump::Collection instance. To retrieve an array of Pipejump::Source instances, call the _all_ method on the collection:
19
+ #
20
+ #
21
+ # @session.sources.all
22
+ # # => [#<Pipejump::Source name: "My Source", id: "1">, #<Pipejump::Source name: "Another Source", id: "2">]
23
+ #
24
+ #
25
+ # Instead of _all_, you can also call a variety of Array methods in the collection which will be delegated to the array returned by _all_, such as:
26
+ #
27
+ # * first
28
+ # * last
29
+ # * each
30
+ # * size
31
+ # * collect
32
+ # * reject
33
+ #
34
+ # *Examples*:
35
+ #
36
+ # @session.sources.first
37
+ # # => #<Pipejump::Source name: "My Source", id: "1">
38
+ # @session.sources.last
39
+ # # => #<Pipejump::Source name: "Another Source", id: "2">
40
+ # @session.sources.size
41
+ # # => 2
42
+ # @session.sources.each { |source| puts source.name }
43
+ # # Prints out "My Source" and "Another Source"
44
+ # @session.sources.collect { |source| source.name }
45
+ # # => ["My Source", "Another Source"]
46
+ # @session.sources.reject { |source| source.name == 'My Source' }
47
+ # # => ["My Source", "Another Source"]
48
+ #
49
+ #
50
+ # === Resource
51
+ #
52
+ # To fetch a single resource, call the _find_ method with the resource id as an argument:
53
+ #
54
+ #
55
+ # @session.sources.find(1)
56
+ # # => #<Pipejump::Source name: "My Source", id: "1">
57
+ #
58
+ #
59
+ # == Creation
60
+ #
61
+ # *Note*: Sources require the following fields in the hash of attributes:
62
+ #
63
+ # * _name_: a valid name
64
+ #
65
+ # To create a new source, call the _create_ method on the Source Pipejump::Collection with a hash of attributes as an argument:
66
+ #
67
+ #
68
+ # @session.sources.create(:name => 'Third Source')
69
+ # # => #<Pipejump::Source name: "Third Source", id: "3">
70
+ #
71
+ #
72
+ # If the resource was created properly, it will be returned with a id assigned to it. You can check it by calling the created? method
73
+ #
74
+ #
75
+ # source = @session.sources.create(:name => 'Third Source')
76
+ # # => #<Pipejump::Source name: "Third Source", id: "3">
77
+ # source.created?
78
+ # # => true
79
+ # source = @session.sources.create(:name => '')
80
+ # # => #<Pipejump::Source name: "">
81
+ # source.created?
82
+ # # => false
83
+ #
84
+ #
85
+ # You can access validation/creation errors by calling the _errors_ method on the instance returned by _create_:
86
+ #
87
+ #
88
+ # source = @session.sources.create(:name => '')
89
+ # # => #<Pipejump::Source name: "">
90
+ # source.created?
91
+ # # => false
92
+ # source.errors
93
+ # # => {"source"=>[{"error"=>{"code"=>"E0001", "field"=>"name", "description"=>"can't be blank"}}]}
94
+ #
95
+ #
96
+ # == Update
97
+ #
98
+ # To update a resource, change the attributes and call the _save_ method.
99
+ #
100
+ # === Changing attributes
101
+ #
102
+ # Each attribute has an accessor which you can use to change the values:
103
+ #
104
+ #
105
+ # source = @session.sources.create(:name => 'Third Source')
106
+ # # => #<Pipejump::Source name: "Third Source", id: "3">
107
+ # source.name
108
+ # # => 'Third Source'
109
+ # source.name = 'Super Source'
110
+ # # => 'Super Source'
111
+ # source.name
112
+ # # => 'Super Source'
113
+ #
114
+ #
115
+ # === Saving
116
+ #
117
+ # Once you've changed the attributes, call the _save_ method on the resource instance:
118
+ #
119
+ #
120
+ # source = @session.sources.create(:name => 'Third Source')
121
+ # # => #<Pipejump::Source name: "Third Source", id: "3">
122
+ # source.name
123
+ # # => 'Third Source'
124
+ # source.save
125
+ # # => true
126
+ #
127
+ #
128
+ # _save_ returns _true_ if save was successful and _false_ if it failed. In the latter scenario, you can access the errors via the _errors_ method:
129
+ #
130
+ #
131
+ # source = @session.sources.create(:name => 'Third Source')
132
+ # # => #<Pipejump::Source name: "Third Source", id: "3">
133
+ # source.name = 'Super Source'
134
+ # # => 'Super Source'
135
+ # source.save
136
+ # # => true
137
+ # source.name = ''
138
+ # # => ''
139
+ # source.save
140
+ # # => false
141
+ # source.errors
142
+ # # => {"source"=>[{"error"=>{"code"=>"E0001", "field"=>"name", "description"=>"can't be blank"}}]}
143
+ #
144
+ #
145
+ # == Removal
146
+ #
147
+ # To remove a resource, call the _destroy_ method on the instance:
148
+ #
149
+ #
150
+ # source = @session.sources.find(1)
151
+ # # => #<Pipejump::Source name: "My Source", id: "1">
152
+ # source.destroy
153
+ # # => true
154
+ #
155
+
156
+ class Source < Resource
157
+ end
158
+
159
+ end
data/lib/pipejump.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'net/https'
4
+ module Pipejump #:nodoc:
5
+ end
6
+
7
+ require 'pipejump/base/collection'
8
+ require 'pipejump/base/resource'
9
+ require 'pipejump/base/session'
10
+ require 'pipejump/base/errors'
11
+ require 'pipejump/base/connection'
12
+
13
+ require 'pipejump/resources/account'
14
+ require 'pipejump/resources/deal'
15
+ require 'pipejump/resources/note'
16
+ require 'pipejump/resources/reminder'
17
+ require 'pipejump/resources/source'
18
+ require 'pipejump/resources/client'
19
+ require 'pipejump/resources/contact'
@@ -0,0 +1,3 @@
1
+ endpoint: TEST_API_SERVER
2
+ email: YOUR_EMAIL
3
+ password: YOUR_PASSWORD
@@ -0,0 +1,119 @@
1
+ require 'spec/spec_helper'
2
+ describe Pipejump::Client do
3
+
4
+ before do
5
+ @session = PipejumpSpec.session
6
+ @client1 = @session.clients.create(:name => 'Client1')
7
+ @client1.id.should_not be_nil
8
+ @client2 = @session.clients.create(:name => 'Client2')
9
+ @client2.id.should_not be_nil
10
+ end
11
+
12
+ after do
13
+ @client1.destroy
14
+ @client2.destroy
15
+ end
16
+
17
+ describe '@session.clients' do
18
+
19
+ it ".all should return all clients" do
20
+ @session.clients.all.collect(&:name).should == [@client1, @client2].collect(&:name)
21
+ end
22
+
23
+ it ".first should return first client" do
24
+ @session.clients.first.name.should == @client1.name
25
+ end
26
+
27
+ it ".last should return last client" do
28
+ @session.clients.last.name.should == @client2.name
29
+ end
30
+
31
+ it ".find should find exact client" do
32
+ @session.clients.find(@client1.id).name.should == @client1.name
33
+ end
34
+
35
+ end
36
+
37
+ describe '#method_missing' do
38
+
39
+ it "should correctly get and set attributes" do
40
+ @client1.attributes.keys.sort.should == ['id', 'name']
41
+ @client1.attributes.keys.each do |attribute|
42
+ @client1.send(attribute).should == @client1.attributes[attribute]
43
+ end
44
+ @client1.name = 'Different name'
45
+ @client1.name.should == 'Different name'
46
+ @client1.attributes['name'].should == 'Different name'
47
+ end
48
+
49
+ it "should raise a NoMethodError when no accessor is set" do
50
+ lambda {
51
+ @client1.not_a_method_name
52
+ }.should raise_error(NoMethodError)
53
+ end
54
+
55
+ end
56
+
57
+ describe '#create' do
58
+
59
+ it "should create record" do
60
+ @client3 = @session.clients.create(:name => 'Client3')
61
+ @client3.id.should_not be_nil
62
+ @client3.name.should == 'Client3'
63
+ @session.clients.find(@client3.id).name.should == 'Client3'
64
+ @client3.destroy
65
+ end
66
+
67
+ it "should return error on validation fail" do
68
+ @client3 = @session.clients.create(:name => '')
69
+ @client3.id.should be_nil
70
+ @client3.errors.should_not == {}
71
+ end
72
+
73
+ end
74
+
75
+ describe '#update' do
76
+
77
+ it "should update record" do
78
+ @client1.name = 'Different name'
79
+ @client1.save.should == true
80
+ @session.clients.find(@client1.id).name.should == 'Different name'
81
+ end
82
+
83
+ it "should return error on validation fail" do
84
+ @client1.name = ''
85
+ @client1.save.should == false
86
+ @client1.errors.should_not == {}
87
+ end
88
+
89
+ end
90
+
91
+ describe '#contacts' do
92
+
93
+ before do
94
+ @contact1 = @session.contacts.create(:name => 'contact1', :client_id => @client1.id)
95
+ @contact2 = @session.contacts.create(:name => 'contact2', :client_id => @client1.id)
96
+ end
97
+
98
+ describe '#all' do
99
+
100
+ it "should return contacts of a client" do
101
+ contacts = @client1.contacts
102
+ contacts.size.should == 2
103
+ contacts.collect(&:name).should == ['contact1', 'contact2']
104
+ end
105
+
106
+ end
107
+
108
+ describe '#find' do
109
+
110
+ it "should return a single contact of a client" do
111
+ contact = @client1.contacts.find(@contact1.id)
112
+ contact.name == 'contact1'
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+
119
+ end
@@ -0,0 +1,93 @@
1
+ require 'spec/spec_helper'
2
+ describe Pipejump::Contact do
3
+
4
+ before do
5
+ @session = PipejumpSpec.session
6
+ @client = @session.clients.create(:name => 'Client1')
7
+ @contact1 = @session.contacts.create(:name => 'contact1', :client_id => @client.id)
8
+ @contact1.id.should_not be_nil
9
+ @contact2 = @session.contacts.create(:name => 'contact2', :client_id => @client.id)
10
+ @contact2.id.should_not be_nil
11
+ end
12
+
13
+ after do
14
+ @client.destroy
15
+ @contact1.destroy
16
+ @contact2.destroy
17
+ end
18
+
19
+ describe '@session.contacts' do
20
+
21
+ it ".all should return all contacts" do
22
+ @session.contacts.all.collect(&:name).should == [@contact1, @contact2].collect(&:name)
23
+ end
24
+
25
+ it ".first should return first contact" do
26
+ @session.contacts.first.name.should == @contact1.name
27
+ end
28
+
29
+ it ".last should return last contact" do
30
+ @session.contacts.last.name.should == @contact2.name
31
+ end
32
+
33
+ it ".find should find exact contact" do
34
+ @session.contacts.find(@contact1.id).name.should == @contact1.name
35
+ end
36
+
37
+ end
38
+
39
+ describe '#method_missing' do
40
+
41
+ it "should correctly get and set attributes" do
42
+ @contact1.attributes.keys.sort.should == ["client_id", "email", "id", "mobile", "name", "phone"]
43
+ @contact1.attributes.keys.each do |attribute|
44
+ @contact1.send(attribute).should == @contact1.attributes[attribute]
45
+ end
46
+ @contact1.name = 'Different name'
47
+ @contact1.name.should == 'Different name'
48
+ @contact1.attributes['name'].should == 'Different name'
49
+ end
50
+
51
+ it "should raise a NoMethodError when no accessor is set" do
52
+ lambda {
53
+ @contact1.not_a_method_name
54
+ }.should raise_error(NoMethodError)
55
+ end
56
+
57
+ end
58
+
59
+ describe '#create' do
60
+
61
+ it "should create record" do
62
+ @contact3 = @session.contacts.create(:name => 'contact3', :client_id => @client.id)
63
+ @contact3.id.should_not be_nil
64
+ @contact3.name.should == 'contact3'
65
+ @session.contacts.find(@contact3.id).name.should == 'contact3'
66
+ @contact3.destroy
67
+ end
68
+
69
+ it "should return error on validation fail" do
70
+ @contact3 = @session.contacts.create(:name => '')
71
+ @contact3.id.should be_nil
72
+ @contact3.errors.should_not == {}
73
+ end
74
+
75
+ end
76
+
77
+ describe '#update' do
78
+
79
+ it "should update record" do
80
+ @contact1.name = 'Different name'
81
+ @contact1.save.should == true
82
+ @session.contacts.find(@contact1.id).name.should == 'Different name'
83
+ end
84
+
85
+ it "should return error on validation fail" do
86
+ @contact1.name = ''
87
+ @contact1.save.should == false
88
+ @contact1.errors.should_not == {}
89
+ end
90
+
91
+ end
92
+
93
+ end
@@ -0,0 +1,115 @@
1
+ require 'spec/spec_helper'
2
+ describe Pipejump::Deal do
3
+
4
+ before do
5
+ @session = PipejumpSpec.session
6
+ @client = @session.clients.create(:name => 'Client1')
7
+ end
8
+
9
+ after do
10
+ @client.destroy
11
+ end
12
+
13
+ describe '#create' do
14
+
15
+ it "should create deal with valid params" do
16
+ @deal = @session.deals.create(:name => 'New deal', :client_id => @client.id)
17
+ @deal.attributes.keys.sort.should == ["client", "client_id", "deal_tags", "hot", "id", "name", "scope", "stage_name"]
18
+ (@deal.attributes.keys - ['client']).each do |attribute|
19
+ @deal.send(attribute).should == @deal.attributes[attribute]
20
+ end
21
+ @deal.client.class.should == Pipejump::Client
22
+ @deal.destroy
23
+ end
24
+
25
+ it "should return errors with invalid params" do
26
+ @deal = @session.deals.create({})
27
+ @deal.id.should == nil
28
+ @deal.errors['deal'].collect{ |e| e['error']['field'] }.sort.should == ['client', 'name']
29
+ end
30
+
31
+ end
32
+
33
+ describe '.find' do
34
+
35
+ it "should find deal" do
36
+ @deal = @session.deals.create(:name => 'New deal', :client_id => @client.id)
37
+ @found = @session.deals.find(@deal.id)
38
+ @found.name == @deal.name
39
+ @deal.destroy
40
+ end
41
+
42
+ it "should raise error if not found" do
43
+ lambda {
44
+ @session.deals.find(-1)
45
+ }.should raise_error(Pipejump::ResourceNotFound)
46
+ end
47
+ end
48
+
49
+ describe '#update' do
50
+
51
+ before do
52
+ @deal = @session.deals.create(:name => 'New deal', :client_id => @client.id)
53
+ end
54
+
55
+ after do
56
+ @deal.destroy
57
+ end
58
+
59
+ it "should update deal with valid params" do
60
+ @deal.name = 'Updated deal'
61
+ @deal.save
62
+ @deal.save.should == true
63
+ @found = @session.deals.find(@deal.id)
64
+ @found.name.should == 'Updated deal'
65
+ end
66
+
67
+ it "should return errors with invalid params" do
68
+ @deal.name = ''
69
+ @deal.save.should == false
70
+ @deal.errors['deal'].collect{ |e| e['error']['field'] }.sort.should == ['name']
71
+ end
72
+
73
+ end
74
+
75
+ describe '#contacts' do
76
+
77
+ before do
78
+ @deal = @session.deals.create(:name => 'New deal', :client_id => @client.id)
79
+ @contact = @session.contacts.create(:name => 'Tom', :client_id => @client.id)
80
+ @contact2 = @session.contacts.create(:name => 'Mike', :client_id => @client.id)
81
+ @deal.contacts.update(@contact.id).should == true
82
+ end
83
+
84
+ after do
85
+ @contact.destroy
86
+ @contact2.destroy
87
+ end
88
+
89
+ it "should return a collection of contacts" do
90
+ contacts = @deal.contacts
91
+ contacts.class.should == Pipejump::Collection
92
+ contacts.respond_to?(:update).should == true
93
+ contacts.size.should == 1
94
+ end
95
+
96
+ describe '#update' do
97
+
98
+ it "should update a collection of contacts and be smart about it" do
99
+ @deal.contacts.update([@contact.id, @contact2.id])
100
+ @deal.contacts.size.should == 2
101
+ @deal.contacts.update([@contact])
102
+ @deal.contacts.size.should == 1
103
+ @deal.contacts.update([@contact.id, @contact2])
104
+ @deal.contacts.size.should == 2
105
+ @deal.contacts.update([@contact2.id])
106
+ @deal.contacts.size.should == 1
107
+ @deal.contacts.update([@contact.id, @contact2.id].join(','))
108
+ @deal.contacts.size.should == 2
109
+ end
110
+
111
+ end
112
+
113
+ end
114
+
115
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec/spec_helper'
2
+ describe Pipejump::Note do
3
+
4
+ before do
5
+ @session = PipejumpSpec.session
6
+ @client = @session.clients.create(:name => 'Client1')
7
+ @deal = @session.deals.create(:name => 'New deal', :client_id => @client.id)
8
+ end
9
+
10
+ after do
11
+ @client.destroy
12
+ end
13
+
14
+ describe '#all' do
15
+
16
+ before do
17
+ @note = @deal.notes.create(:content => 'Some note')
18
+ end
19
+
20
+ after do
21
+ @note.destroy
22
+ end
23
+
24
+ it "should fetch notes within a deal" do
25
+ @deal.notes.size.should == 1
26
+ @deal.notes.first.content.should == 'Some note'
27
+ end
28
+
29
+ end
30
+
31
+ describe '#find' do
32
+
33
+ it "should fetch notes within a deal" do
34
+ @note = @deal.notes.create(:content => 'Some note')
35
+ @found = @deal.notes.find(@note.id)
36
+ @found.content.should == 'Some note'
37
+ end
38
+
39
+ it "should raise an error if not found" do
40
+ lambda {
41
+ @deal.notes.find(-1)
42
+ }.should raise_error(Pipejump::ResourceNotFound)
43
+ end
44
+
45
+ end
46
+
47
+ describe '#create' do
48
+
49
+ it "should create note with valid params" do
50
+ @note = @deal.notes.create(:content => 'Some note')
51
+ @note.attributes.keys.sort.should == ["content", "id", "username"]
52
+ @note.destroy
53
+ end
54
+
55
+ it "should not create note with invalid params" do
56
+ @note = @deal.notes.create(:content => '')
57
+ @note.errors['note'].collect{ |e| e['error']['field'] }.sort.should == ['content']
58
+ end
59
+
60
+ end
61
+
62
+ describe '#update' do
63
+
64
+ before do
65
+ @note = @deal.notes.create(:content => 'Some note')
66
+ end
67
+
68
+ after do
69
+ @note.destroy
70
+ end
71
+
72
+ it "should update note with valid params" do
73
+ @note.content = 'Updated note'
74
+ @note.save.should == true
75
+ @deal.notes.find(@note.id).content.should == 'Updated note'
76
+ end
77
+
78
+ it "should not update note with invalid params" do
79
+ @note.content = ''
80
+ @note.save.should == false
81
+ @note.errors['note'].collect{ |e| e['error']['field'] }.sort.should == ['content']
82
+ end
83
+
84
+ end
85
+
86
+ describe '#destroy' do
87
+
88
+ it "should destroy a note" do
89
+ @note = @deal.notes.create(:content => 'Some note')
90
+ @note.destroy.should == true
91
+ lambda {
92
+ @deal.notes.find(@note.id)
93
+ }.should raise_error(Pipejump::ResourceNotFound)
94
+
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+