cta_aggregator_client 0.1.0 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 409f45de3418b9c9b28d8658dba105cc036f1173
4
- data.tar.gz: caff1471ad88c925a46d1ab2037613bd1f435eab
3
+ metadata.gz: '0296b1a6b5e845c92ff5e651a0845f9e414ebc53'
4
+ data.tar.gz: ac44dbb3526e56a04b642a0fdfc298ab2af27f4b
5
5
  SHA512:
6
- metadata.gz: 611d21df72ce9c2c93aedf0aeb9aeb3fb210d7af405882b89c0abc0d69c592eb7a1802b0dc748f8bc768a83476324d48eb25ba4ed9120c37dedc5e5401f8a40f
7
- data.tar.gz: eb92d66882035b642d7beba93572aad7607dff1c0175762686a83b4a9b0825c9ee1b8e4f2508dc77562711df19f88291ff751e78df294c23e0aab81d17ee8bc5
6
+ metadata.gz: 4a323263b16f7661e6398968bcebdc945936807efb20bd30f71a60d24fea94bf529f2e63d73798ea6c49a9c69349881b4fa117f1ebbb023b563577cec4a30860
7
+ data.tar.gz: c32e4d4ce6caedcf35a50b50219215fc0739eabbd700980c1fe75fe412419473b354181dd09d406124c6a469110a2fef8dd6b7074384e772e40ad43dd0f7d72a
@@ -1,10 +1,10 @@
1
1
  require 'cta_aggregator_client/cta_resource'
2
-
3
2
  module CTAAggregatorClient
4
3
  module AdvocacyCampaign
5
4
  extend CTAResource
6
5
 
7
6
  RESOURCE_NAME = :advocacy_campaign
7
+ RELATIONSHIPS = :targets
8
8
 
9
9
  class << self
10
10
 
@@ -12,6 +12,10 @@ module CTAAggregatorClient
12
12
  RESOURCE_NAME
13
13
  end
14
14
 
15
+ def relationships
16
+ RELATIONSHIPS
17
+ end
18
+
15
19
  end
16
20
  end
17
21
  end
@@ -36,8 +36,28 @@ module CTAAggregatorClient
36
36
  perform_authenticated_request(:post, url, payload)
37
37
  end
38
38
 
39
+ def update(resource_name, uuid, attributes)
40
+ url = "#{base_url}/#{api_version}/#{resource_name}s/#{uuid}"
41
+ payload = {
42
+ 'data': {
43
+ 'type': "#{resource_name}s",
44
+ 'id': uuid,
45
+ 'attributes': attributes
46
+ }
47
+ }.to_json
48
+
49
+ perform_authenticated_request(:put, url, payload)
50
+ end
51
+
52
+ def delete(resource_name, uuid)
53
+ url = "#{base_url}/#{api_version}/#{resource_name}s/#{uuid}"
54
+
55
+ raw_response = RestClient.delete(url, headers_with_access_token)
56
+ translate_response(raw_response)
57
+ end
58
+
39
59
  def relationship_params(relationships)
40
- return {} unless relationships
60
+ return {} unless relationships && relationships.reject { |k,v| v.nil? }.any?
41
61
 
42
62
  relationships.each_with_object({}) do |(resource_name, uuid_data), obj|
43
63
  if uuid_data.is_a? Array
@@ -73,7 +93,7 @@ module CTAAggregatorClient
73
93
  perform_authenticated_request(:put, url, payload)
74
94
  end
75
95
 
76
- def perform_authenticated_request(http_method, url, payload)
96
+ def perform_authenticated_request(http_method, url, payload={})
77
97
  raw_response = RestClient.send(http_method.to_sym, url, payload, headers_with_access_token)
78
98
  translate_response(raw_response)
79
99
  end
@@ -20,7 +20,7 @@ module CTAAggregatorClient
20
20
  end
21
21
 
22
22
  def auth_url
23
- "#{base_url}/#{api_version}/authorize"
23
+ "#{base_url}/#{api_version}/authentications"
24
24
  end
25
25
 
26
26
  def api_key
@@ -11,13 +11,30 @@ module CTAAggregatorClient
11
11
  API::Client.find(resource_name, uuid)
12
12
  end
13
13
 
14
- def create(attributes, relationships = {})
15
- API::Client.create(resource_name, attributes, relationships)
14
+ def create(attributes)
15
+ if relationships
16
+ relationship_data = { relationships => attributes.delete(relationships) }
17
+ end
18
+
19
+ API::Client.create(resource_name, attributes, relationship_data)
20
+ end
21
+
22
+ def update(attributes)
23
+ uuid = attributes.delete(:id)
24
+ API::Client.update(resource_name, uuid, attributes)
25
+ end
26
+
27
+ def delete(uuid)
28
+ API::Client.delete(resource_name, uuid)
16
29
  end
17
30
 
18
31
  def resource_name
19
32
  raise NotImplementedError
20
33
  end
21
34
 
35
+ def relationships
36
+ nil
37
+ end
38
+
22
39
  end
23
40
  end
@@ -5,6 +5,7 @@ module CTAAggregatorClient
5
5
  extend CTAResource
6
6
 
7
7
  RESOURCE_NAME = :event
8
+ RELATIONSHIPS = :location
8
9
 
9
10
  class << self
10
11
 
@@ -12,6 +13,10 @@ module CTAAggregatorClient
12
13
  RESOURCE_NAME
13
14
  end
14
15
 
16
+ def relationships
17
+ RELATIONSHIPS
18
+ end
19
+
15
20
  end
16
21
  end
17
22
  end
@@ -1,3 +1,3 @@
1
1
  module CTAAggregatorClient
2
- VERSION = '0.1.0'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -11,7 +11,7 @@ module TestHelpers
11
11
  end
12
12
 
13
13
  def auth_url
14
- "#{api_url}/authorize"
14
+ "#{api_url}/authentications"
15
15
  end
16
16
 
17
17
  def headers_with_auth_creds
@@ -4,6 +4,8 @@ describe CTAAggregatorClient::AdvocacyCampaign do
4
4
 
5
5
  it_behaves_like 'listable resource'
6
6
  it_behaves_like 'findable resource'
7
+ it_behaves_like 'updatable resource'
8
+ it_behaves_like 'deletable resource'
7
9
 
8
10
  context 'creation' do
9
11
  campaign_attrs = {
@@ -14,9 +16,9 @@ describe CTAAggregatorClient::AdvocacyCampaign do
14
16
  featured_image_url: 'http://lorempixel.com/300/300',
15
17
  action_type: 'email',
16
18
  template: 'Eum itaque et nisi dolores assumenda ipsum. Voluptates qui aut nobis veniam maxime qui. Illum saepe eum corporis vero qui soluta aliquam.\nTemporibus reiciendis velit fugiat. Facere laborum quia ea laboriosam necessitatibus quisquam eligendi. Et est dolorem eligendi id aut sint.\nAnimi qui totam voluptatem nesciunt iure et dolorem. Eos ut dolorum et quo illum fuga atque. Facere a ipsa corrupti assumenda provident commodi facilis. Eligendi officia est et.',
19
+ targets: ['a2f6f86b-a214-4892-8c06-8caece820fb0', '215ed993-3cd1-4fbc-b8af-7e2082813d06']
17
20
  }
18
- relationship_attrs = { targets: ['a2f6f86b-a214-4892-8c06-8caece820fb0', '215ed993-3cd1-4fbc-b8af-7e2082813d06'] }
19
21
 
20
- it_behaves_like 'creatable resource', campaign_attrs, relationship_attrs
22
+ it_behaves_like 'creatable resource', campaign_attrs, :targets
21
23
  end
22
24
  end
@@ -4,6 +4,9 @@ describe CTAAggregatorClient::Event do
4
4
 
5
5
  it_behaves_like 'listable resource'
6
6
  it_behaves_like 'findable resource'
7
+ it_behaves_like 'updatable resource'
8
+ it_behaves_like 'deletable resource'
9
+
7
10
  context 'creation' do
8
11
  event_attrs = {
9
12
  title: 'March on Washington',
@@ -14,10 +17,10 @@ describe CTAAggregatorClient::Event do
14
17
  start_date: '2017-07-08T03:58:25.098Z',
15
18
  end_date: '2017-07-13T03:58:25.098Z',
16
19
  free: false,
20
+ location: '215ed993-3cd1-4fbc-b8af-7e2082813d06'
17
21
  }
18
- relationship_attrs = { location: '215ed993-3cd1-4fbc-b8af-7e2082813d06' }
19
22
 
20
- it_behaves_like 'creatable resource', event_attrs, relationship_attrs
23
+ it_behaves_like 'creatable resource', event_attrs, :location
21
24
  end
22
25
 
23
26
  end
@@ -4,6 +4,9 @@ describe CTAAggregatorClient::Location do
4
4
 
5
5
  it_behaves_like 'listable resource'
6
6
  it_behaves_like 'findable resource'
7
+ it_behaves_like 'updatable resource'
8
+ it_behaves_like 'deletable resource'
9
+
7
10
  context 'creation' do
8
11
  attributes = {
9
12
  venue: 'Eastern Kemmer University',
@@ -4,6 +4,8 @@ describe CTAAggregatorClient::Target do
4
4
 
5
5
  it_behaves_like 'listable resource'
6
6
  it_behaves_like 'findable resource'
7
+ it_behaves_like 'updatable resource'
8
+ it_behaves_like 'deletable resource'
7
9
 
8
10
  context 'creation' do
9
11
  attributes = {
@@ -4,7 +4,8 @@ shared_examples_for "creatable resource" do |passed_attrs, passed_relationships,
4
4
  let(:token) { Factory.token }
5
5
 
6
6
  def relationships(relationship_hash)
7
- return {} unless relationship_hash
7
+ return {} unless relationship_hash && relationship_hash.reject { |k,v| v.nil? }.any?
8
+
8
9
  relationship_hash.each_with_object({}) do |(resource_name, uuid_data), obj|
9
10
  if uuid_data.is_a? Array
10
11
  obj[resource_name.to_sym] = {
@@ -28,12 +29,14 @@ shared_examples_for "creatable resource" do |passed_attrs, passed_relationships,
28
29
  resource_name = formatted_resource_name(described_class, resource)
29
30
 
30
31
  url = "#{api_url}/#{resource_name}"
32
+ payload_params = passed_attrs.clone
33
+ relationship_data = { passed_relationships => payload_params.delete(passed_relationships) }
31
34
 
32
35
  payload = {
33
36
  'data': {
34
37
  'type': resource_name,
35
- 'attributes': passed_attrs,
36
- 'relationships': relationships(passed_relationships)
38
+ 'attributes': payload_params,
39
+ 'relationships': relationships(relationship_data)
37
40
  }.reject{ |k,v| v.empty? }
38
41
  }
39
42
 
@@ -53,7 +56,7 @@ shared_examples_for "creatable resource" do |passed_attrs, passed_relationships,
53
56
  headers_with_auth_token
54
57
  ).and_return(response)
55
58
 
56
- expect(described_class.create(passed_attrs, passed_relationships)).to eq response
59
+ expect(described_class.create(passed_attrs)).to eq response
57
60
  end
58
61
  end
59
62
 
@@ -73,11 +76,14 @@ shared_examples_for "creatable resource" do |passed_attrs, passed_relationships,
73
76
  resource_name = formatted_resource_name(described_class, resource)
74
77
 
75
78
  url = "#{api_url}/#{resource_name}"
79
+ payload_params = passed_attrs.clone
80
+ relationship_data = { passed_relationships => payload_params.delete(passed_relationships) }
81
+
76
82
  payload = {
77
83
  'data': {
78
84
  'type': resource_name,
79
- 'attributes': passed_attrs,
80
- 'relationships': relationships(passed_relationships)
85
+ 'attributes': payload_params,
86
+ 'relationships': relationships(relationship_data)
81
87
  }.reject{ |k,v| v.empty? }
82
88
  }
83
89
 
@@ -101,7 +107,7 @@ shared_examples_for "creatable resource" do |passed_attrs, passed_relationships,
101
107
  header_sans_auth_token
102
108
  ).and_return(Factory.bad_token_response)
103
109
 
104
- expect(described_class.create(passed_attrs, passed_relationships).code).to eq 401
110
+ expect(described_class.create(passed_attrs).code).to eq 401
105
111
  end
106
112
  end
107
113
 
@@ -0,0 +1,31 @@
1
+ shared_examples_for "deletable resource" do |resource|
2
+ let(:response) { Factory.response }
3
+ let(:auth_response) { Factory.auth_response }
4
+ let(:token) { Factory.token }
5
+ let(:uuid) { '123' }
6
+
7
+ after do
8
+ CTAAggregatorClient::API::Authenticator.reset_auth
9
+ end
10
+
11
+ it 'updates record' do
12
+ url = "#{api_url}/#{formatted_resource_name(described_class, resource)}/#{uuid}"
13
+
14
+ allow(RestClient).to receive(:post).with(
15
+ auth_url,
16
+ nil,
17
+ headers_with_auth_creds
18
+ ).and_return(auth_response)
19
+
20
+ headers_with_auth_token = CTAAggregatorClient::API::Client.default_headers.merge(
21
+ authorization: "Bearer: #{token}"
22
+ )
23
+
24
+ expect(RestClient).to receive(:delete).with(
25
+ url,
26
+ headers_with_auth_token
27
+ ).and_return(response)
28
+
29
+ expect(described_class.delete(uuid)).to eq response
30
+ end
31
+ end
@@ -0,0 +1,42 @@
1
+ shared_examples_for "updatable resource" do |resource|
2
+ let(:response) { Factory.response }
3
+ let(:auth_response) { Factory.auth_response }
4
+ let(:token) { Factory.token }
5
+ let(:uuid) { '123' }
6
+ let(:attrs_to_change) { {foo: :bar} }
7
+ let(:attrs) { {id: uuid}.merge(attrs_to_change) }
8
+
9
+ after do
10
+ CTAAggregatorClient::API::Authenticator.reset_auth
11
+ end
12
+
13
+ it 'updates record' do
14
+ url = "#{api_url}/#{formatted_resource_name(described_class, resource)}/#{uuid}"
15
+
16
+ payload = {
17
+ 'data': {
18
+ 'type': formatted_resource_name(described_class, resource),
19
+ 'id': uuid,
20
+ 'attributes': attrs_to_change,
21
+ }
22
+ }
23
+
24
+ allow(RestClient).to receive(:post).with(
25
+ auth_url,
26
+ nil,
27
+ headers_with_auth_creds
28
+ ).and_return(auth_response)
29
+
30
+ headers_with_auth_token = CTAAggregatorClient::API::Client.default_headers.merge(
31
+ authorization: "Bearer: #{token}"
32
+ )
33
+
34
+ expect(RestClient).to receive(:put).with(
35
+ url,
36
+ payload.to_json,
37
+ headers_with_auth_token
38
+ ).and_return(response)
39
+
40
+ expect(described_class.update(attrs)).to eq response
41
+ end
42
+ end
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,8 @@ require 'cta_aggregator_client'
2
2
  require 'shared_examples/listable_resource'
3
3
  require 'shared_examples/findable_resource'
4
4
  require 'shared_examples/creatable_resource'
5
+ require 'shared_examples/updatable_resource'
6
+ require 'shared_examples/deletable_resource'
5
7
  require 'helpers/test_helpers'
6
8
  require 'factory'
7
9
  require 'pry'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cta_aggregator_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Downey
@@ -77,8 +77,10 @@ files:
77
77
  - spec/integration/location_spec.rb
78
78
  - spec/integration/target_spec.rb
79
79
  - spec/shared_examples/creatable_resource.rb
80
+ - spec/shared_examples/deletable_resource.rb
80
81
  - spec/shared_examples/findable_resource.rb
81
82
  - spec/shared_examples/listable_resource.rb
83
+ - spec/shared_examples/updatable_resource.rb
82
84
  - spec/spec_helper.rb
83
85
  homepage: https://github.com/Ragtagteam/cta-aggregator-client-ruby
84
86
  licenses:
@@ -112,6 +114,8 @@ test_files:
112
114
  - spec/integration/location_spec.rb
113
115
  - spec/integration/target_spec.rb
114
116
  - spec/shared_examples/creatable_resource.rb
117
+ - spec/shared_examples/deletable_resource.rb
115
118
  - spec/shared_examples/findable_resource.rb
116
119
  - spec/shared_examples/listable_resource.rb
120
+ - spec/shared_examples/updatable_resource.rb
117
121
  - spec/spec_helper.rb