action_kit_rest 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +5 -13
  2. data/.ruby-version +1 -1
  3. data/.travis.yml +1 -1
  4. data/Gemfile +2 -3
  5. data/Gemfile.lock +78 -84
  6. data/README.md +53 -1
  7. data/Rakefile +8 -8
  8. data/VERSION +1 -1
  9. data/action_kit_rest.gemspec +65 -53
  10. data/lib/action_kit_rest.rb +10 -0
  11. data/lib/action_kit_rest/actions/event_create_action.rb +9 -0
  12. data/lib/action_kit_rest/actions/event_signup_action.rb +9 -0
  13. data/lib/action_kit_rest/actions/unsubscribe_action.rb +9 -0
  14. data/lib/action_kit_rest/api.rb +3 -4
  15. data/lib/action_kit_rest/client.rb +16 -21
  16. data/lib/action_kit_rest/event.rb +12 -0
  17. data/lib/action_kit_rest/event_signup.rb +7 -0
  18. data/lib/action_kit_rest/language.rb +12 -0
  19. data/lib/action_kit_rest/pages/donation_page.rb +9 -0
  20. data/lib/action_kit_rest/pages/event_campaign_page.rb +54 -0
  21. data/lib/action_kit_rest/pages/unsubscribe_page.rb +9 -0
  22. data/lib/action_kit_rest/phone.rb +7 -0
  23. data/lib/action_kit_rest/response/raise_error.rb +15 -2
  24. data/lib/action_kit_rest/user.rb +13 -0
  25. data/lib/action_kit_rest/version.rb +3 -3
  26. data/spec/fixtures/page/campaign.json +32 -0
  27. data/spec/fixtures/phone/object.json +33 -0
  28. data/spec/fixtures/user/{object.json → object_with_phones.json} +0 -0
  29. data/spec/fixtures/user/object_without_phones.json +1 -0
  30. data/spec/{action_kit_rest → lib/action_kit_rest}/logger_spec.rb +0 -0
  31. data/spec/{action_kit_rest → lib/action_kit_rest}/page_spec.rb +10 -2
  32. data/spec/lib/action_kit_rest/pages/event_campaign_page_spec.rb +48 -0
  33. data/spec/{action_kit_rest/pages/input_page_spec.rb → lib/action_kit_rest/pages/import_page_spec.rb} +6 -12
  34. data/spec/{action_kit_rest → lib/action_kit_rest}/response/collection_spec.rb +0 -0
  35. data/spec/{action_kit_rest → lib/action_kit_rest}/response/wrapper_spec.rb +0 -0
  36. data/spec/{action_kit_rest → lib/action_kit_rest}/user_spec.rb +17 -3
  37. data/spec/support/shared_contexts/stub_logger.rb +9 -0
  38. metadata +57 -54
@@ -16,11 +16,21 @@ require 'action_kit_rest/page'
16
16
  require 'action_kit_rest/base'
17
17
  require 'action_kit_rest/list'
18
18
  require 'action_kit_rest/action'
19
+ require 'action_kit_rest/actions/unsubscribe_action'
20
+ require 'action_kit_rest/actions/event_create_action'
21
+ require 'action_kit_rest/actions/event_signup_action'
19
22
  require 'action_kit_rest/user'
23
+ require 'action_kit_rest/phone'
24
+ require 'action_kit_rest/event'
25
+ require 'action_kit_rest/language'
20
26
  require 'action_kit_rest/tag'
27
+ require 'action_kit_rest/event_signup'
21
28
  require 'action_kit_rest/pages/base'
22
29
  require 'action_kit_rest/pages/import_page'
23
30
  require 'action_kit_rest/pages/signup_page'
31
+ require 'action_kit_rest/pages/donation_page'
32
+ require 'action_kit_rest/pages/unsubscribe_page'
33
+ require 'action_kit_rest/pages/event_campaign_page'
24
34
  require 'action_kit_rest/response/wrapper'
25
35
  require 'action_kit_rest/response/raise_error'
26
36
  require 'action_kit_rest/response/validation_error'
@@ -0,0 +1,9 @@
1
+ module ActionKitRest
2
+ module Actions
3
+ class EventCreateAction < Action
4
+ def base_path
5
+ 'eventcreateaction'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module ActionKitRest
2
+ module Actions
3
+ class EventSignupAction < Action
4
+ def base_path
5
+ 'eventsignupaction'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module ActionKitRest
2
+ module Actions
3
+ class UnsubscribeAction < Action
4
+ def base_path
5
+ 'unsubscribeaction'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,10 +1,9 @@
1
1
  module ActionKitRest
2
2
  class API < Vertebrae::API
3
3
 
4
- def request_with_wrapper( *args )
5
- ActionKitRest::Response::Wrapper.new( request_without_wrapper( *args ) )
4
+ def request(*args)
5
+ ActionKitRest::Response::Wrapper.new( super(*args) )
6
6
  end
7
- alias_method_chain :request, :wrapper
8
7
 
9
8
  def post_json_request(path, params)
10
9
  p = {}
@@ -50,4 +49,4 @@ module ActionKitRest
50
49
  end
51
50
  end
52
51
  end
53
- end
52
+ end
@@ -2,34 +2,29 @@ require 'action_kit_rest/api'
2
2
 
3
3
  module ActionKitRest
4
4
  class Client < API
5
+ attr_accessor :clients
5
6
 
6
- def page
7
- @page ||= ActionKitRest::Page.new(client: self)
7
+ def initialize(params)
8
+ super(params)
9
+ self.clients = {}
8
10
  end
9
11
 
10
- def import_page
11
- @import_page ||= ActionKitRest::Pages::ImportPage.new(client: self)
12
+ [:import, :signup, :donation, :unsubscribe, :event_campaign].each do |page_type|
13
+ define_method "#{page_type}_page" do
14
+ clients["#{page_type}_page"] ||= ("ActionKitRest::Pages::#{page_type.to_s.classify}Page".constantize).new(client: self)
15
+ end
12
16
  end
13
17
 
14
- def signup_page
15
- @signup_page ||= ActionKitRest::Pages::SignupPage.new(client: self)
18
+ [:unsubscribe, :event_create, :event_signup].each do |action|
19
+ define_method "#{action}_action" do
20
+ clients["#{action}_action"] ||= ("ActionKitRest::Actions::#{action.to_s.classify}Action".constantize).new(client: self)
21
+ end
16
22
  end
17
23
 
18
- def action
19
- @action ||= ActionKitRest::Action.new(client: self)
24
+ [:action, :page, :tag, :list, :user, :phone, :event, :language, :event_signup].each do |thing|
25
+ define_method thing do
26
+ clients[thing] ||= ("ActionKitRest::#{thing.to_s.classify}".constantize).new(client: self)
27
+ end
20
28
  end
21
-
22
- def tag
23
- @tag ||= ActionKitRest::Tag.new(client: self)
24
- end
25
-
26
- def list
27
- @list ||= ActionKitRest::List.new(client: self)
28
- end
29
-
30
- def user
31
- @user ||= ActionKitRest::User.new(client: self)
32
- end
33
-
34
29
  end
35
30
  end
@@ -0,0 +1,12 @@
1
+ module ActionKitRest
2
+ class Event < Base
3
+ def base_path
4
+ 'event'
5
+ end
6
+
7
+ def find(name)
8
+ response = list(name: name)
9
+ response.obj.first
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module ActionKitRest
2
+ class EventSignup < Base
3
+ def base_path
4
+ 'eventsignup'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module ActionKitRest
2
+ class Language < Base
3
+ def base_path
4
+ 'language'
5
+ end
6
+
7
+ def find(iso_code)
8
+ response = list(iso_code: iso_code)
9
+ response.obj.first
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ module ActionKitRest
2
+ module Pages
3
+ class DonationPage < ActionKitRest::Pages::Base
4
+ def base_path
5
+ 'donationpage'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,54 @@
1
+ module ActionKitRest
2
+ module Pages
3
+ class EventCampaignPage < ActionKitRest::Pages::Base
4
+ def base_path
5
+ 'campaign'
6
+ end
7
+
8
+ def create(params)
9
+ event_pages_tags = params.delete(:event_pages_tags)
10
+ event_campaign = super(params)
11
+
12
+ # Also create EventCreatePage and EventSignupPage
13
+ event_campaign.obj['event_create_page_name'] = create_event_create_page(event_campaign, event_pages_tags)
14
+ event_campaign.obj['event_signup_page_name'] = create_event_signup_page(event_campaign, event_pages_tags)
15
+
16
+ event_campaign
17
+ end
18
+
19
+ private
20
+
21
+ def create_event_create_page(event_campaign, tags)
22
+ params = event_create_page_params(event_campaign, tags)
23
+ response = client.post_json_request('eventcreatepage/', params)
24
+
25
+ params[:name]
26
+ end
27
+
28
+ def event_create_page_params(event_campaign, tags)
29
+ {
30
+ campaign: event_campaign['resource_uri'],
31
+ name: "#{event_campaign['name']}-event-create",
32
+ title: "#{event_campaign['title']}: event create",
33
+ tags: tags
34
+ }
35
+ end
36
+
37
+ def create_event_signup_page(event_campaign, tags)
38
+ params = event_signup_page_params(event_campaign, tags)
39
+ response = client.post_json_request('eventsignuppage/', params)
40
+
41
+ params[:name]
42
+ end
43
+
44
+ def event_signup_page_params(event_campaign, tags)
45
+ {
46
+ campaign: event_campaign['resource_uri'],
47
+ name: "#{event_campaign['name']}-event-signup",
48
+ title: "#{event_campaign['title']}: event signup",
49
+ tags: tags
50
+ }
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,9 @@
1
+ module ActionKitRest
2
+ module Pages
3
+ class UnsubscribePage < ActionKitRest::Pages::Base
4
+ def base_path
5
+ 'unsubscribepage'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ module ActionKitRest
2
+ class Phone < Base
3
+ def base_path
4
+ 'phone'
5
+ end
6
+ end
7
+ end
@@ -9,6 +9,17 @@ module ActionKitRest
9
9
  raise ActionKitRest::Response::ValidationError.new(url: response[:url].to_s, body: response[:body])
10
10
  elsif status_code == 404
11
11
  raise ActionKitRest::Response::NotFound.new(response[:url].to_s)
12
+ elsif status_code == 401
13
+ raise ActionKitRest::Response::Unauthorized.new(response[:url].to_s)
14
+ elsif status_code == 500 && response[:body] =~ /\"error\"/
15
+ error_hsh = JSON.parse(response[:body])
16
+ error_message = error_hsh['error']
17
+
18
+ if error_message == 'Sorry, this request could not be processed. Please try again later.'
19
+ raise ActionKitRest::Response::TryAgainLater.new(error_message(response))
20
+ else
21
+ raise StandardError.new(error_message(response))
22
+ end
12
23
  else
13
24
  raise StandardError.new(error_message(response))
14
25
  end
@@ -20,6 +31,8 @@ module ActionKitRest
20
31
  end
21
32
  end
22
33
 
34
+ class TryAgainLater < StandardError ; end
23
35
  class NotFound < StandardError ; end
24
- end # Response::RaiseError
25
- end
36
+ class Unauthorized < StandardError ; end
37
+ end
38
+ end
@@ -3,5 +3,18 @@ module ActionKitRest
3
3
  def base_path
4
4
  'user'
5
5
  end
6
+
7
+ def get(id)
8
+ user = super(id)
9
+
10
+ # Perform a second request to retrieve user's phones data if any available
11
+ # (user.phones attribute includes the path for associated phones, not the data itself)
12
+ if user.id.present? && user.phones.any?
13
+ phones_list = client.phone.list(user: user.id)
14
+ user.obj.phones = phones_list.obj.to_a
15
+ end
16
+
17
+ user
18
+ end
6
19
  end
7
20
  end
@@ -1,10 +1,10 @@
1
1
  module ActionKitRest
2
2
  module VERSION
3
3
  MAJOR = 0
4
- MINOR = 2
5
- PATCH = 2
4
+ MINOR = 3
5
+ PATCH = 0
6
6
  BUILD = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
9
9
  end
10
- end
10
+ end
@@ -0,0 +1,32 @@
1
+ {
2
+ "allow_private":true,
3
+ "created_at":"2010-11-30T18:45:43",
4
+ "default_event_size":50,
5
+ "default_title":"",
6
+ "eventcreatepages":[],
7
+ "events":"/rest/v1/event/?campaign=88",
8
+ "eventsignuppages":[],
9
+ "id":88,
10
+ "max_event_size":null,
11
+ "name":"climate-change-paris-2015",
12
+ "public_create_page":true,
13
+ "public_search_page":true,
14
+ "require_email_confirmation":true,
15
+ "require_staff_approval":false,
16
+ "resource_uri":"/rest/v1/campaign/88/",
17
+ "show_address1":false,
18
+ "show_attendee_count":false,
19
+ "show_city":true,
20
+ "show_directions":false,
21
+ "show_public_description":true,
22
+ "show_state":true,
23
+ "show_title":true,
24
+ "show_venue":true,
25
+ "show_zip":true,
26
+ "starts_at":null,
27
+ "title":"Climate Change Paris 2015",
28
+ "updated_at":"2015-06-19T01:39:09",
29
+ "use_start_date":true,
30
+ "use_start_time":true,
31
+ "use_title":true
32
+ }
@@ -0,0 +1,33 @@
1
+ { "meta": { "limit": 20,
2
+ "next": "None",
3
+ "offset": 0,
4
+ "previous": "None",
5
+ "total_count": 3},
6
+ "objects": [ { "created_at": "2015-09-29T04:15:50",
7
+ "id": 1,
8
+ "normalized_phone": "7755555555",
9
+ "phone": "7755555555",
10
+ "resource_uri": "/rest/v1/phone/1/",
11
+ "source": "api",
12
+ "type": "home",
13
+ "updated_at": "2016-03-31T17:05:27",
14
+ "user": "/rest/v1/user/1/"},
15
+ { "created_at": "2012-05-12T23:49:44",
16
+ "id": 2,
17
+ "normalized_phone": "7755555577",
18
+ "phone": "7755555577",
19
+ "resource_uri": "/rest/v1/phone/2/",
20
+ "source": "user",
21
+ "type": "home",
22
+ "updated_at": "2016-03-31T17:05:27",
23
+ "user": "/rest/v1/user/1/"},
24
+ { "created_at": "2012-05-12T23:49:44",
25
+ "id": 3,
26
+ "normalized_phone": "3103103310",
27
+ "phone": "310-310-3310",
28
+ "resource_uri": "/rest/v1/phone/3/",
29
+ "source": "user",
30
+ "type": "mobile",
31
+ "updated_at": "2016-03-31T17:05:27",
32
+ "user": "/rest/v1/user/1/"}]
33
+ }
@@ -0,0 +1 @@
1
+ {"actions": "/rest/v1/action/?user=1", "address1": "", "address2": "", "city": "Philadelphia", "country": "United States", "created_at": "2009-09-02T18:42:54", "email": "walkers@wawd.com", "events": "/rest/v1/event/?user=1", "eventsignups": "/rest/v1/eventsignup/?user=1", "fields": {"abc1": "2012-07-09 22:22:38", "abc2": "1", "abc4": "808003061", "abc5": "100", "abc6": "unsubscribed", "abc8": "roboticdogs", "color": ["blue", "yellow"], "food": "meatloaf", "test_custom": "\">la la la"}, "first_name": "", "id": 1, "lang": "/rest/v1/language/100/", "last_name": "", "location": "/rest/v1/location/1/", "middle_name": "", "orderrecurrings": "/rest/v1/orderrecurring/?user=1", "orders": "/rest/v1/order/?user=1", "phones": [], "plus4": "", "postal": "19130", "prefix": "", "rand_id": 382815937, "region": "PA", "resource_uri": "/rest/v1/user/1/", "source": "", "state": "PA", "subscription_status": "unsubscribed", "subscriptionhistory": "/rest/v1/subscriptionhistory/?user=1", "subscriptions": "/rest/v1/subscription/?user=1", "suffix": "", "token": ".1.9P66-W", "updated_at": "2013-10-02T00:56:36", "usergeofields": "/rest/v1/usergeofield/?user=1", "usermailings": "/rest/v1/usermailing/?user=1", "useroriginal": "/rest/v1/useroriginal/1/", "zip": "19130"}
@@ -87,9 +87,17 @@ describe ActionKitRest::Page do
87
87
  let(:status) { 500 }
88
88
 
89
89
  it "should return nil" do
90
- lambda{ subject.page.get(1).should == nil }.should raise_exception
90
+ lambda{ subject.page.get(1).should == nil }.should raise_exception(StandardError)
91
+ end
92
+ end
93
+
94
+ describe 'try again' do
95
+ let(:status) { 500 }
96
+ let(:body) { '{"error": "Sorry, this request could not be processed. Please try again later."}' }
97
+ it "should return nil" do
98
+ lambda{ subject.page.get(1).should == nil }.should raise_exception(ActionKitRest::Response::TryAgainLater)
91
99
  end
92
100
  end
93
101
  end
94
102
  end
95
- end
103
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+ require 'support/shared_contexts/stub_logger'
3
+
4
+ describe ActionKitRest::Pages::EventCampaignPage do
5
+ include_context "stub_logger"
6
+
7
+ describe 'create' do
8
+ let(:actionkit) { ActionKitRest.new(host: 'test.com') }
9
+
10
+ let(:event_campaign_title) { "Climate Change Paris 2015" }
11
+ let(:event_campaign_name) { "climate-change-paris-2015" }
12
+
13
+ before :each do
14
+ stub_request(:post, 'https://test.com/rest/v1/campaign/')
15
+ .with(body: "{\"title\":\"#{event_campaign_title}\",\"name\":\"#{event_campaign_name}\"}")
16
+ .to_return({status: '200', headers: {location: 'https://test.com/rest/v1/campaign/88/'} })
17
+
18
+ stub_request(:get, 'https://test.com/rest/v1/campaign/88/')
19
+ .to_return({body: fixture('page/campaign.json'), status: '200', headers: {content_type: "application/json; charset=utf-8"}})
20
+
21
+ stub_request(:post, 'https://test.com/rest/v1/eventcreatepage/')
22
+ .with(:body => "{\"campaign\":\"/rest/v1/campaign/88/\",\"name\":\"climate-change-paris-2015-event-create\",\"title\":\"Climate Change Paris 2015: event create\",\"tags\":[\"/rest/v1/tag/1/\",\"/rest/v1/tag/5/\"]}")
23
+ .to_return({status: '200', headers: {location: "https://test.com/rest/v1/eventcreatepage/99/"}})
24
+
25
+ stub_request(:post, 'https://test.com/rest/v1/eventsignuppage/')
26
+ .with(:body => "{\"campaign\":\"/rest/v1/campaign/88/\",\"name\":\"climate-change-paris-2015-event-signup\",\"title\":\"Climate Change Paris 2015: event signup\",\"tags\":[\"/rest/v1/tag/1/\",\"/rest/v1/tag/5/\"]}")
27
+ .to_return({status: '200', headers: {location: "https://test.com/rest/v1/eventsignuppage/111/"}})
28
+ end
29
+
30
+ it 'should create an event campaign' do
31
+ resp = actionkit.event_campaign_page.create({title: 'Climate Change Paris 2015', name: 'climate-change-paris-2015', event_pages_tags: ['/rest/v1/tag/1/', '/rest/v1/tag/5/']})
32
+ expect(resp.title).to eq event_campaign_title
33
+ expect(resp.name).to eq event_campaign_name
34
+
35
+ expect(a_request(:post, 'https://test.com/rest/v1/campaign/')).to have_been_made
36
+ expect(a_request(:get, 'https://test.com/rest/v1/campaign/88/')).to have_been_made
37
+ end
38
+
39
+ it 'should create associated event and signup create pages' do
40
+ resp = actionkit.event_campaign_page.create({title: 'Climate Change Paris 2015', name: 'climate-change-paris-2015', event_pages_tags: ['/rest/v1/tag/1/', '/rest/v1/tag/5/']})
41
+ expect(resp.event_create_page_name).to eq 'climate-change-paris-2015-event-create'
42
+ expect(resp.event_signup_page_name).to eq 'climate-change-paris-2015-event-signup'
43
+
44
+ expect(a_request(:post, 'https://test.com/rest/v1/eventcreatepage/')).to have_been_made
45
+ expect(a_request(:post, 'https://test.com/rest/v1/eventsignuppage/')).to have_been_made
46
+ end
47
+ end
48
+ end