kentaa-api 0.5.0 → 0.6.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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +23 -0
  3. data/.rubocop.yml +1 -0
  4. data/Gemfile.lock +14 -14
  5. data/README.md +64 -21
  6. data/lib/kentaa/api.rb +4 -24
  7. data/lib/kentaa/api/client.rb +24 -24
  8. data/lib/kentaa/api/config.rb +6 -3
  9. data/lib/kentaa/api/request.rb +2 -2
  10. data/lib/kentaa/api/resources/action.rb +6 -2
  11. data/lib/kentaa/api/resources/base.rb +18 -5
  12. data/lib/kentaa/api/resources/donation_form.rb +7 -3
  13. data/lib/kentaa/api/resources/list.rb +61 -3
  14. data/lib/kentaa/api/resources/performance.rb +62 -0
  15. data/lib/kentaa/api/resources/project.rb +11 -3
  16. data/lib/kentaa/api/resources/recurring_donor.rb +1 -1
  17. data/lib/kentaa/api/resources/segment.rb +15 -3
  18. data/lib/kentaa/api/resources/site.rb +3 -3
  19. data/lib/kentaa/api/{clients → resources}/sites.rb +1 -1
  20. data/lib/kentaa/api/resources/team.rb +2 -2
  21. data/lib/kentaa/api/resources/user.rb +0 -6
  22. data/lib/kentaa/api/resources/users.rb +5 -32
  23. data/lib/kentaa/api/util.rb +13 -0
  24. data/lib/kentaa/api/version.rb +1 -1
  25. metadata +6 -25
  26. data/.travis.yml +0 -11
  27. data/lib/kentaa/api/clients/actions.rb +0 -34
  28. data/lib/kentaa/api/clients/base.rb +0 -15
  29. data/lib/kentaa/api/clients/donation_forms.rb +0 -24
  30. data/lib/kentaa/api/clients/donations.rb +0 -24
  31. data/lib/kentaa/api/clients/manual_donations.rb +0 -39
  32. data/lib/kentaa/api/clients/newsletter_subscriptions.rb +0 -24
  33. data/lib/kentaa/api/clients/projects.rb +0 -24
  34. data/lib/kentaa/api/clients/recurring_donors.rb +0 -24
  35. data/lib/kentaa/api/clients/segments.rb +0 -24
  36. data/lib/kentaa/api/clients/teams.rb +0 -24
  37. data/lib/kentaa/api/clients/users.rb +0 -39
  38. data/lib/kentaa/api/finder.rb +0 -44
  39. data/lib/kentaa/api/resources/actions.rb +0 -40
  40. data/lib/kentaa/api/resources/donation_forms.rb +0 -35
  41. data/lib/kentaa/api/resources/donations.rb +0 -35
  42. data/lib/kentaa/api/resources/manual_donations.rb +0 -40
  43. data/lib/kentaa/api/resources/newsletter_subscriptions.rb +0 -35
  44. data/lib/kentaa/api/resources/projects.rb +0 -35
  45. data/lib/kentaa/api/resources/recurring_donors.rb +0 -35
  46. data/lib/kentaa/api/resources/segments.rb +0 -35
  47. data/lib/kentaa/api/resources/teams.rb +0 -35
@@ -35,8 +35,6 @@ module Kentaa
35
35
  uri = URI.parse(File.join(config.api_url, path))
36
36
  uri.query = URI.encode_www_form(params) unless params.empty?
37
37
 
38
- logger.debug("[Kentaa-API] Request: #{http_method.upcase} #{uri}") if config.debug?
39
-
40
38
  case http_method
41
39
  when :get
42
40
  request = Net::HTTP::Get.new(uri)
@@ -52,6 +50,8 @@ module Kentaa
52
50
  raise Kentaa::Api::Exception, "Invalid HTTP method: #{http_method}"
53
51
  end
54
52
 
53
+ logger.debug("[Kentaa-API] Request: #{http_method.upcase} #{uri}") if config.debug?
54
+
55
55
  request["Accept"] = "application/json"
56
56
  request["Content-Type"] = "application/json"
57
57
  request["X-Api-Key"] = config.api_key
@@ -198,11 +198,15 @@ module Kentaa
198
198
  end
199
199
 
200
200
  def donations
201
- @donations ||= Kentaa::Api::Resources::Donations.new(config, action_id: id)
201
+ @donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Donation, endpoint_path: "/actions/#{id}/donations")
202
202
  end
203
203
 
204
204
  def manual_donations
205
- @manual_donations ||= Kentaa::Api::Resources::ManualDonations.new(config, action_id: id)
205
+ @manual_donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::ManualDonation, endpoint_path: "/actions/#{id}/manual-donations")
206
+ end
207
+
208
+ def performances
209
+ @performances ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Performance, endpoint_path: "/actions/#{id}/performances")
206
210
  end
207
211
 
208
212
  private
@@ -4,15 +4,29 @@ module Kentaa
4
4
  module Api
5
5
  module Resources
6
6
  class Base
7
- attr_reader :config, :options
7
+ attr_reader :config, :options, :resource_class, :endpoint_path
8
8
 
9
9
  def initialize(config, options = {})
10
10
  @config = config
11
11
  @options = options
12
+ @resource_class = options.delete(:resource_class) || self.class
13
+ @endpoint_path = options.delete(:endpoint_path)
14
+ end
15
+
16
+ class << self
17
+ def attribute_key
18
+ class_name = name.split('::').last
19
+ class_name.gsub(/([^\^])([A-Z])/, '\1_\2').downcase
20
+ end
12
21
  end
13
22
 
14
23
  def load
15
- @response ||= load_resource
24
+ @response ||=
25
+ if block_given?
26
+ yield
27
+ else
28
+ load_resource
29
+ end
16
30
 
17
31
  self
18
32
  end
@@ -24,8 +38,7 @@ module Kentaa
24
38
  private
25
39
 
26
40
  def attribute_key
27
- class_name = self.class.name.split('::').last
28
- class_name.gsub(/([^\^])([A-Z])/, '\1_\2').downcase.to_sym
41
+ self.class.attribute_key
29
42
  end
30
43
 
31
44
  def load_resource
@@ -36,7 +49,7 @@ module Kentaa
36
49
  @data ||= begin
37
50
  load unless loaded?
38
51
 
39
- @response.body[attribute_key] || {}
52
+ @response.body[attribute_key.to_sym] || {}
40
53
  end
41
54
  end
42
55
 
@@ -78,15 +78,19 @@ module Kentaa
78
78
  end
79
79
 
80
80
  def donations
81
- @donations ||= Kentaa::Api::Resources::Donations.new(config, donation_form_id: id)
81
+ @donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Donation, endpoint_path: "/donation-forms/#{id}/donations")
82
82
  end
83
83
 
84
84
  def manual_donations
85
- @manual_donations ||= Kentaa::Api::Resources::ManualDonations.new(config, donation_form_id: id)
85
+ @manual_donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::ManualDonation, endpoint_path: "/donation-forms/#{id}/manual-donations")
86
86
  end
87
87
 
88
88
  def newsletter_subscriptions
89
- @newsletter_subscriptions ||= Kentaa::Api::Resources::NewsletterSubscriptions.new(config, donation_form_id: id)
89
+ @newsletter_subscriptions ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::NewsletterSubscription, endpoint_path: "/donation-forms/#{id}/newsletter-subscriptions")
90
+ end
91
+
92
+ def recurring_donors
93
+ @recurring_donors ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::RecurringDonor, endpoint_path: "/donation-forms/#{id}/recurring-donors")
90
94
  end
91
95
 
92
96
  private
@@ -4,6 +4,20 @@ module Kentaa
4
4
  module Api
5
5
  module Resources
6
6
  class List < Base
7
+ include Enumerable
8
+
9
+ def [](index)
10
+ resources[index]
11
+ end
12
+
13
+ def size
14
+ resources.size
15
+ end
16
+
17
+ def each(&block)
18
+ resources.each(&block)
19
+ end
20
+
7
21
  def links
8
22
  body[:links]
9
23
  end
@@ -45,11 +59,11 @@ module Kentaa
45
59
  end
46
60
 
47
61
  def next
48
- self.class.new(config, options.merge(page: next_page)) if next_page?
62
+ self.class.new(config, options.merge(resource_class: resource_class, endpoint_path: endpoint_path, page: next_page)) if next_page?
49
63
  end
50
64
 
51
65
  def previous
52
- self.class.new(config, options.merge(page: previous_page)) if previous_page?
66
+ self.class.new(config, options.merge(resource_class: resource_class, endpoint_path: endpoint_path, page: previous_page)) if previous_page?
53
67
  end
54
68
 
55
69
  def all
@@ -57,7 +71,7 @@ module Kentaa
57
71
  page = 1
58
72
 
59
73
  loop do
60
- response = self.class.new(config, options.merge(page: page))
74
+ response = self.class.new(config, options.merge(resource_class: resource_class, endpoint_path: endpoint_path, page: page))
61
75
  response.each { |item| yielder.yield item }
62
76
 
63
77
  raise StopIteration unless response.next_page?
@@ -68,6 +82,50 @@ module Kentaa
68
82
 
69
83
  enumerator.lazy
70
84
  end
85
+
86
+ def get(id, options = {})
87
+ resource = resource_class.new(config, id: id, options: options.merge(endpoint_path: endpoint_path))
88
+ resource.load
89
+ end
90
+
91
+ def create(attributes, options = {})
92
+ resource = resource_class.new(config, options: options.merge(endpoint_path: endpoint_path))
93
+ resource.save(attributes)
94
+ end
95
+
96
+ def update(id, attributes, options = {})
97
+ resource = resource_class.new(config, id: id, options: options.merge(endpoint_path: endpoint_path))
98
+ resource.save(attributes)
99
+ end
100
+
101
+ def delete(id, options = {})
102
+ resource = resource_class.new(config, id: id, options: options.merge(endpoint_path: endpoint_path))
103
+ resource.delete
104
+ end
105
+
106
+ private
107
+
108
+ def resources
109
+ @resources ||= begin
110
+ resources = []
111
+
112
+ if data
113
+ data.each do |resource|
114
+ resources << resource_class.new(config, data: resource, options: options)
115
+ end
116
+ end
117
+
118
+ resources
119
+ end
120
+ end
121
+
122
+ def attribute_key
123
+ Util.pluralize(resource_class.attribute_key)
124
+ end
125
+
126
+ def load_resource
127
+ request.get(endpoint_path, options)
128
+ end
71
129
  end
72
130
  end
73
131
  end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bigdecimal'
4
+ require 'time'
5
+
6
+ module Kentaa
7
+ module Api
8
+ module Resources
9
+ class Performance < Resource
10
+ def object_key
11
+ "ActionPerformance_#{id}"
12
+ end
13
+
14
+ def action_id
15
+ data[:action_id]
16
+ end
17
+
18
+ def action
19
+ Kentaa::Api::Resources::Action.new(config, id: action_id, options: options)
20
+ end
21
+
22
+ def title
23
+ data[:title]
24
+ end
25
+
26
+ def performance_type
27
+ data[:performance_type]
28
+ end
29
+
30
+ def performance_at
31
+ Time.parse(data[:performance_at])
32
+ end
33
+
34
+ def distance
35
+ BigDecimal(data[:distance])
36
+ end
37
+
38
+ def unit
39
+ data[:unit]
40
+ end
41
+
42
+ private
43
+
44
+ def load_resource
45
+ request.get("#{endpoint_path}/#{id}", options)
46
+ end
47
+
48
+ def create_resource(attributes)
49
+ request.post(endpoint_path, options, attributes)
50
+ end
51
+
52
+ def update_resource(attributes)
53
+ request.patch("#{endpoint_path}/#{id}", options, attributes)
54
+ end
55
+
56
+ def delete_resource
57
+ request.delete("#{endpoint_path}/#{id}", options)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -145,16 +145,24 @@ module Kentaa
145
145
  data[:external_reference]
146
146
  end
147
147
 
148
+ def actions
149
+ @actions ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Action, endpoint_path: "/projects/#{id}/actions")
150
+ end
151
+
152
+ def teams
153
+ @teams ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Team, endpoint_path: "/projects/#{id}/teams")
154
+ end
155
+
148
156
  def donations
149
- @donations ||= Kentaa::Api::Resources::Donations.new(config, project_id: id)
157
+ @donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Donation, endpoint_path: "/projects/#{id}/donations")
150
158
  end
151
159
 
152
160
  def manual_donations
153
- @manual_donations ||= Kentaa::Api::Resources::ManualDonations.new(config, project_id: id)
161
+ @manual_donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::ManualDonation, endpoint_path: "/projects/#{id}/manual-donations")
154
162
  end
155
163
 
156
164
  def newsletter_subscriptions
157
- @newsletter_subscriptions ||= Kentaa::Api::Resources::NewsletterSubscriptions.new(config, project_id: id)
165
+ @newsletter_subscriptions ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::NewsletterSubscription, endpoint_path: "/projects/#{id}/newsletter-subscriptions")
158
166
  end
159
167
 
160
168
  private
@@ -96,7 +96,7 @@ module Kentaa
96
96
  end
97
97
 
98
98
  def donations
99
- @donations ||= Kentaa::Api::Resources::Donations.new(config, recurring_donor_id: id)
99
+ @donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Donation, endpoint_path: "/recurring-donors/#{id}/donations")
100
100
  end
101
101
 
102
102
  private
@@ -81,16 +81,28 @@ module Kentaa
81
81
  data[:external_reference]
82
82
  end
83
83
 
84
+ def actions
85
+ @actions ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Action, endpoint_path: "/segments/#{id}/actions")
86
+ end
87
+
88
+ def teams
89
+ @teams ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Team, endpoint_path: "/segments/#{id}/teams")
90
+ end
91
+
92
+ def projects
93
+ @projects ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Project, endpoint_path: "/segments/#{id}/projects")
94
+ end
95
+
84
96
  def donations
85
- @donations ||= Kentaa::Api::Resources::Donations.new(config, segment_id: id)
97
+ @donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Donation, endpoint_path: "/segments/#{id}/donations")
86
98
  end
87
99
 
88
100
  def manual_donations
89
- @manual_donations ||= Kentaa::Api::Resources::ManualDonations.new(config, segment_id: id)
101
+ @manual_donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::ManualDonation, endpoint_path: "/segments/#{id}/manual-donations")
90
102
  end
91
103
 
92
104
  def newsletter_subscriptions
93
- @newsletter_subscriptions ||= Kentaa::Api::Resources::NewsletterSubscriptions.new(config, segment_id: id)
105
+ @newsletter_subscriptions ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::NewsletterSubscription, endpoint_path: "/segments/#{id}/newsletter-subscriptions")
94
106
  end
95
107
 
96
108
  private
@@ -86,15 +86,15 @@ module Kentaa
86
86
  end
87
87
 
88
88
  def donations
89
- @donations ||= Kentaa::Api::Resources::Donations.new(config)
89
+ @donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Donation, endpoint_path: "/donations")
90
90
  end
91
91
 
92
92
  def manual_donations
93
- @manual_donations ||= Kentaa::Api::Resources::ManualDonations.new(config)
93
+ @manual_donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::ManualDonation, endpoint_path: "/manual-donations")
94
94
  end
95
95
 
96
96
  def newsletter_subscriptions
97
- @newsletter_subscriptions ||= Kentaa::Api::Resources::NewsletterSubscriptions.new(config)
97
+ @newsletter_subscriptions ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::NewsletterSubscription, endpoint_path: "/newsletter-subscriptions")
98
98
  end
99
99
 
100
100
  private
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kentaa
4
4
  module Api
5
- module Clients
5
+ module Resources
6
6
  class Sites < Base
7
7
  def current(options = {})
8
8
  site = Kentaa::Api::Resources::Site.new(config, options: options)
@@ -162,11 +162,11 @@ module Kentaa
162
162
  end
163
163
 
164
164
  def donations
165
- @donations ||= Kentaa::Api::Resources::Donations.new(config, team_id: id)
165
+ @donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Donation, endpoint_path: "/teams/#{id}/donations")
166
166
  end
167
167
 
168
168
  def manual_donations
169
- @manual_donations ||= Kentaa::Api::Resources::ManualDonations.new(config, team_id: id)
169
+ @manual_donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::ManualDonation, endpoint_path: "/teams/#{id}/manual-donations")
170
170
  end
171
171
 
172
172
  private
@@ -94,12 +94,6 @@ module Kentaa
94
94
  @consent ||= Kentaa::Api::Resources::Consent.new(data[:consent]) if data[:consent]
95
95
  end
96
96
 
97
- def auth(attributes)
98
- @response = request.post("/users/auth", options, attributes)
99
-
100
- self
101
- end
102
-
103
97
  private
104
98
 
105
99
  def load_resource
@@ -4,40 +4,13 @@ module Kentaa
4
4
  module Api
5
5
  module Resources
6
6
  class Users < List
7
- include Enumerable
8
-
9
- def each(&block)
10
- users.each(&block)
11
- end
12
-
13
- def create(attributes = {})
14
- user = Kentaa::Api::Resources::User.new(config, options: options)
15
- user.save(attributes)
16
- end
17
-
18
- def auth(attributes = {})
19
- user = Kentaa::Api::Resources::User.new(config, options: options)
20
- user.auth(attributes)
7
+ def initialize(config, options = {})
8
+ super(config, options.merge(resource_class: Kentaa::Api::Resources::User, endpoint_path: "/users"))
21
9
  end
22
10
 
23
- private
24
-
25
- def load_resource
26
- request.get("/users", options)
27
- end
28
-
29
- def users
30
- @users ||= begin
31
- users = []
32
-
33
- if data
34
- data.each do |user|
35
- users << Kentaa::Api::Resources::User.new(config, data: user, options: options)
36
- end
37
- end
38
-
39
- users
40
- end
11
+ def auth(attributes, options = {})
12
+ resource = resource_class.new(config, options: options)
13
+ resource.load { request.post("#{endpoint_path}/auth", options, attributes) }
41
14
  end
42
15
  end
43
16
  end