kentaa-api 0.3.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 (63) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +23 -0
  3. data/.gitignore +2 -0
  4. data/.rubocop.yml +21 -5
  5. data/Gemfile +4 -2
  6. data/Gemfile.lock +45 -32
  7. data/README.md +364 -67
  8. data/kentaa-api.gemspec +3 -3
  9. data/lib/kentaa/api.rb +9 -20
  10. data/lib/kentaa/api/client.rb +30 -18
  11. data/lib/kentaa/api/config.rb +10 -3
  12. data/lib/kentaa/api/exception.rb +9 -2
  13. data/lib/kentaa/api/request.rb +49 -5
  14. data/lib/kentaa/api/resources/action.rb +39 -15
  15. data/lib/kentaa/api/resources/activity.rb +11 -1
  16. data/lib/kentaa/api/resources/address.rb +7 -1
  17. data/lib/kentaa/api/resources/banner.rb +21 -1
  18. data/lib/kentaa/api/resources/base.rb +19 -6
  19. data/lib/kentaa/api/resources/consent.rb +7 -1
  20. data/lib/kentaa/api/resources/contact.rb +83 -0
  21. data/lib/kentaa/api/resources/donation.rb +30 -12
  22. data/lib/kentaa/api/resources/donation_form.rb +104 -0
  23. data/lib/kentaa/api/resources/error.rb +23 -0
  24. data/lib/kentaa/api/resources/list.rb +77 -2
  25. data/lib/kentaa/api/resources/location.rb +7 -1
  26. data/lib/kentaa/api/resources/manual_donation.rb +122 -0
  27. data/lib/kentaa/api/resources/newsletter_subscription.rb +16 -6
  28. data/lib/kentaa/api/resources/performance.rb +62 -0
  29. data/lib/kentaa/api/resources/photo.rb +21 -1
  30. data/lib/kentaa/api/resources/project.rb +37 -9
  31. data/lib/kentaa/api/resources/question.rb +19 -1
  32. data/lib/kentaa/api/resources/recurring_donor.rb +110 -0
  33. data/lib/kentaa/api/resources/registration_fee.rb +1 -1
  34. data/lib/kentaa/api/resources/resource.rb +43 -5
  35. data/lib/kentaa/api/resources/reward.rb +11 -1
  36. data/lib/kentaa/api/resources/segment.rb +35 -3
  37. data/lib/kentaa/api/resources/site.rb +15 -3
  38. data/lib/kentaa/api/{clients → resources}/sites.rb +2 -2
  39. data/lib/kentaa/api/resources/team.rb +22 -10
  40. data/lib/kentaa/api/resources/user.rb +16 -4
  41. data/lib/kentaa/api/resources/users.rb +5 -24
  42. data/lib/kentaa/api/resources/video.rb +21 -1
  43. data/lib/kentaa/api/response.rb +20 -2
  44. data/lib/kentaa/api/util.rb +13 -0
  45. data/lib/kentaa/api/version.rb +1 -1
  46. metadata +21 -30
  47. data/.travis.yml +0 -11
  48. data/lib/kentaa/api/clients/actions.rb +0 -21
  49. data/lib/kentaa/api/clients/all.rb +0 -26
  50. data/lib/kentaa/api/clients/base.rb +0 -15
  51. data/lib/kentaa/api/clients/donations.rb +0 -21
  52. data/lib/kentaa/api/clients/newsletter_subscriptions.rb +0 -21
  53. data/lib/kentaa/api/clients/projects.rb +0 -21
  54. data/lib/kentaa/api/clients/segments.rb +0 -21
  55. data/lib/kentaa/api/clients/teams.rb +0 -21
  56. data/lib/kentaa/api/clients/users.rb +0 -21
  57. data/lib/kentaa/api/finder.rb +0 -44
  58. data/lib/kentaa/api/resources/actions.rb +0 -37
  59. data/lib/kentaa/api/resources/donations.rb +0 -37
  60. data/lib/kentaa/api/resources/newsletter_subscriptions.rb +0 -37
  61. data/lib/kentaa/api/resources/projects.rb +0 -37
  62. data/lib/kentaa/api/resources/segments.rb +0 -37
  63. data/lib/kentaa/api/resources/teams.rb +0 -37
@@ -3,7 +3,7 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class RegistrationFee < Resource
6
+ class RegistrationFee
7
7
  def amount
8
8
  data[:amount]
9
9
  end
@@ -8,17 +8,41 @@ module Kentaa
8
8
  class Resource < Base
9
9
  attr_accessor :id
10
10
 
11
- def initialize(config, options = {})
11
+ def initialize(config, id: nil, data: nil, options: {})
12
12
  super(config, options)
13
13
 
14
- if options.key?(:data)
15
- @data = options.delete(:data) || {}
14
+ if data
15
+ @data = data || {}
16
16
  @id = @data.fetch(:id) if @data.key?(:id)
17
- elsif options.key?(:id)
18
- @id = options.delete(:id)
17
+ elsif id
18
+ @id = id
19
19
  end
20
20
  end
21
21
 
22
+ def load
23
+ super
24
+ @id = data.fetch(:id) if data.key?(:id)
25
+
26
+ self
27
+ end
28
+
29
+ def save(attributes)
30
+ if id
31
+ @response = update_resource(attributes)
32
+ else
33
+ @response = create_resource(attributes)
34
+ @id = data.fetch(:id) if data.key?(:id)
35
+ end
36
+
37
+ self
38
+ end
39
+
40
+ def delete
41
+ delete_resource
42
+
43
+ nil
44
+ end
45
+
22
46
  def created_at
23
47
  Time.parse(data[:created_at]) if data[:created_at]
24
48
  end
@@ -26,6 +50,20 @@ module Kentaa
26
50
  def updated_at
27
51
  Time.parse(data[:updated_at]) if data[:updated_at]
28
52
  end
53
+
54
+ private
55
+
56
+ def create_resource
57
+ raise NotImplementedError
58
+ end
59
+
60
+ def update_resource
61
+ raise NotImplementedError
62
+ end
63
+
64
+ def delete_resource
65
+ raise NotImplementedError
66
+ end
29
67
  end
30
68
  end
31
69
  end
@@ -3,7 +3,17 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class Reward < Resource
6
+ class Reward
7
+ attr_reader :data
8
+
9
+ def initialize(data)
10
+ @data = data
11
+ end
12
+
13
+ def id
14
+ data[:id]
15
+ end
16
+
7
17
  def type
8
18
  data[:type]
9
19
  end
@@ -11,6 +11,14 @@ module Kentaa
11
11
  "Segment_#{id}"
12
12
  end
13
13
 
14
+ def parent
15
+ site
16
+ end
17
+
18
+ def site
19
+ Kentaa::Api::Resources::Site.new(config, id: site_id, options: options)
20
+ end
21
+
14
22
  def site_id
15
23
  data[:site_id]
16
24
  end
@@ -61,7 +69,7 @@ module Kentaa
61
69
 
62
70
  if data[:banners]
63
71
  data[:banners].each do |banner|
64
- banners << Kentaa::Api::Resources::Banner.new(config, data: banner)
72
+ banners << Kentaa::Api::Resources::Banner.new(banner)
65
73
  end
66
74
  end
67
75
 
@@ -73,9 +81,33 @@ module Kentaa
73
81
  data[:external_reference]
74
82
  end
75
83
 
76
- protected
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
+
96
+ def donations
97
+ @donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Donation, endpoint_path: "/segments/#{id}/donations")
98
+ end
99
+
100
+ def manual_donations
101
+ @manual_donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::ManualDonation, endpoint_path: "/segments/#{id}/manual-donations")
102
+ end
103
+
104
+ def newsletter_subscriptions
105
+ @newsletter_subscriptions ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::NewsletterSubscription, endpoint_path: "/segments/#{id}/newsletter-subscriptions")
106
+ end
107
+
108
+ private
77
109
 
78
- def load_resource(options)
110
+ def load_resource
79
111
  request.get("/segments/#{id}", options)
80
112
  end
81
113
  end
@@ -73,7 +73,7 @@ module Kentaa
73
73
 
74
74
  if data[:banners]
75
75
  data[:banners].each do |banner|
76
- banners << Kentaa::Api::Resources::Banner.new(config, data: banner)
76
+ banners << Kentaa::Api::Resources::Banner.new(banner)
77
77
  end
78
78
  end
79
79
 
@@ -85,9 +85,21 @@ module Kentaa
85
85
  data[:external_reference]
86
86
  end
87
87
 
88
- protected
88
+ def donations
89
+ @donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Donation, endpoint_path: "/donations")
90
+ end
91
+
92
+ def manual_donations
93
+ @manual_donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::ManualDonation, endpoint_path: "/manual-donations")
94
+ end
95
+
96
+ def newsletter_subscriptions
97
+ @newsletter_subscriptions ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::NewsletterSubscription, endpoint_path: "/newsletter-subscriptions")
98
+ end
99
+
100
+ private
89
101
 
90
- def load_resource(options)
102
+ def load_resource
91
103
  request.get("/sites/current", options)
92
104
  end
93
105
  end
@@ -2,10 +2,10 @@
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
- site = Kentaa::Api::Resources::Site.new(config, options)
8
+ site = Kentaa::Api::Resources::Site.new(config, options: options)
9
9
  site.load
10
10
  end
11
11
  end
@@ -13,14 +13,18 @@ module Kentaa
13
13
 
14
14
  def parent
15
15
  if project_id
16
- Kentaa::Api::Resources::Project.new(config, id: project_id)
16
+ Kentaa::Api::Resources::Project.new(config, id: project_id, options: options)
17
17
  elsif segment_id
18
- Kentaa::Api::Resources::Segment.new(config, id: segment_id)
18
+ Kentaa::Api::Resources::Segment.new(config, id: segment_id, options: options)
19
19
  else
20
- Kentaa::Api::Resources::Site.new(config, id: site_id)
20
+ Kentaa::Api::Resources::Site.new(config, id: site_id, options: options)
21
21
  end
22
22
  end
23
23
 
24
+ def site
25
+ Kentaa::Api::Resources::Site.new(config, id: site_id, options: options)
26
+ end
27
+
24
28
  def slug
25
29
  data[:slug]
26
30
  end
@@ -38,7 +42,7 @@ module Kentaa
38
42
  end
39
43
 
40
44
  def owner
41
- @owner ||= Kentaa::Api::Resources::User.new(config, data: data[:owner])
45
+ @owner ||= Kentaa::Api::Resources::User.new(config, data: data[:owner], options: options)
42
46
  end
43
47
 
44
48
  def members
@@ -47,7 +51,7 @@ module Kentaa
47
51
 
48
52
  if data[:members]
49
53
  data[:members].each do |member|
50
- members << Kentaa::Api::Resources::Action.new(config, data: member)
54
+ members << Kentaa::Api::Resources::Action.new(config, data: member, options: options)
51
55
  end
52
56
  end
53
57
 
@@ -117,7 +121,7 @@ module Kentaa
117
121
 
118
122
  if data[:photos]
119
123
  data[:photos].each do |photo|
120
- photos << Kentaa::Api::Resources::Photo.new(config, data: photo)
124
+ photos << Kentaa::Api::Resources::Photo.new(photo)
121
125
  end
122
126
  end
123
127
 
@@ -131,7 +135,7 @@ module Kentaa
131
135
 
132
136
  if data[:videos]
133
137
  data[:videos].each do |video|
134
- videos << Kentaa::Api::Resources::Video.new(config, data: video)
138
+ videos << Kentaa::Api::Resources::Video.new(video)
135
139
  end
136
140
  end
137
141
 
@@ -145,7 +149,7 @@ module Kentaa
145
149
 
146
150
  if data[:questions]
147
151
  data[:questions].each do |question|
148
- questions << Kentaa::Api::Resources::Question.new(config, data: question)
152
+ questions << Kentaa::Api::Resources::Question.new(question)
149
153
  end
150
154
  end
151
155
 
@@ -157,9 +161,17 @@ module Kentaa
157
161
  data[:external_reference]
158
162
  end
159
163
 
160
- protected
164
+ def donations
165
+ @donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Donation, endpoint_path: "/teams/#{id}/donations")
166
+ end
167
+
168
+ def manual_donations
169
+ @manual_donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::ManualDonation, endpoint_path: "/teams/#{id}/manual-donations")
170
+ end
171
+
172
+ private
161
173
 
162
- def load_resource(options)
174
+ def load_resource
163
175
  request.get("/teams/#{id}", options)
164
176
  end
165
177
  end
@@ -10,6 +10,10 @@ module Kentaa
10
10
  "User_#{id}"
11
11
  end
12
12
 
13
+ def site
14
+ Kentaa::Api::Resources::Site.new(config, id: site_id, options: options)
15
+ end
16
+
13
17
  def site_id
14
18
  data[:site_id]
15
19
  end
@@ -75,7 +79,7 @@ module Kentaa
75
79
  end
76
80
 
77
81
  def birthday
78
- Time.parse(data[:birthday]) if data[:birthday]
82
+ Date.parse(data[:birthday]) if data[:birthday]
79
83
  end
80
84
 
81
85
  def gender
@@ -87,14 +91,22 @@ module Kentaa
87
91
  end
88
92
 
89
93
  def consent
90
- @consent ||= Kentaa::Api::Resources::Consent.new(config, data: data[:consent]) if data[:consent]
94
+ @consent ||= Kentaa::Api::Resources::Consent.new(data[:consent]) if data[:consent]
91
95
  end
92
96
 
93
- protected
97
+ private
94
98
 
95
- def load_resource(options)
99
+ def load_resource
96
100
  request.get("/users/#{id}", options)
97
101
  end
102
+
103
+ def create_resource(attributes)
104
+ request.post("/users", options, attributes)
105
+ end
106
+
107
+ def update_resource(attributes)
108
+ request.patch("/users/#{id}", options, attributes)
109
+ end
98
110
  end
99
111
  end
100
112
  end
@@ -4,32 +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
- protected
14
-
15
- def load_resource(options)
16
- request.get("/users", options)
7
+ def initialize(config, options = {})
8
+ super(config, options.merge(resource_class: Kentaa::Api::Resources::User, endpoint_path: "/users"))
17
9
  end
18
10
 
19
- private
20
-
21
- def users
22
- @users ||= begin
23
- users = []
24
-
25
- if data
26
- data.each do |user|
27
- users << Kentaa::Api::Resources::User.new(config, data: user)
28
- end
29
- end
30
-
31
- users
32
- end
11
+ def auth(attributes, options = {})
12
+ resource = resource_class.new(config, options: options)
13
+ resource.load { request.post("#{endpoint_path}/auth", options, attributes) }
33
14
  end
34
15
  end
35
16
  end
@@ -1,9 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'time'
4
+
3
5
  module Kentaa
4
6
  module Api
5
7
  module Resources
6
- class Video < Resource
8
+ class Video
9
+ attr_reader :data
10
+
11
+ def initialize(data)
12
+ @data = data
13
+ end
14
+
15
+ def id
16
+ data[:id]
17
+ end
18
+
19
+ def created_at
20
+ Time.parse(data[:created_at]) if data[:created_at]
21
+ end
22
+
23
+ def updated_at
24
+ Time.parse(data[:updated_at]) if data[:updated_at]
25
+ end
26
+
7
27
  def url
8
28
  data[:url]
9
29
  end
@@ -9,11 +9,11 @@ module Kentaa
9
9
 
10
10
  def initialize(response)
11
11
  @response = response
12
- @body = parse_body(response.body)
12
+ @body = response.body ? parse_body(response.body) : {}
13
13
  end
14
14
 
15
15
  def success?
16
- (http_code == 200 || http_code == 201) && !message
16
+ (http_code == 200 || http_code == 201 || http_code == 204) && !message
17
17
  end
18
18
 
19
19
  def error?
@@ -24,10 +24,28 @@ module Kentaa
24
24
  response.code.to_i
25
25
  end
26
26
 
27
+ def request_uri
28
+ response.uri
29
+ end
30
+
27
31
  def message
28
32
  body[:message]
29
33
  end
30
34
 
35
+ def errors
36
+ @errors ||= begin
37
+ errors = []
38
+
39
+ if body[:errors]
40
+ body[:errors].each do |error|
41
+ errors << Kentaa::Api::Resources::Error.new(error)
42
+ end
43
+ end
44
+
45
+ errors
46
+ end
47
+ end
48
+
31
49
  private
32
50
 
33
51
  def parse_body(body)