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,172 @@
1
+ module Pipejump
2
+
3
+ # The Pipejump::Session instance represents an active Session with the Pipejump API
4
+ #
5
+ # Any access to the Pipejump API requires authentication, which means you need to initialize a
6
+ # Pipejump::Session instance before calling any other methods
7
+ #
8
+ # == Authentication
9
+ #
10
+ # To authenticate, simply call Pipejump::Session.new with a argument hash with the following keys:
11
+ #
12
+ # * _email_ - your Pipejump user account email
13
+ # * _password_ - your Pipejump user account password
14
+ # * _endpoint_ - (optional) Default is http://api.pipejump.com, however you can override it (for example for development)
15
+ #
16
+ # You can perform later actions either on a instance returned by the canstructor or within a supplied block
17
+ #
18
+ #
19
+ # @session = Pipejump::Session.new(:email => EMAIL, :password => PASSWORD)
20
+ # @session.account
21
+ #
22
+ #
23
+ # or
24
+ #
25
+ #
26
+ # Pipejump::Session.new(:email => EMAIL, :password => PASSWORD) do |session|
27
+ # session.account
28
+ # end
29
+ #
30
+ # As of version 0.1.1 you can use the token which is fetched when authenticating using the email and password. So once you get the token, you can use it for future initialization of the Session and not send the username and password, which is a more secure.
31
+ #
32
+ # @session = Pipejump::Session.new(:token => 'your_token')
33
+ #
34
+ # Also, as of version 0.1.1 connection is performed over SSL.
35
+ #
36
+ #
37
+ #
38
+ #
39
+ # == Account
40
+ #
41
+ # To access the Account instance, call the account method
42
+ #
43
+ #
44
+ # @session.account # => #<Pipejump::Account name: "myaccount", id: "1", currency_name: "$">
45
+ #
46
+ #
47
+ # == Deals
48
+ #
49
+ # You can access your deals by calling
50
+ #
51
+ #
52
+ # @session.deals
53
+ #
54
+ #
55
+ # With Deals you get access to Notes, Reminders and Deal Contacts.
56
+ #
57
+ # For more information, consult the Deals page.
58
+ #
59
+ # == Clients
60
+ #
61
+ # You can access your clients by calling
62
+ #
63
+ #
64
+ # @session.clients
65
+ #
66
+ #
67
+ # For more information, consult the Clients page.
68
+ #
69
+ # == Contacts
70
+ #
71
+ # You can access your contacts by calling
72
+ #
73
+ #
74
+ # @session.contacts
75
+ #
76
+ #
77
+ # For more information, consult the Contacts page.
78
+ #
79
+ # == Sources
80
+ #
81
+ # You can access your sources by calling
82
+ #
83
+ #
84
+ # @session.sources
85
+ #
86
+ #
87
+ # For more information, consult the Sources page.
88
+ #
89
+ class Session
90
+
91
+ attr_accessor :token
92
+
93
+ def initialize(params, &block)
94
+ if endpoint = params.delete("endpoint") or endpoint = params.delete(:endpoint)
95
+ connection(endpoint)
96
+ end
97
+ # If user supplies token, do not connect for authentication
98
+ if token = params.delete('token') or token = params.delete(:token)
99
+ self.token = token
100
+ else
101
+ authenticate(params)
102
+ end
103
+ yield(self) if block_given?
104
+ end
105
+
106
+ def authenticate(params) #:nodoc:
107
+ response = connection.post('/authentication', params.collect { |pair| pair.join('=') }.join('&'))
108
+ data = JSON.parse(response.body)
109
+ self.token = data['authentication']['token']
110
+ raise AuthenticationFailed if response.code == '401'
111
+ end
112
+
113
+ def connection(endpoint = nil) #:nodoc:
114
+ @connection ||= Connection.new(self, endpoint)
115
+ end
116
+
117
+ def get(url) #:nodoc:
118
+ response = connection.get(url)
119
+ data = (response.code.to_i == 200 and url.match('.json')) ? JSON.parse(response.body) : ''
120
+ [response.code.to_i, data]
121
+ end
122
+
123
+ def post(url, data) #:nodoc:
124
+ response = connection.post(url, data)
125
+ data = url.match('.json') ? JSON.parse(response.body) : response.body
126
+ [response.code.to_i, data]
127
+ end
128
+
129
+ def put(url, data) #:nodoc:
130
+ response = connection.put(url, data)
131
+ data = url.match('.json') ? JSON.parse(response.body) : response.body
132
+ [response.code.to_i, data]
133
+ end
134
+
135
+ def delete(url) #:nodoc:
136
+ response = connection.delete(url)
137
+ [response.code.to_i, response.body]
138
+ end
139
+
140
+ def inspect
141
+ "#<#{self.class} token: \"#{token}\">"
142
+ end
143
+
144
+ # Returns a Pipejump::Account instance of the current account
145
+ def account
146
+ code, response = get('/account.json')
147
+ Account.new(response['account'])
148
+ end
149
+
150
+ # Returns a Pipejump::Collection instance of Clients
151
+ def clients
152
+ Collection.new(self, Client)
153
+ end
154
+
155
+ # Returns a Pipejump::Collection instance of Sources
156
+ def sources
157
+ Collection.new(self, Source)
158
+ end
159
+
160
+ # Returns a Pipejump::Collection instance of Contacts
161
+ def contacts
162
+ Collection.new(self, Contact)
163
+ end
164
+
165
+ # Returns a Pipejump::Collection instance of Deals
166
+ def deals
167
+ Collection.new(self, Deal)
168
+ end
169
+
170
+
171
+ end
172
+ end
@@ -0,0 +1,20 @@
1
+ module Pipejump
2
+
3
+ # The Account resource is represented by an instance of Pipejump::Account, which holds information about your account, such as:
4
+ #
5
+ # * id
6
+ # * name
7
+ # * currency
8
+ #
9
+ # *Note*: To access any resources, you need a valid [[Session]] instance, referred to as @session in the following examples.
10
+ #
11
+ # == Access
12
+ #
13
+ # To access an instance of Pipejump::Account, call
14
+ #
15
+ # @session.account
16
+ #
17
+ class Account < Resource
18
+ end
19
+
20
+ end
@@ -0,0 +1,162 @@
1
+ module Pipejump
2
+
3
+
4
+ # The Client resource is represented by an instance of Pipejump::Client.
5
+ #
6
+ # *Note*: To access any resources, you need a valid Session instance, referred to as @session in the following examples.
7
+ #
8
+ # == Access
9
+ #
10
+ # === Collection
11
+ #
12
+ # To fetch a collection of Pipejump::Client instances, call
13
+ #
14
+ #
15
+ # @session.clients
16
+ # # => #<Pipejump::Collection resource: Pipejump::Client>
17
+ #
18
+ #
19
+ # This returns a Pipejump::Collection instance. To retrieve an array of Pipejump::Client instances, call the _all_ method on the collection:
20
+ #
21
+ #
22
+ # @session.clients.all
23
+ # # => [#<Pipejump::Client name: "My Client", id: "1">, #<Pipejump::Client name: "Another Client", id: "2">]
24
+ #
25
+ #
26
+ # 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:
27
+ #
28
+ # * first
29
+ # * last
30
+ # * each
31
+ # * size
32
+ # * collect
33
+ # * reject
34
+ #
35
+ # *Examples*:
36
+ #
37
+ # @session.clients.first
38
+ # # => #<Pipejump::Client name: "My Client", id: "1">
39
+ # @session.clients.last
40
+ # # => #<Pipejump::Client name: "Another Client", id: "2">
41
+ # @session.clients.size
42
+ # # => 2
43
+ # @session.clients.each { |client| puts client.name }
44
+ # # Prints out "My Client" and "Another Client"
45
+ # @session.clients.collect { |client| client.name }
46
+ # # => ["My Client", "Another Client"]
47
+ # @session.clients.reject { |client| client.name == 'My Client' }
48
+ # # => ["My Client", "Another Client"]
49
+ #
50
+ #
51
+ # === Resource
52
+ #
53
+ # To fetch a single resource, call the _find_ method with the resource id as an argument:
54
+ #
55
+ #
56
+ # @session.clients.find(1)
57
+ # # => #<Pipejump::Client name: "My Client", id: "1">
58
+ #
59
+ #
60
+ # == Creation
61
+ #
62
+ # *Note*: Clients require the following fields in the hash of attributes:
63
+ #
64
+ # * _name_: a valid name
65
+ #
66
+ # To create a new client, call the _create_ method on the Client Pipejump::Collection with a hash of attributes as an argument:
67
+ #
68
+ #
69
+ # @session.clients.create(:name => 'Third Client')
70
+ # # => #<Pipejump::Client name: "Third Client", id: "3">
71
+ #
72
+ #
73
+ # 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
74
+ #
75
+ #
76
+ # client = @session.clients.create(:name => 'Third Client')
77
+ # # => #<Pipejump::Client name: "Third Client", id: "3">
78
+ # client.created?
79
+ # # => true
80
+ # client = @session.clients.create(:name => '')
81
+ # # => #<Pipejump::Client name: "">
82
+ # client.created?
83
+ # # => false
84
+ #
85
+ #
86
+ # You can access validation/creation errors by calling the _errors_ method on the instance returned by _create_:
87
+ #
88
+ #
89
+ # client = @session.clients.create(:name => '')
90
+ # # => #<Pipejump::Client name: "">
91
+ # client.created?
92
+ # # => false
93
+ # client.errors
94
+ # # => {"client"=>[{"error"=>{"code"=>"E0001", "field"=>"name", "description"=>"can't be blank"}}]}
95
+ #
96
+ #
97
+ # == Update
98
+ #
99
+ # To update a resource, change the attributes and call the _save_ method.
100
+ #
101
+ # === Changing attributes
102
+ #
103
+ # Each attribute has an accessor which you can use to change the values:
104
+ #
105
+ #
106
+ # client = @session.clients.create(:name => 'Third Client')
107
+ # # => #<Pipejump::Client name: "Third Client", id: "3">
108
+ # client.name
109
+ # # => 'Third Client'
110
+ # client.name = 'Super Client'
111
+ # # => 'Super Client'
112
+ # client.name
113
+ # # => 'Super Client'
114
+ #
115
+ #
116
+ # === Saving
117
+ #
118
+ # Once you've changed the attributes, call the _save_ method on the resource instance:
119
+ #
120
+ #
121
+ # client = @session.clients.create(:name => 'Third Client')
122
+ # # => #<Pipejump::Client name: "Third Client", id: "3">
123
+ # client.name
124
+ # # => 'Third Client'
125
+ # client.save
126
+ # # => true
127
+ #
128
+ #
129
+ # _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:
130
+ #
131
+ #
132
+ # client = @session.clients.create(:name => 'Third Client')
133
+ # # => #<Pipejump::Client name: "Third Client", id: "3">
134
+ # client.name = 'Super Client'
135
+ # # => 'Super Client'
136
+ # client.save
137
+ # # => true
138
+ # client.name = ''
139
+ # # => ''
140
+ # client.save
141
+ # # => false
142
+ # client.errors
143
+ # # => {"client"=>[{"error"=>{"code"=>"E0001", "field"=>"name", "description"=>"can't be blank"}}]}
144
+ #
145
+ #
146
+ # == Removal
147
+ #
148
+ # To remove a resource, call the _destroy_ method on the instance:
149
+ #
150
+ #
151
+ # client = @session.clients.find(1)
152
+ # # => #<Pipejump::Client name: "My Client", id: "1">
153
+ # client.destroy
154
+ # # => true
155
+ #
156
+ class Client < Resource
157
+ has_many :contacts do
158
+ disable :create
159
+ end
160
+ end
161
+
162
+ end
@@ -0,0 +1,225 @@
1
+ module Pipejump
2
+
3
+ # A Contact is represented by an instance of Pipejump::Contact.
4
+ #
5
+ # *Note*: To access any contacts, 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::Contact instances, call
12
+ #
13
+ #
14
+ # @session.contacts
15
+ # # => #<Pipejump::Collection recontact: Pipejump::Contact>
16
+ #
17
+ #
18
+ # This returns a Pipejump::Collection instance. To retrieve an array of Pipejump::Contact instances, call the _all_ method on the collection:
19
+ #
20
+ #
21
+ # @session.contacts.all
22
+ # # => [#<Pipejump::Contact name: "Tom", id: "1", mobile: "123", phone: "456", email: "tom@email.com">, #<Pipejump::Contact name: "Mike", id: "2", mobile: "321", phone: "654", email: "mike@email.com">]
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.contacts.first
37
+ # # => #<Pipejump::Contact name: "Tom", id: "1", mobile: "123", phone: "456", email: "tom@email.com">
38
+ # @session.contacts.last
39
+ # # => #<Pipejump::Contact name: "Mike", id: "2", mobile: "321", phone: "654", email: "mike@email.com">
40
+ # @session.contacts.size
41
+ # # => 2
42
+ # @session.contacts.each { |contact| puts contact.name }
43
+ # # Prints out "Tom" and "Mike"
44
+ # @session.contacts.collect { |contact| contact.name }
45
+ # # => ["Tom", "Mike"]
46
+ # @session.contacts.reject { |contact| contact.name == 'Tom' }
47
+ # # => ["Mike"]
48
+ #
49
+ #
50
+ # === Resource
51
+ #
52
+ # To fetch a single contact, call the _find_ method with the contact id as an argument:
53
+ #
54
+ #
55
+ # @session.contacts.find(1)
56
+ # # => #<Pipejump::Contact name: "Tom", id: "1", mobile: "123", phone: "456", email: "tom@email.com">
57
+ #
58
+ #
59
+ # == Creation
60
+ #
61
+ # *Note*: Contacts require the following fields in the hash of attributes:
62
+ #
63
+ # * _name_: a valid name
64
+ # * _client_id_: a valid a Client| id
65
+ #
66
+ # To create a new contact, call the _create_ method on the Contact Pipejump::Collection with a hash of attributes as an argument.
67
+ #
68
+ #
69
+ # @session.contacts.create(:name => 'Jerry', :client_id => 1)
70
+ # # => #<Pipejump::Contact name: "Jerry", id: "3", mobile: "", client_id: "1", phone: "", email: "">
71
+ #
72
+ #
73
+ # If the contact was created properly, it will be returned with a id assigned to it. You can check it by calling the created? method
74
+ #
75
+ #
76
+ # contact = @session.contacts.create(:name => 'Jerry', :client_id => 1)
77
+ # # => #<Pipejump::Contact name: "Jerry", id: "3", mobile: "", client_id: "1", phone: "", email: "">
78
+ # contact.created?
79
+ # # => true
80
+ # contact = @session.contacts.create(:name => '')
81
+ # # => #<Pipejump::Contact name: "">
82
+ # contact.created?
83
+ # # => false
84
+ #
85
+ #
86
+ # You can access validation/creation errors by calling the _errors_ method on the instance returned by _create_:
87
+ #
88
+ #
89
+ # contact = @session.contacts.create(:name => '')
90
+ # # => #<Pipejump::Contact name: "">
91
+ # contact.created?
92
+ # # => false
93
+ # contact.errors
94
+ # # => "contact"=>[{"error"=>{"code"=>"E0001", "field"=>"name", "description"=>"Well... we need a name."}}, {"error"=>{"code"=>"E0001", "field"=>"client", "description"=>"can't be blank"}}]}
95
+ #
96
+ #
97
+ # == Update
98
+ #
99
+ # To update a contact, change the attributes and call the _save_ method.
100
+ #
101
+ # === Changing attributes
102
+ #
103
+ # Each attribute has an accessor which you can use to change the values:
104
+ #
105
+ #
106
+ # contact = @session.contacts.create(:name => 'Jerry', :client_id => 1)
107
+ # # => #<Pipejump::Contact name: "Jerry", id: "3", mobile: "", client_id: "1", phone: "", email: "">
108
+ # contact.name
109
+ # # => 'Jerry'
110
+ # contact.name = 'Wally'
111
+ # # => 'Wally'
112
+ # contact.name
113
+ # # => 'Wally'
114
+ #
115
+ #
116
+ # === Saving
117
+ #
118
+ # Once you've changed the attributes, call the _save_ method on the contact instance:
119
+ #
120
+ #
121
+ # contact = @session.contacts.create(:name => 'Jerry', :client_id => 1)
122
+ # # => #<Pipejump::Contact name: "Jerry", id: "3", mobile: "", client_id: "1", phone: "", email: "">
123
+ # contact.name = 'Wally'
124
+ # # => 'Wally'
125
+ # contact.save
126
+ # # => true
127
+ #
128
+ #
129
+ # _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:
130
+ #
131
+ #
132
+ # contact = @session.contacts.create(:name => 'Jerry', :client_id => 1)
133
+ # # => #<Pipejump::Contact name: "Jerry", id: "3", mobile: "", client_id: "1", phone: "", email: "">
134
+ # contact.name = 'Wally'
135
+ # # => 'Wally'
136
+ # contact.save
137
+ # # => true
138
+ # contact.name = ''
139
+ # # => ''
140
+ # contact.save
141
+ # # => false
142
+ # contact.errors
143
+ # # => "contact"=>[{"error"=>{"code"=>"E0001", "field"=>"name", "description"=>"Well... we need a name."}}, {"error"=>{"code"=>"E0001", "field"=>"client", "description"=>"can't be blank"}}]}
144
+ #
145
+ #
146
+ # == Removal
147
+ #
148
+ # To remove a contact, call the _destroy_ method on the instance:
149
+ #
150
+ #
151
+ # contact = @session.contacts.find(1)
152
+ # # => #<Pipejump::Contact name: "Tom", id: "1", mobile: "123", phone: "456", email: "tom@email.com">
153
+ # contact.destroy
154
+ # # => true
155
+ #
156
+ #
157
+ # = Contacts within a Deal
158
+ #
159
+ # A Contact is represented by an instance of Pipejump::Contact. Deal Contacts, however, can only be accessed from a specific deal.
160
+ #
161
+ # *Note*: To access any Deal Contacts, you need a valid Deal| instance, referred to as @deal in the following examples. Deal Contacts belong to a deal, so you can only access them via @deal, not the session.
162
+ #
163
+ # == Access
164
+ #
165
+ # === Collection
166
+ #
167
+ # To fetch a collection of Pipejump::Contact instances belonging to a specific Deal, call
168
+ #
169
+ #
170
+ # @deal.contacts
171
+ # # => #<Pipejump::Collection resource: Pipejump::Contact, owner: #<Pipejump::Deal name: "My Deal", scope: "0", hot: "false", stage_name: "incoming", id: "1", deal_tags: "">>
172
+ #
173
+ #
174
+ # This returns a Pipejump::Collection instance. To retrieve an array of Pipejump::Contact instances belonging to a specific Deal, call the _all_ method on the collection:
175
+ #
176
+ #
177
+ # @deal.contacts.all
178
+ # # => [#<Pipejump::Contact name: "Tom", id: "1", mobile: "123", phone: "456", email: "tom@email.com">,
179
+ # # #<Pipejump::Contact name: "Mike", id: "2", mobile: "321", phone: "654", email: "mike@email.com">]
180
+ #
181
+ #
182
+ # 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:
183
+ #
184
+ # * first
185
+ # * last
186
+ # * each
187
+ # * size
188
+ # * collect
189
+ # * reject
190
+ #
191
+ # *Examples*:
192
+ #
193
+ # @deal.contacts.first
194
+ # # => #<Pipejump::Contact name: "Tom", id: "1", mobile: "123", phone: "456", email: "tom@email.com">
195
+ # @deal.contacts.last
196
+ # # => #<Pipejump::Contact name: "Mike", id: "2", mobile: "321", phone: "654", email: "mike@email.com">
197
+ # @deal.contacts.size
198
+ # # => 2
199
+ # @deal.contacts.each { |contact| puts contact.name }
200
+ # # Prints out "Tom" and "Mike"
201
+ # @deal.contacts.collect { |contact| contact.name }
202
+ # # => ["Tom", "Mike"]
203
+ # @deal.contacts.reject { |contact| contact.name == 'Tom' }
204
+ # # => ["Mike"]
205
+ #
206
+ #
207
+ # == Updating
208
+ #
209
+ # To update the Contacts that are assigned to a Deal|, call the _update_ method on the Contacts collection:
210
+ #
211
+ #
212
+ # @deal.contacts.collect(&:id)
213
+ # # => ['1', '2', '3']
214
+ # @deal.contacts.update('1,2')
215
+ # # => true
216
+ # @deal.contacts.collect(&:id)
217
+ # # => ['1', '2']
218
+ #
219
+ #
220
+ # *Note*: The Contacts must have the same Client as the Deal or update will return _false_
221
+ class Contact < Resource
222
+ belongs_to :client
223
+ end
224
+
225
+ end