rdstation-ruby-client 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e3abd8cbbff8541804f9098ccf8076025404e9fd
4
- data.tar.gz: 303aed653c2a72f186bdbfc2b1dea8f56c7ff55d
3
+ metadata.gz: a0928853bb9ae75ec8ed56f8f532c18ddc42c205
4
+ data.tar.gz: 74e5de6ea1c9f33cae06f781f4979a0e674b7e1b
5
5
  SHA512:
6
- metadata.gz: 752ad7a2b479d064dfc176f2299f31035a51bfff11dbf71f3bf96fd813ac840524125789a035880a613b1c77b79ca8698b0b40687afb9ef28eee4ab4da1f6807
7
- data.tar.gz: 1494b26e7d0867b640e8ea4deab9e57b8fb716417a194c821a9e77f7a9ebc8bce1057910be8e10908afde16b1ab347b28add6feacbb1f8b49818adb76019aace
6
+ metadata.gz: a89281c0b92792ebe57eb4383dcb460aa0edc04b967a8aa283a7272fc336dd410ba4b2e0482109949cb6f27321af5566c055bd49c9e7a55efa85c995b4329ab4
7
+ data.tar.gz: 0703a456fd3772fa02b1026f69c6bcec1b3dfb22b34efe93ba6ce75c4bcd528feb88b2d9dba8dc97d4dccc14eaa8c9a965c59367bc0e786106b57f1e2c3f9271
@@ -0,0 +1,57 @@
1
+ # Ruby CircleCI 2.0 configuration file
2
+ #
3
+ # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
+ #
5
+ version: 2
6
+ jobs:
7
+ build:
8
+ docker:
9
+ # specify the version you desire here
10
+ - image: circleci/ruby:2.4.1-node-browsers
11
+
12
+ # Specify service dependencies here if necessary
13
+ # CircleCI maintains a library of pre-built images
14
+ # documented at https://circleci.com/docs/2.0/circleci-images/
15
+ # - image: circleci/postgres:9.4
16
+
17
+ working_directory: ~/repo
18
+
19
+ steps:
20
+ - checkout
21
+
22
+ # Download and cache dependencies
23
+ - restore_cache:
24
+ keys:
25
+ - v1-dependencies-{{ checksum "Gemfile.lock" }}
26
+ # fallback to using the latest cache if no exact match is found
27
+ - v1-dependencies-
28
+
29
+ - run:
30
+ name: install dependencies
31
+ command: |
32
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
33
+
34
+ - save_cache:
35
+ paths:
36
+ - ./vendor/bundle
37
+ key: v1-dependencies-{{ checksum "Gemfile.lock" }}
38
+
39
+ # run tests!
40
+ - run:
41
+ name: run tests
42
+ command: |
43
+ mkdir /tmp/test-results
44
+ TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)"
45
+
46
+ bundle exec rspec --format progress \
47
+ --format RspecJunitFormatter \
48
+ --out /tmp/test-results/rspec.xml \
49
+ --format progress \
50
+ $TEST_FILES
51
+
52
+ # collect reports
53
+ - store_test_results:
54
+ path: /tmp/test-results
55
+ - store_artifacts:
56
+ path: /tmp/test-results
57
+ destination: test-results
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rdstation-ruby-client (1.0.1)
4
+ rdstation-ruby-client (1.2.0)
5
5
  httparty (~> 0.12)
6
6
 
7
7
  GEM
@@ -32,6 +32,8 @@ GEM
32
32
  rspec-mocks (3.1.3)
33
33
  rspec-support (~> 3.1.0)
34
34
  rspec-support (3.1.2)
35
+ rspec_junit_formatter (0.3.0)
36
+ rspec-core (>= 2, < 4, != 2.12.0)
35
37
  safe_yaml (1.0.4)
36
38
  turn (0.9.7)
37
39
  ansi
@@ -49,6 +51,7 @@ DEPENDENCIES
49
51
  rake
50
52
  rdstation-ruby-client!
51
53
  rspec
54
+ rspec_junit_formatter
52
55
  turn
53
56
  webmock (~> 2.1)
54
57
 
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![CircleCI](https://circleci.com/gh/ResultadosDigitais/rdstation-ruby-client.svg?style=svg)](https://circleci.com/gh/ResultadosDigitais/rdstation-ruby-client)
2
+
1
3
  # RDStation Ruby Client
2
4
 
3
5
  RDstation ruby wrapper to interact with RDStation API.
@@ -1,11 +1,15 @@
1
1
  require 'httparty'
2
2
 
3
+ # API Response handler
4
+ require 'rdstation/api_response'
5
+
3
6
  # API requests
4
7
  require 'rdstation/authentication'
5
8
  require 'rdstation/client'
6
9
  require 'rdstation/contacts'
7
10
  require 'rdstation/events'
8
11
  require 'rdstation/fields'
12
+ require 'rdstation/webhooks'
9
13
 
10
14
  # Error handling
11
15
  require 'rdstation/error'
@@ -0,0 +1,9 @@
1
+ module RDStation
2
+ module ApiResponse
3
+ def self.build(response)
4
+ response_body = JSON.parse(response.body)
5
+ return response_body unless response_body['errors']
6
+ RDStation::ErrorHandler.new(response).raise_errors
7
+ end
8
+ end
9
+ end
@@ -35,9 +35,7 @@ module RDStation
35
35
  # or code is invalid.
36
36
  def authenticate(code)
37
37
  response = post_to_auth_endpoint(code: code)
38
- parsed_body = JSON.parse(response.body)
39
- return parsed_body unless parsed_body['errors']
40
- RDStation::ErrorHandler.new(response).raise_errors
38
+ ApiResponse.build(response)
41
39
  end
42
40
 
43
41
  #
@@ -46,9 +44,7 @@ module RDStation
46
44
  #
47
45
  def update_access_token(refresh_token)
48
46
  response = post_to_auth_endpoint(refresh_token: refresh_token)
49
- parsed_body = JSON.parse(response.body)
50
- return parsed_body unless parsed_body['errors']
51
- RDStation::ErrorHandler.new(response).raise_errors
47
+ ApiResponse.build(response)
52
48
  end
53
49
 
54
50
  private
@@ -14,16 +14,12 @@ module RDStation
14
14
  #
15
15
  def by_uuid(uuid)
16
16
  response = self.class.get(base_url(uuid), headers: required_headers)
17
- response_body = JSON.parse(response.body)
18
- return response_body unless response_body['errors']
19
- RDStation::ErrorHandler.new(response).raise_errors
17
+ ApiResponse.build(response)
20
18
  end
21
19
 
22
20
  def by_email(email)
23
21
  response = self.class.get(base_url("email:#{email}"), headers: required_headers)
24
- response_body = JSON.parse(response.body)
25
- return response_body unless response_body['errors']
26
- RDStation::ErrorHandler.new(response).raise_errors
22
+ ApiResponse.build(response)
27
23
  end
28
24
 
29
25
  # The Contact hash may contain the following parameters:
@@ -39,9 +35,7 @@ module RDStation
39
35
  # :tags
40
36
  def update(uuid, contact_hash)
41
37
  response = self.class.patch(base_url(uuid), :body => contact_hash.to_json, :headers => required_headers)
42
- response_body = JSON.parse(response.body)
43
- return response_body unless response_body['errors']
44
- RDStation::ErrorHandler.new(response).raise_errors
38
+ ApiResponse.build(response)
45
39
  end
46
40
 
47
41
  #
@@ -55,9 +49,7 @@ module RDStation
55
49
  def upsert(identifier, identifier_value, contact_hash)
56
50
  path = "#{identifier}:#{identifier_value}"
57
51
  response = self.class.patch(base_url(path), body: contact_hash.to_json, headers: required_headers)
58
- response_body = JSON.parse(response.body)
59
- return response_body unless response_body['errors']
60
- RDStation::ErrorHandler.new(response).raise_errors
52
+ ApiResponse.build(response)
61
53
  end
62
54
 
63
55
  private
@@ -1,6 +1,4 @@
1
- # encoding: utf-8
2
1
  module RDStation
3
- # More info: https://developers.rdstation.com/pt-BR/reference/contacts
4
2
  class Events
5
3
  include HTTParty
6
4
 
@@ -12,9 +12,7 @@ module RDStation
12
12
 
13
13
  def all
14
14
  response = self.class.get(BASE_URL, headers: required_headers)
15
- response_body = JSON.parse(response.body)
16
- return response_body unless response_body['errors']
17
- RDStation::ErrorHandler.new(response).raise_errors
15
+ ApiResponse.build(response)
18
16
  end
19
17
 
20
18
  private
@@ -1,3 +1,3 @@
1
1
  module RDStation
2
- VERSION = '1.1.0'.freeze
2
+ VERSION = '1.2.0'.freeze
3
3
  end
@@ -0,0 +1,49 @@
1
+ module RDStation
2
+ class Webhooks
3
+ include HTTParty
4
+
5
+ def initialize(auth_token)
6
+ @auth_token = auth_token
7
+ end
8
+
9
+ def all
10
+ response = self.class.get(base_url, headers: required_headers)
11
+ ApiResponse.build(response)
12
+ end
13
+
14
+ def by_uuid(uuid)
15
+ response = self.class.get(base_url(uuid), headers: required_headers)
16
+ ApiResponse.build(response)
17
+ end
18
+
19
+ def create(payload)
20
+ response = self.class.post(base_url, headers: required_headers, body: payload.to_json)
21
+ ApiResponse.build(response)
22
+ end
23
+
24
+ def update(uuid, payload)
25
+ response = self.class.put(base_url(uuid), headers: required_headers, body: payload.to_json)
26
+ ApiResponse.build(response)
27
+ end
28
+
29
+ def delete(uuid)
30
+ response = self.class.delete(base_url(uuid), headers: required_headers)
31
+ return webhook_deleted_message unless response.body
32
+ RDStation::ErrorHandler.new(response).raise_errors
33
+ end
34
+
35
+ private
36
+
37
+ def webhook_deleted_message
38
+ { message: 'Webhook deleted successfuly!' }
39
+ end
40
+
41
+ def base_url(path = '')
42
+ "https://api.rd.services/integrations/webhooks/#{path}"
43
+ end
44
+
45
+ def required_headers
46
+ { 'Authorization' => "Bearer #{@auth_token}", 'Content-Type' => 'application/json' }
47
+ end
48
+ end
49
+ end
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency 'rspec'
24
24
  spec.add_development_dependency 'webmock', '~> 2.1'
25
25
  spec.add_development_dependency 'turn'
26
+ spec.add_development_dependency 'rspec_junit_formatter'
26
27
 
27
28
  spec.add_dependency "httparty", "~> 0.12"
28
29
  end
@@ -0,0 +1,243 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe RDStation::Webhooks do
4
+ let(:webhooks_client) { described_class.new('auth_token') }
5
+ let(:webhooks_endpoint) { 'https://api.rd.services/integrations/webhooks/' }
6
+
7
+ let(:headers) do
8
+ {
9
+ 'Authorization' => 'Bearer auth_token',
10
+ 'Content-Type' => 'application/json'
11
+ }
12
+ end
13
+
14
+ let(:error_handler) do
15
+ instance_double(RDStation::ErrorHandler, raise_errors: 'mock raised errors')
16
+ end
17
+
18
+ before do
19
+ allow(RDStation::ErrorHandler).to receive(:new).and_return(error_handler)
20
+ end
21
+
22
+ describe '#all' do
23
+ context 'when the request is successful' do
24
+ let(:webhooks) do
25
+ {
26
+ 'webhooks' => [
27
+ {
28
+ 'uuid' => '5408c5a3-4711-4f2e-8d0b-13407a3e30f3',
29
+ 'event_type' => 'WEBHOOK.CONVERTED',
30
+ 'entity_type' => 'CONTACT',
31
+ 'url' => 'http =>//my-url.com',
32
+ 'http_method' => 'POST',
33
+ 'include_relations' => []
34
+ },
35
+ {
36
+ 'uuid' => '642d985c-487c-4c53-b9de-2c1223841cae',
37
+ 'event_type' => 'WEBHOOK.MARKED_OPPORTUNITY',
38
+ 'entity_type' => 'CONTACT',
39
+ 'url' => 'http://my-url.com',
40
+ 'http_method' => 'POST',
41
+ 'include_relations' => %w[COMPANY CONTACT_FUNNEL]
42
+ }
43
+ ]
44
+ }
45
+ end
46
+
47
+ before do
48
+ stub_request(:get, webhooks_endpoint)
49
+ .with(headers: headers)
50
+ .to_return(status: 200, body: webhooks.to_json)
51
+ end
52
+
53
+ it 'returns all webhooks' do
54
+ response = webhooks_client.all
55
+ expect(response).to eq(webhooks)
56
+ end
57
+ end
58
+
59
+ context 'when the response contains errors' do
60
+ before do
61
+ stub_request(:get, webhooks_endpoint)
62
+ .with(headers: headers)
63
+ .to_return(status: 400, body: { 'errors' => ['all errors'] }.to_json)
64
+ end
65
+
66
+ it 'calls raise_errors on error handler' do
67
+ webhooks_client.all
68
+ expect(error_handler).to have_received(:raise_errors)
69
+ end
70
+ end
71
+ end
72
+
73
+ describe '#by_uuid' do
74
+ let(:uuid) { '5408c5a3-4711-4f2e-8d0b-13407a3e30f3' }
75
+ let(:webhooks_endpoint_by_uuid) { webhooks_endpoint + uuid }
76
+
77
+ context 'when the request is successful' do
78
+ let(:webhook) do
79
+ {
80
+ 'uuid' => uuid,
81
+ 'event_type' => 'WEBHOOK.CONVERTED',
82
+ 'entity_type' => 'CONTACT',
83
+ 'url' => 'http =>//my-url.com',
84
+ 'http_method' => 'POST',
85
+ 'include_relations' => []
86
+ }
87
+ end
88
+
89
+ before do
90
+ stub_request(:get, webhooks_endpoint_by_uuid)
91
+ .with(headers: headers)
92
+ .to_return(status: 200, body: webhook.to_json)
93
+ end
94
+
95
+ it 'returns the webhook' do
96
+ response = webhooks_client.by_uuid(uuid)
97
+ expect(response).to eq(webhook)
98
+ end
99
+ end
100
+
101
+ context 'when the response contains errors' do
102
+ before do
103
+ stub_request(:get, webhooks_endpoint_by_uuid)
104
+ .with(headers: headers)
105
+ .to_return(status: 400, body: { 'errors' => ['all errors'] }.to_json)
106
+ end
107
+
108
+ it 'calls raise_errors on error handler' do
109
+ webhooks_client.by_uuid(uuid)
110
+ expect(error_handler).to have_received(:raise_errors)
111
+ end
112
+ end
113
+ end
114
+
115
+ describe '#create' do
116
+ let(:payload) do
117
+ {
118
+ 'entity_type' => 'CONTACT',
119
+ 'event_type' => 'WEBHOOK.CONVERTED',
120
+ 'url' => 'http://my-url.com',
121
+ 'http_method' => 'POST',
122
+ 'include_relations' => %w[COMPANY CONTACT_FUNNEL]
123
+ }
124
+ end
125
+
126
+ context 'when the request is successful' do
127
+ let(:webhook) do
128
+ {
129
+ 'uuid' => '5408c5a3-4711-4f2e-8d0b-13407a3e30f3',
130
+ 'event_type' => 'WEBHOOK.CONVERTED',
131
+ 'entity_type' => 'CONTACT',
132
+ 'url' => 'http =>//my-url.com',
133
+ 'http_method' => 'POST',
134
+ 'include_relations' => %w[COMPANY CONTACT_FUNNEL]
135
+ }
136
+ end
137
+
138
+ before do
139
+ stub_request(:post, webhooks_endpoint)
140
+ .with(headers: headers, body: payload.to_json)
141
+ .to_return(status: 200, body: webhook.to_json)
142
+ end
143
+
144
+ it 'returns the webhook' do
145
+ response = webhooks_client.create(payload)
146
+ expect(response).to eq(webhook)
147
+ end
148
+ end
149
+
150
+ context 'when the response contains errors' do
151
+ before do
152
+ stub_request(:post, webhooks_endpoint)
153
+ .with(headers: headers)
154
+ .to_return(status: 400, body: { 'errors' => ['all errors'] }.to_json)
155
+ end
156
+
157
+ it 'calls raise_errors on error handler' do
158
+ webhooks_client.create(payload)
159
+ expect(error_handler).to have_received(:raise_errors)
160
+ end
161
+ end
162
+ end
163
+
164
+ describe '#update' do
165
+ let(:uuid) { '5408c5a3-4711-4f2e-8d0b-13407a3e30f3' }
166
+ let(:webhooks_endpoint_by_uuid) { webhooks_endpoint + uuid }
167
+ let(:new_payload) do
168
+ {
169
+ 'entity_type' => 'CONTACT',
170
+ 'event_type' => 'WEBHOOK.MARKED_OPPORTUNITY',
171
+ 'url' => 'http://my-new-url.com',
172
+ 'http_method' => 'POST',
173
+ 'include_relations' => %w[CONTACT_FUNNEL]
174
+ }
175
+ end
176
+
177
+ context 'when the request is successful' do
178
+ let(:updated_webhook) do
179
+ {
180
+ 'uuid' => uuid,
181
+ 'event_type' => 'WEBHOOK.MARKED_OPPORTUNITY',
182
+ 'entity_type' => 'CONTACT',
183
+ 'url' => 'http://my-new-url.com',
184
+ 'http_method' => 'POST',
185
+ 'include_relations' => %w[CONTACT_FUNNEL]
186
+ }
187
+ end
188
+
189
+ before do
190
+ stub_request(:put, webhooks_endpoint_by_uuid)
191
+ .with(headers: headers, body: new_payload.to_json)
192
+ .to_return(status: 200, body: updated_webhook.to_json)
193
+ end
194
+
195
+ it 'returns the webhook' do
196
+ response = webhooks_client.update(uuid, new_payload)
197
+ expect(response).to eq(updated_webhook)
198
+ end
199
+ end
200
+
201
+ context 'when the response contains errors' do
202
+ before do
203
+ stub_request(:put, webhooks_endpoint_by_uuid)
204
+ .with(headers: headers)
205
+ .to_return(status: 400, body: { 'errors' => ['all errors'] }.to_json)
206
+ end
207
+
208
+ it 'calls raise_errors on error handler' do
209
+ webhooks_client.update(uuid, new_payload)
210
+ expect(error_handler).to have_received(:raise_errors)
211
+ end
212
+ end
213
+ end
214
+
215
+ describe '#delete' do
216
+ let(:uuid) { '5408c5a3-4711-4f2e-8d0b-13407a3e30f3' }
217
+ let(:webhooks_endpoint_by_uuid) { webhooks_endpoint + uuid }
218
+
219
+ context 'when the request is successful' do
220
+ before do
221
+ stub_request(:delete, webhooks_endpoint_by_uuid).with(headers: headers).to_return(status: 204)
222
+ end
223
+
224
+ it 'returns the webhook' do
225
+ response = webhooks_client.delete(uuid)
226
+ expect(response).to eq(message: 'Webhook deleted successfuly!')
227
+ end
228
+ end
229
+
230
+ context 'when the response contains errors' do
231
+ before do
232
+ stub_request(:delete, webhooks_endpoint_by_uuid)
233
+ .with(headers: headers)
234
+ .to_return(status: 400, body: { 'errors' => ['all errors'] }.to_json)
235
+ end
236
+
237
+ it 'calls raise_errors on error handler' do
238
+ webhooks_client.delete(uuid)
239
+ expect(error_handler).to have_received(:raise_errors)
240
+ end
241
+ end
242
+ end
243
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdstation-ruby-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo L F Casaretto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-05 00:00:00.000000000 Z
11
+ date: 2018-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec_junit_formatter
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: httparty
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -101,6 +115,7 @@ executables: []
101
115
  extensions: []
102
116
  extra_rdoc_files: []
103
117
  files:
118
+ - ".circleci/config.yml"
104
119
  - ".gitignore"
105
120
  - Gemfile
106
121
  - Gemfile.lock
@@ -108,6 +123,7 @@ files:
108
123
  - README.md
109
124
  - Rakefile
110
125
  - lib/rdstation-ruby-client.rb
126
+ - lib/rdstation/api_response.rb
111
127
  - lib/rdstation/authentication.rb
112
128
  - lib/rdstation/client.rb
113
129
  - lib/rdstation/contacts.rb
@@ -126,6 +142,7 @@ files:
126
142
  - lib/rdstation/events.rb
127
143
  - lib/rdstation/fields.rb
128
144
  - lib/rdstation/version.rb
145
+ - lib/rdstation/webhooks.rb
129
146
  - rdstation-ruby-client.gemspec
130
147
  - spec/lib/rdstation-ruby-client_spec.rb
131
148
  - spec/lib/rdstation/authentication_spec.rb
@@ -144,6 +161,7 @@ files:
144
161
  - spec/lib/rdstation/error_spec.rb
145
162
  - spec/lib/rdstation/events_spec.rb
146
163
  - spec/lib/rdstation/fields_spec.rb
164
+ - spec/lib/rdstation/webhooks_spec.rb
147
165
  - spec/spec_helper.rb
148
166
  homepage: http://resultadosdigitais.com.br
149
167
  licenses:
@@ -187,4 +205,5 @@ test_files:
187
205
  - spec/lib/rdstation/error_spec.rb
188
206
  - spec/lib/rdstation/events_spec.rb
189
207
  - spec/lib/rdstation/fields_spec.rb
208
+ - spec/lib/rdstation/webhooks_spec.rb
190
209
  - spec/spec_helper.rb