action_kit_rest 0.4.0 → 0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +26 -0
- data/.ruby-version +1 -1
- data/.travis.yml +7 -1
- data/Gemfile +11 -9
- data/Rakefile +15 -18
- data/action_kit_rest.gemspec +39 -23
- data/lib/action_kit_rest.rb +2 -0
- data/lib/action_kit_rest/action.rb +3 -1
- data/lib/action_kit_rest/actions/event_create_action.rb +5 -3
- data/lib/action_kit_rest/actions/event_signup_action.rb +6 -3
- data/lib/action_kit_rest/actions/unsubscribe_action.rb +3 -1
- data/lib/action_kit_rest/allowed_user_field.rb +10 -0
- data/lib/action_kit_rest/api.rb +9 -7
- data/lib/action_kit_rest/base.rb +5 -4
- data/lib/action_kit_rest/client.rb +5 -3
- data/lib/action_kit_rest/event.rb +3 -1
- data/lib/action_kit_rest/event_signup.rb +3 -1
- data/lib/action_kit_rest/language.rb +3 -1
- data/lib/action_kit_rest/list.rb +3 -1
- data/lib/action_kit_rest/page.rb +4 -2
- data/lib/action_kit_rest/pages/base.rb +4 -5
- data/lib/action_kit_rest/pages/donation_page.rb +3 -1
- data/lib/action_kit_rest/pages/event_campaign_page.rb +4 -2
- data/lib/action_kit_rest/pages/import_page.rb +3 -1
- data/lib/action_kit_rest/pages/signup_page.rb +3 -1
- data/lib/action_kit_rest/pages/unsubscribe_page.rb +3 -1
- data/lib/action_kit_rest/phone.rb +2 -0
- data/lib/action_kit_rest/railties.rb +3 -1
- data/lib/action_kit_rest/response/collection.rb +4 -4
- data/lib/action_kit_rest/response/raise_error.rb +15 -12
- data/lib/action_kit_rest/response/validation_error.rb +7 -0
- data/lib/action_kit_rest/response/wrapper.rb +10 -10
- data/lib/action_kit_rest/tag.rb +6 -6
- data/lib/action_kit_rest/user.rb +3 -1
- data/lib/action_kit_rest/version.rb +3 -1
- data/spec/fixtures/allowed_user_field/get.json +19 -0
- data/spec/lib/action_kit_rest/actions/event_create_action_spec.rb +6 -4
- data/spec/lib/action_kit_rest/actions/event_signup_action_spec.rb +6 -4
- data/spec/lib/action_kit_rest/allowed_user_field_spec.rb +43 -5
- data/spec/lib/action_kit_rest/logger_spec.rb +7 -5
- data/spec/lib/action_kit_rest/page_spec.rb +28 -28
- data/spec/lib/action_kit_rest/pages/event_campaign_page_spec.rb +25 -20
- data/spec/lib/action_kit_rest/pages/import_page_spec.rb +21 -17
- data/spec/lib/action_kit_rest/response/collection_spec.rb +13 -11
- data/spec/lib/action_kit_rest/response/validation_error_spec.rb +29 -0
- data/spec/lib/action_kit_rest/response/wrapper_spec.rb +6 -4
- data/spec/lib/action_kit_rest/user_spec.rb +11 -9
- data/spec/spec_helper.rb +6 -4
- data/spec/support/shared_contexts/stub_logger.rb +4 -2
- metadata +64 -59
data/lib/action_kit_rest/list.rb
CHANGED
data/lib/action_kit_rest/page.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActionKitRest
|
2
4
|
module Pages
|
3
5
|
class Base < ActionKitRest::Base
|
@@ -6,15 +8,12 @@ module ActionKitRest
|
|
6
8
|
response = list(name: name)
|
7
9
|
response.obj.first
|
8
10
|
end
|
9
|
-
|
11
|
+
|
10
12
|
def find_or_create(params)
|
11
13
|
page = find(params[:name])
|
12
|
-
if page.blank?
|
13
|
-
page = create(params)
|
14
|
-
end
|
14
|
+
page = create(params) if page.blank?
|
15
15
|
page
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
20
|
-
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActionKitRest
|
2
4
|
module Pages
|
3
5
|
class EventCampaignPage < ActionKitRest::Pages::Base
|
@@ -41,7 +43,7 @@ module ActionKitRest
|
|
41
43
|
|
42
44
|
def create_event_create_page(event_campaign, tags)
|
43
45
|
params = event_create_page_params(event_campaign, tags)
|
44
|
-
|
46
|
+
_response = client.post_json_request('eventcreatepage/', params)
|
45
47
|
|
46
48
|
params[:name]
|
47
49
|
end
|
@@ -57,7 +59,7 @@ module ActionKitRest
|
|
57
59
|
|
58
60
|
def create_event_signup_page(event_campaign, tags)
|
59
61
|
params = event_signup_page_params(event_campaign, tags)
|
60
|
-
|
62
|
+
_response = client.post_json_request('eventsignuppage/', params)
|
61
63
|
|
62
64
|
params[:name]
|
63
65
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# A class responsible for proxing to faraday response &
|
2
4
|
# or a pagination collection.
|
3
5
|
module ActionKitRest
|
@@ -5,9 +7,7 @@ module ActionKitRest
|
|
5
7
|
class Collection
|
6
8
|
include Enumerable
|
7
9
|
|
8
|
-
attr_reader :meta
|
9
|
-
attr_reader :objects
|
10
|
-
|
10
|
+
attr_reader :meta, :objects
|
11
11
|
|
12
12
|
def initialize(meta, objects)
|
13
13
|
@meta = meta
|
@@ -15,7 +15,7 @@ module ActionKitRest
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def each(&block)
|
18
|
-
#
|
18
|
+
# TODO: handle pagination somehow!
|
19
19
|
objects.each do |o|
|
20
20
|
block.call(o)
|
21
21
|
end
|
@@ -1,43 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActionKitRest
|
2
4
|
module Response
|
3
5
|
class RaiseError < Faraday::Response::Middleware
|
4
|
-
|
5
6
|
def on_complete(response)
|
6
7
|
status_code = response[:status].to_i
|
7
8
|
if (400...600).include? status_code
|
8
9
|
if status_code == 400
|
9
10
|
response_body = response[:body]
|
10
|
-
if JSON.parse(response_body)['errors'] == {'mailing_id' => ['Unable to associate this mailing ID with account.']}
|
11
|
+
if JSON.parse(response_body)['errors'] == { 'mailing_id' => ['Unable to associate this mailing ID with account.'] }
|
11
12
|
raise ActionKitRest::Response::InvalidAkidError.new(url: response[:url].to_s, body: response_body)
|
12
13
|
else
|
13
14
|
raise ActionKitRest::Response::ValidationError.new(url: response[:url].to_s, body: response_body)
|
14
15
|
end
|
15
16
|
elsif status_code == 404
|
16
|
-
raise ActionKitRest::Response::NotFound
|
17
|
+
raise ActionKitRest::Response::NotFound, response[:url].to_s
|
17
18
|
elsif status_code == 401
|
18
|
-
raise ActionKitRest::Response::Unauthorized
|
19
|
-
elsif status_code == 500 && response[:body] =~
|
19
|
+
raise ActionKitRest::Response::Unauthorized, response[:url].to_s
|
20
|
+
elsif status_code == 500 && response[:body] =~ /"error"/
|
20
21
|
error_hsh = JSON.parse(response[:body])
|
21
22
|
error_message = error_hsh['error']
|
22
23
|
|
23
24
|
if error_message == 'Sorry, this request could not be processed. Please try again later.'
|
24
|
-
raise ActionKitRest::Response::TryAgainLater
|
25
|
+
raise ActionKitRest::Response::TryAgainLater, error_message(response)
|
25
26
|
else
|
26
|
-
raise StandardError
|
27
|
+
raise StandardError, error_message(response)
|
27
28
|
end
|
28
29
|
else
|
29
|
-
raise StandardError
|
30
|
+
raise StandardError, error_message(response)
|
30
31
|
end
|
31
32
|
end
|
32
33
|
end
|
33
34
|
|
34
35
|
def error_message(response)
|
35
|
-
"#{response[:method].to_s.upcase} #{response[:url]
|
36
|
+
"#{response[:method].to_s.upcase} #{response[:url]}: #{response[:status]} \n\n #{response[:body]}"
|
36
37
|
end
|
37
38
|
end
|
38
39
|
|
39
|
-
class TryAgainLater < StandardError
|
40
|
-
|
41
|
-
class
|
40
|
+
class TryAgainLater < StandardError; end
|
41
|
+
|
42
|
+
class NotFound < StandardError; end
|
43
|
+
|
44
|
+
class Unauthorized < StandardError; end
|
42
45
|
end
|
43
46
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActionKitRest
|
2
4
|
module Response
|
3
5
|
class ValidationError < StandardError
|
@@ -12,6 +14,11 @@ module ActionKitRest
|
|
12
14
|
|
13
15
|
def to_s
|
14
16
|
"#{super()} \n url: #{url} \n body: #{body} \n errors: #{errors}"
|
17
|
+
rescue Encoding::CompatibilityError
|
18
|
+
# Something went gravely wrong trying to construct the error message, so give up on the extra info
|
19
|
+
# and just raise the name of the exception.
|
20
|
+
# This will let us at least raise with a backtrace.
|
21
|
+
super
|
15
22
|
end
|
16
23
|
end
|
17
24
|
|
@@ -1,17 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActionKitRest
|
2
4
|
module Response
|
3
5
|
class Wrapper
|
4
6
|
include Enumerable
|
5
7
|
extend Forwardable
|
6
8
|
|
7
|
-
attr_reader :response
|
8
|
-
attr_reader :kind
|
9
|
-
attr_reader :obj
|
9
|
+
attr_reader :response, :kind, :obj
|
10
10
|
|
11
11
|
def_delegators :body, :empty?, :size, :include?, :length, :to_a, :first, :flatten, :include?, :keys, :[]
|
12
12
|
|
13
13
|
def initialize(response)
|
14
|
-
@response
|
14
|
+
@response = response
|
15
15
|
|
16
16
|
if response.body.respond_to?(:meta) && response.body.meta
|
17
17
|
@kind = :collection
|
@@ -42,7 +42,7 @@ module ActionKitRest
|
|
42
42
|
|
43
43
|
# Response raw body
|
44
44
|
def body
|
45
|
-
@body
|
45
|
+
@body || response.body
|
46
46
|
end
|
47
47
|
|
48
48
|
# Response status
|
@@ -70,10 +70,10 @@ module ActionKitRest
|
|
70
70
|
# Convert any key to string before calling.
|
71
71
|
#
|
72
72
|
def [](key)
|
73
|
-
if
|
74
|
-
|
73
|
+
if body.is_a?(Array)
|
74
|
+
body[key]
|
75
75
|
else
|
76
|
-
|
76
|
+
body.send(:"#{key}")
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
@@ -112,9 +112,9 @@ module ActionKitRest
|
|
112
112
|
block.call(o)
|
113
113
|
end
|
114
114
|
else
|
115
|
-
raise(
|
115
|
+
raise('can only iterate over collections')
|
116
116
|
end
|
117
117
|
end
|
118
118
|
end
|
119
119
|
end
|
120
|
-
end
|
120
|
+
end
|
data/lib/action_kit_rest/tag.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActionKitRest
|
2
4
|
class Tag < Base
|
3
5
|
def base_path
|
4
6
|
'tag'
|
5
7
|
end
|
6
|
-
|
8
|
+
|
7
9
|
def find(name)
|
8
10
|
response = list(name: name)
|
9
11
|
response.obj.first
|
10
12
|
end
|
11
|
-
|
13
|
+
|
12
14
|
def find_or_create(name)
|
13
15
|
tag = find(name)
|
14
|
-
if tag.blank?
|
15
|
-
tag = create(name: name)
|
16
|
-
end
|
16
|
+
tag = create(name: name) if tag.blank?
|
17
17
|
tag
|
18
18
|
end
|
19
19
|
end
|
20
|
-
end
|
20
|
+
end
|
data/lib/action_kit_rest/user.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
{
|
2
|
+
"allow_multiple": false,
|
3
|
+
"always_show": false,
|
4
|
+
"choices": [],
|
5
|
+
"created_at": "2016-10-19T01:39:35",
|
6
|
+
"description": "",
|
7
|
+
"display_name": "Foo",
|
8
|
+
"field_choices": "",
|
9
|
+
"field_default": "",
|
10
|
+
"field_length": null,
|
11
|
+
"field_regex": "",
|
12
|
+
"field_type": "string",
|
13
|
+
"hidden": false,
|
14
|
+
"name": "foo",
|
15
|
+
"order_index": 0,
|
16
|
+
"required": false,
|
17
|
+
"resource_uri": "/rest/v1/alloweduserfield/age/",
|
18
|
+
"updated_at": "2016-10-19T01:39:35"
|
19
|
+
}
|
@@ -1,18 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe ActionKitRest::Actions::EventCreateAction do
|
4
6
|
describe '#get' do
|
5
|
-
let(:actionkit) { ActionKitRest.new(host: 'test.com')
|
7
|
+
let(:actionkit) { ActionKitRest.new(host: 'test.com') }
|
6
8
|
|
7
9
|
before :each do
|
8
|
-
stub_request(:get,
|
9
|
-
.to_return(body: fixture('action/event_create_action.json'), status: '200', headers: {content_type:
|
10
|
+
stub_request(:get, 'https://test.com/rest/v1/eventcreateaction/123/')
|
11
|
+
.to_return(body: fixture('action/event_create_action.json'), status: '200', headers: { content_type: 'application/json; charset=utf-8' })
|
10
12
|
end
|
11
13
|
|
12
14
|
it 'should retrieve eventcreateaction and aggregate parsed event and user IDs' do
|
13
15
|
ak_event_create_action = actionkit.event_create_action.get('123')
|
14
16
|
|
15
|
-
expect(ak_event_create_action.id).to eq(
|
17
|
+
expect(ak_event_create_action.id).to eq(12_345)
|
16
18
|
expect(ak_event_create_action.event_id).to eq('999')
|
17
19
|
expect(ak_event_create_action.user_id).to eq('112233')
|
18
20
|
end
|