kentaa-api 0.2.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.rubocop.yml +14 -3
  4. data/.travis.yml +5 -4
  5. data/Gemfile +3 -2
  6. data/Gemfile.lock +19 -14
  7. data/README.md +12 -12
  8. data/kentaa-api.gemspec +2 -2
  9. data/lib/kentaa/api.rb +6 -4
  10. data/lib/kentaa/api/client.rb +4 -0
  11. data/lib/kentaa/api/clients/actions.rb +19 -6
  12. data/lib/kentaa/api/clients/base.rb +0 -4
  13. data/lib/kentaa/api/clients/donations.rb +9 -6
  14. data/lib/kentaa/api/clients/manual_donations.rb +39 -0
  15. data/lib/kentaa/api/clients/newsletter_subscriptions.rb +9 -6
  16. data/lib/kentaa/api/clients/projects.rb +9 -6
  17. data/lib/kentaa/api/clients/segments.rb +9 -6
  18. data/lib/kentaa/api/clients/sites.rb +3 -3
  19. data/lib/kentaa/api/clients/teams.rb +9 -6
  20. data/lib/kentaa/api/clients/users.rb +19 -6
  21. data/lib/kentaa/api/exception.rb +18 -0
  22. data/lib/kentaa/api/finder.rb +9 -0
  23. data/lib/kentaa/api/request.rb +46 -5
  24. data/lib/kentaa/api/resources/action.rb +36 -20
  25. data/lib/kentaa/api/resources/actions.rb +11 -3
  26. data/lib/kentaa/api/resources/activity.rb +10 -2
  27. data/lib/kentaa/api/resources/address.rb +6 -2
  28. data/lib/kentaa/api/resources/banner.rb +20 -2
  29. data/lib/kentaa/api/resources/base.rb +35 -11
  30. data/lib/kentaa/api/resources/consent.rb +7 -1
  31. data/lib/kentaa/api/resources/contact.rb +83 -0
  32. data/lib/kentaa/api/resources/donation.rb +25 -18
  33. data/lib/kentaa/api/resources/donations.rb +6 -3
  34. data/lib/kentaa/api/resources/error.rb +23 -0
  35. data/lib/kentaa/api/resources/{pagination.rb → list.rb} +26 -3
  36. data/lib/kentaa/api/resources/location.rb +7 -1
  37. data/lib/kentaa/api/resources/manual_donation.rb +112 -0
  38. data/lib/kentaa/api/resources/manual_donations.rb +40 -0
  39. data/lib/kentaa/api/resources/newsletter_subscription.rb +12 -11
  40. data/lib/kentaa/api/resources/newsletter_subscriptions.rb +6 -3
  41. data/lib/kentaa/api/resources/photo.rb +20 -2
  42. data/lib/kentaa/api/resources/project.rb +26 -12
  43. data/lib/kentaa/api/resources/projects.rb +6 -3
  44. data/lib/kentaa/api/resources/question.rb +10 -2
  45. data/lib/kentaa/api/resources/registration_fee.rb +1 -1
  46. data/lib/kentaa/api/resources/resource.rb +50 -3
  47. data/lib/kentaa/api/resources/reward.rb +10 -2
  48. data/lib/kentaa/api/resources/segment.rb +16 -4
  49. data/lib/kentaa/api/resources/segments.rb +6 -3
  50. data/lib/kentaa/api/resources/site.rb +16 -4
  51. data/lib/kentaa/api/resources/team.rb +23 -14
  52. data/lib/kentaa/api/resources/teams.rb +6 -3
  53. data/lib/kentaa/api/resources/user.rb +17 -5
  54. data/lib/kentaa/api/resources/users.rb +11 -3
  55. data/lib/kentaa/api/resources/video.rb +20 -2
  56. data/lib/kentaa/api/response.rb +21 -3
  57. data/lib/kentaa/api/version.rb +1 -1
  58. metadata +16 -14
  59. data/lib/kentaa/api/clients/all.rb +0 -26
  60. data/lib/kentaa/api/resources/status.rb +0 -27
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kentaa
4
+ module Api
5
+ module Resources
6
+ class ManualDonations < List
7
+ include Enumerable
8
+
9
+ def each(&block)
10
+ manual_donations.each(&block)
11
+ end
12
+
13
+ def create(attributes = {})
14
+ donation = Kentaa::Api::Resources::ManualDonation.new(config, options)
15
+ donation.save(attributes)
16
+ end
17
+
18
+ private
19
+
20
+ def load_resource
21
+ request.get("/manual-donations", options)
22
+ end
23
+
24
+ def manual_donations
25
+ @manual_donations ||= begin
26
+ manual_donations = []
27
+
28
+ if data
29
+ data.each do |manual_donation|
30
+ manual_donations << Kentaa::Api::Resources::ManualDonation.new(config, data: manual_donation)
31
+ end
32
+ end
33
+
34
+ manual_donations
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -3,23 +3,18 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class NewsletterSubscription < Base
7
- include Kentaa::Api::Resources::Resource
8
-
6
+ class NewsletterSubscription < Resource
9
7
  def object_key
10
8
  "NewsletterSubscription_#{id}"
11
9
  end
12
10
 
13
11
  def entity
14
12
  if project_id
15
- client = Kentaa::Api::Clients::Projects.new(config)
16
- client.get(project_id)
13
+ Kentaa::Api::Resources::Project.new(config, id: project_id)
17
14
  elsif segment_id
18
- client = Kentaa::Api::Clients::Segments.new(config)
19
- client.get(segment_id)
15
+ Kentaa::Api::Resources::Segment.new(config, id: segment_id)
20
16
  else
21
- client = Kentaa::Api::Clients::Sites.new(config)
22
- client.current
17
+ Kentaa::Api::Resources::Site.new(config, id: site_id)
23
18
  end
24
19
  end
25
20
 
@@ -36,7 +31,7 @@ module Kentaa
36
31
  end
37
32
 
38
33
  def name
39
- [first_name, infix, last_name].compact.join(" ")
34
+ [first_name, infix, last_name].reject { |s| s.to_s.empty? }.join(" ")
40
35
  end
41
36
 
42
37
  def site_id
@@ -64,7 +59,13 @@ module Kentaa
64
59
  end
65
60
 
66
61
  def consent
67
- @consent ||= Kentaa::Api::Resources::Consent.new(config, data[:consent]) if data[:consent]
62
+ @consent ||= Kentaa::Api::Resources::Consent.new(data[:consent]) if data[:consent]
63
+ end
64
+
65
+ private
66
+
67
+ def load_resource
68
+ request.get("/newsletter-subscriptions/#{id}", options)
68
69
  end
69
70
  end
70
71
  end
@@ -3,9 +3,8 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class NewsletterSubscriptions < Base
6
+ class NewsletterSubscriptions < List
7
7
  include Enumerable
8
- include Kentaa::Api::Resources::Pagination
9
8
 
10
9
  def each(&block)
11
10
  newsletter_subscriptions.each(&block)
@@ -13,13 +12,17 @@ module Kentaa
13
12
 
14
13
  private
15
14
 
15
+ def load_resource
16
+ request.get("/newsletter-subscriptions", options)
17
+ end
18
+
16
19
  def newsletter_subscriptions
17
20
  @newsletter_subscriptions ||= begin
18
21
  newsletter_subscriptions = []
19
22
 
20
23
  if data
21
24
  data.each do |newsletter_subscription|
22
- newsletter_subscriptions << Kentaa::Api::Resources::NewsletterSubscription.new(config, newsletter_subscription)
25
+ newsletter_subscriptions << Kentaa::Api::Resources::NewsletterSubscription.new(config, data: newsletter_subscription)
23
26
  end
24
27
  end
25
28
 
@@ -1,10 +1,28 @@
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 Photo < Base
7
- include Kentaa::Api::Resources::Resource
8
+ class Photo
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
8
26
 
9
27
  def url
10
28
  data[:url]
@@ -6,20 +6,16 @@ require 'time'
6
6
  module Kentaa
7
7
  module Api
8
8
  module Resources
9
- class Project < Base
10
- include Kentaa::Api::Resources::Resource
11
-
9
+ class Project < Resource
12
10
  def object_key
13
11
  "Project_#{id}"
14
12
  end
15
13
 
16
14
  def parent
17
15
  if segment_id
18
- client = Kentaa::Api::Clients::Segments.new(config)
19
- client.get(segment_id)
16
+ Kentaa::Api::Resources::Segment.new(config, id: segment_id)
20
17
  else
21
- client = Kentaa::Api::Clients::Sites.new(config)
22
- client.current
18
+ Kentaa::Api::Resources::Site.new(config, id: site_id)
23
19
  end
24
20
  end
25
21
 
@@ -88,7 +84,7 @@ module Kentaa
88
84
  end
89
85
 
90
86
  def location
91
- @location ||= Kentaa::Api::Resources::Location.new(config, data[:location])
87
+ @location ||= Kentaa::Api::Resources::Location.new(data[:location]) if data[:location]
92
88
  end
93
89
 
94
90
  def photos
@@ -97,7 +93,7 @@ module Kentaa
97
93
 
98
94
  if data[:photos]
99
95
  data[:photos].each do |photo|
100
- photos << Kentaa::Api::Resources::Photo.new(config, photo)
96
+ photos << Kentaa::Api::Resources::Photo.new(photo)
101
97
  end
102
98
  end
103
99
 
@@ -111,7 +107,7 @@ module Kentaa
111
107
 
112
108
  if data[:videos]
113
109
  data[:videos].each do |video|
114
- videos << Kentaa::Api::Resources::Video.new(config, video)
110
+ videos << Kentaa::Api::Resources::Video.new(video)
115
111
  end
116
112
  end
117
113
 
@@ -125,7 +121,7 @@ module Kentaa
125
121
 
126
122
  if data[:questions]
127
123
  data[:questions].each do |question|
128
- questions << Kentaa::Api::Resources::Question.new(config, question)
124
+ questions << Kentaa::Api::Resources::Question.new(question)
129
125
  end
130
126
  end
131
127
 
@@ -134,12 +130,30 @@ module Kentaa
134
130
  end
135
131
 
136
132
  def consent
137
- @consent ||= Kentaa::Api::Resources::Consent.new(config, data[:consent]) if data[:consent]
133
+ @consent ||= Kentaa::Api::Resources::Consent.new(data[:consent]) if data[:consent]
134
+ end
135
+
136
+ def contact
137
+ @contact ||= Kentaa::Api::Resources::Contact.new(data[:contact]) if data[:contact]
138
138
  end
139
139
 
140
140
  def external_reference
141
141
  data[:external_reference]
142
142
  end
143
+
144
+ def donations
145
+ @donations ||= Kentaa::Api::Resources::Donations.new(config, project_id: id)
146
+ end
147
+
148
+ def manual_donations
149
+ @manual_donations ||= Kentaa::Api::Resources::ManualDonations.new(config, project_id: id)
150
+ end
151
+
152
+ private
153
+
154
+ def load_resource
155
+ request.get("/projects/#{id}", options)
156
+ end
143
157
  end
144
158
  end
145
159
  end
@@ -3,9 +3,8 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class Projects < Base
6
+ class Projects < List
7
7
  include Enumerable
8
- include Kentaa::Api::Resources::Pagination
9
8
 
10
9
  def each(&block)
11
10
  projects.each(&block)
@@ -13,13 +12,17 @@ module Kentaa
13
12
 
14
13
  private
15
14
 
15
+ def load_resource
16
+ request.get("/projects", options)
17
+ end
18
+
16
19
  def projects
17
20
  @projects ||= begin
18
21
  projects = []
19
22
 
20
23
  if data
21
24
  data.each do |project|
22
- projects << Kentaa::Api::Resources::Project.new(config, project)
25
+ projects << Kentaa::Api::Resources::Project.new(config, data: project)
23
26
  end
24
27
  end
25
28
 
@@ -3,8 +3,16 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class Question < Base
7
- include Kentaa::Api::Resources::Resource
6
+ class Question
7
+ attr_reader :data
8
+
9
+ def initialize(data)
10
+ @data = data
11
+ end
12
+
13
+ def id
14
+ data[:id]
15
+ end
8
16
 
9
17
  def question
10
18
  data[:question]
@@ -3,7 +3,7 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class RegistrationFee < Base
6
+ class RegistrationFee
7
7
  def amount
8
8
  data[:amount]
9
9
  end
@@ -5,9 +5,42 @@ require 'time'
5
5
  module Kentaa
6
6
  module Api
7
7
  module Resources
8
- module Resource
9
- def id
10
- data[:id]
8
+ class Resource < Base
9
+ attr_accessor :id
10
+
11
+ def initialize(config, options = {})
12
+ super(config, options)
13
+
14
+ if options.key?(:data)
15
+ @data = options.delete(:data) || {}
16
+ @id = @data.fetch(:id) if @data.key?(:id)
17
+ elsif options.key?(:id)
18
+ @id = options.delete(:id)
19
+ end
20
+ end
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
+ self
11
44
  end
12
45
 
13
46
  def created_at
@@ -17,6 +50,20 @@ module Kentaa
17
50
  def updated_at
18
51
  Time.parse(data[:updated_at]) if data[:updated_at]
19
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
20
67
  end
21
68
  end
22
69
  end
@@ -3,8 +3,16 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class Reward < Base
7
- include Kentaa::Api::Resources::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
8
16
 
9
17
  def type
10
18
  data[:type]
@@ -6,9 +6,7 @@ require 'time'
6
6
  module Kentaa
7
7
  module Api
8
8
  module Resources
9
- class Segment < Base
10
- include Kentaa::Api::Resources::Resource
11
-
9
+ class Segment < Resource
12
10
  def object_key
13
11
  "Segment_#{id}"
14
12
  end
@@ -63,7 +61,7 @@ module Kentaa
63
61
 
64
62
  if data[:banners]
65
63
  data[:banners].each do |banner|
66
- banners << Kentaa::Api::Resources::Banner.new(config, banner)
64
+ banners << Kentaa::Api::Resources::Banner.new(banner)
67
65
  end
68
66
  end
69
67
 
@@ -74,6 +72,20 @@ module Kentaa
74
72
  def external_reference
75
73
  data[:external_reference]
76
74
  end
75
+
76
+ def donations
77
+ @donations ||= Kentaa::Api::Resources::Donations.new(config, segment_id: id)
78
+ end
79
+
80
+ def manual_donations
81
+ @manual_donations ||= Kentaa::Api::Resources::ManualDonations.new(config, segment_id: id)
82
+ end
83
+
84
+ private
85
+
86
+ def load_resource
87
+ request.get("/segments/#{id}", options)
88
+ end
77
89
  end
78
90
  end
79
91
  end
@@ -3,9 +3,8 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class Segments < Base
6
+ class Segments < List
7
7
  include Enumerable
8
- include Kentaa::Api::Resources::Pagination
9
8
 
10
9
  def each(&block)
11
10
  segments.each(&block)
@@ -13,13 +12,17 @@ module Kentaa
13
12
 
14
13
  private
15
14
 
15
+ def load_resource
16
+ request.get("/segments", options)
17
+ end
18
+
16
19
  def segments
17
20
  @segments ||= begin
18
21
  segments = []
19
22
 
20
23
  if data
21
24
  data.each do |segment|
22
- segments << Kentaa::Api::Resources::Segment.new(config, segment)
25
+ segments << Kentaa::Api::Resources::Segment.new(config, data: segment)
23
26
  end
24
27
  end
25
28