kentaa-api 0.2.0 → 0.4.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 (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
@@ -4,29 +4,53 @@ module Kentaa
4
4
  module Api
5
5
  module Resources
6
6
  class Base
7
- attr_reader :config, :data
7
+ attr_reader :config, :options
8
8
 
9
- def initialize(config, response)
9
+ def initialize(config, options = {})
10
10
  @config = config
11
+ @options = options
12
+ end
11
13
 
12
- if response.respond_to?(:body)
13
- extend Kentaa::Api::Resources::Status
14
+ def load
15
+ @response ||= load_resource
14
16
 
15
- @response = response
16
- @data = response.body[attribute_key]
17
- else
18
- @data = response
19
- end
17
+ self
18
+ end
20
19
 
21
- @data ||= {}
20
+ def loaded?
21
+ !@response.nil?
22
22
  end
23
23
 
24
24
  private
25
25
 
26
26
  def attribute_key
27
- class_name = self.class.name.split("::").last
27
+ class_name = self.class.name.split('::').last
28
28
  class_name.gsub(/([^\^])([A-Z])/, '\1_\2').downcase.to_sym
29
29
  end
30
+
31
+ def load_resource
32
+ raise NotImplementedError
33
+ end
34
+
35
+ def data
36
+ @data ||= begin
37
+ load unless loaded?
38
+
39
+ @response.body[attribute_key] || {}
40
+ end
41
+ end
42
+
43
+ def body
44
+ @body ||= begin
45
+ load unless loaded?
46
+
47
+ @response.body
48
+ end
49
+ end
50
+
51
+ def request
52
+ @request ||= Kentaa::Api::Request.new(config)
53
+ end
30
54
  end
31
55
  end
32
56
  end
@@ -3,7 +3,13 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class Consent < Base
6
+ class Consent
7
+ attr_reader :data
8
+
9
+ def initialize(data)
10
+ @data = data
11
+ end
12
+
7
13
  def url
8
14
  data[:url]
9
15
  end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kentaa
4
+ module Api
5
+ module Resources
6
+ class Contact
7
+ attr_reader :data
8
+
9
+ def initialize(data)
10
+ @data = data
11
+ end
12
+
13
+ def first_name
14
+ data[:first_name]
15
+ end
16
+
17
+ def infix
18
+ data[:infix]
19
+ end
20
+
21
+ def last_name
22
+ data[:last_name]
23
+ end
24
+
25
+ def name
26
+ [first_name, infix, last_name].reject { |s| s.to_s.empty? }.join(" ")
27
+ end
28
+
29
+ def email
30
+ data[:email]
31
+ end
32
+
33
+ def avatar_url
34
+ data[:avatar_url]
35
+ end
36
+
37
+ def address
38
+ data[:address]
39
+ end
40
+
41
+ def address2
42
+ data[:address2]
43
+ end
44
+
45
+ def street
46
+ data[:street]
47
+ end
48
+
49
+ def house_number
50
+ data[:house_number]
51
+ end
52
+
53
+ def house_number_addition
54
+ data[:house_number_addition]
55
+ end
56
+
57
+ def zipcode
58
+ data[:zipcode]
59
+ end
60
+
61
+ def city
62
+ data[:city]
63
+ end
64
+
65
+ def country
66
+ data[:country]
67
+ end
68
+
69
+ def phone
70
+ data[:phone]
71
+ end
72
+
73
+ def birthday
74
+ Time.parse(data[:birthday]) if data[:birthday]
75
+ end
76
+
77
+ def gender
78
+ data[:gender]
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -6,29 +6,22 @@ require 'time'
6
6
  module Kentaa
7
7
  module Api
8
8
  module Resources
9
- class Donation < Base
10
- include Kentaa::Api::Resources::Resource
11
-
9
+ class Donation < Resource
12
10
  def object_key
13
11
  "Donation_#{id}"
14
12
  end
15
13
 
16
14
  def entity
17
15
  if action_id
18
- client = Kentaa::Api::Clients::Actions.new(config)
19
- client.get(action_id)
16
+ Kentaa::Api::Resources::Action.new(config, id: action_id)
20
17
  elsif team_id
21
- client = Kentaa::Api::Clients::Teams.new(config)
22
- client.get(team_id)
18
+ Kentaa::Api::Resources::Team.new(config, id: team_id)
23
19
  elsif project_id
24
- client = Kentaa::Api::Clients::Projects.new(config)
25
- client.get(project_id)
20
+ Kentaa::Api::Resources::Project.new(config, id: project_id)
26
21
  elsif segment_id
27
- client = Kentaa::Api::Clients::Segments.new(config)
28
- client.get(segment_id)
22
+ Kentaa::Api::Resources::Segment.new(config, id: segment_id)
29
23
  else
30
- client = Kentaa::Api::Clients::Sites.new(config)
31
- client.current
24
+ Kentaa::Api::Resources::Site.new(config, id: site_id)
32
25
  end
33
26
  end
34
27
 
@@ -65,7 +58,7 @@ module Kentaa
65
58
  end
66
59
 
67
60
  def name
68
- [first_name, infix, last_name].compact.join(" ")
61
+ [first_name, infix, last_name].reject { |s| s.to_s.empty? }.join(" ")
69
62
  end
70
63
 
71
64
  def company
@@ -108,6 +101,10 @@ module Kentaa
108
101
  BigDecimal(data[:transaction_costs]) if data[:transaction_costs]
109
102
  end
110
103
 
104
+ def start_donation?
105
+ data[:start_donation]
106
+ end
107
+
111
108
  def registration_fee?
112
109
  data[:registration_fee]
113
110
  end
@@ -120,6 +117,10 @@ module Kentaa
120
117
  BigDecimal(data[:total_amount])
121
118
  end
122
119
 
120
+ def receivable_amount
121
+ BigDecimal(data[:receivable_amount])
122
+ end
123
+
123
124
  def countable?
124
125
  data[:countable]
125
126
  end
@@ -174,7 +175,7 @@ module Kentaa
174
175
 
175
176
  if data[:questions]
176
177
  data[:questions].each do |question|
177
- questions << Kentaa::Api::Resources::Question.new(config, question)
178
+ questions << Kentaa::Api::Resources::Question.new(question)
178
179
  end
179
180
  end
180
181
 
@@ -183,11 +184,11 @@ module Kentaa
183
184
  end
184
185
 
185
186
  def reward
186
- @reward ||= Kentaa::Api::Resources::Reward.new(config, data[:reward]) if data[:reward]
187
+ @reward ||= Kentaa::Api::Resources::Reward.new(data[:reward]) if data[:reward]
187
188
  end
188
189
 
189
190
  def address
190
- @address ||= Kentaa::Api::Resources::Address.new(config, data[:address]) if data[:address]
191
+ @address ||= Kentaa::Api::Resources::Address.new(data[:address]) if data[:address]
191
192
  end
192
193
 
193
194
  def birthday
@@ -203,7 +204,13 @@ module Kentaa
203
204
  end
204
205
 
205
206
  def consent
206
- @consent ||= Kentaa::Api::Resources::Consent.new(config, data[:consent]) if data[:consent]
207
+ @consent ||= Kentaa::Api::Resources::Consent.new(data[:consent]) if data[:consent]
208
+ end
209
+
210
+ private
211
+
212
+ def load_resource
213
+ request.get("/donations/#{id}", options)
207
214
  end
208
215
  end
209
216
  end
@@ -3,9 +3,8 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class Donations < Base
6
+ class Donations < List
7
7
  include Enumerable
8
- include Kentaa::Api::Resources::Pagination
9
8
 
10
9
  def each(&block)
11
10
  donations.each(&block)
@@ -13,13 +12,17 @@ module Kentaa
13
12
 
14
13
  private
15
14
 
15
+ def load_resource
16
+ request.get("/donations", options)
17
+ end
18
+
16
19
  def donations
17
20
  @donations ||= begin
18
21
  donations = []
19
22
 
20
23
  if data
21
24
  data.each do |donation|
22
- donations << Kentaa::Api::Resources::Donation.new(config, donation)
25
+ donations << Kentaa::Api::Resources::Donation.new(config, data: donation)
23
26
  end
24
27
  end
25
28
 
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kentaa
4
+ module Api
5
+ module Resources
6
+ class Error
7
+ attr_reader :data
8
+
9
+ def initialize(data)
10
+ @data = data
11
+ end
12
+
13
+ def field
14
+ data[:field]
15
+ end
16
+
17
+ def error
18
+ data[:error]
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -3,9 +3,7 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- module Pagination
7
- attr_accessor :body
8
-
6
+ class List < Base
9
7
  def links
10
8
  body[:links]
11
9
  end
@@ -45,6 +43,31 @@ module Kentaa
45
43
  def previous_page?
46
44
  current_page && current_page > 1
47
45
  end
46
+
47
+ def next
48
+ self.class.new(config, options.merge(page: next_page)) if next_page?
49
+ end
50
+
51
+ def previous
52
+ self.class.new(config, options.merge(page: previous_page)) if previous_page?
53
+ end
54
+
55
+ def all
56
+ enumerator = Enumerator.new do |yielder|
57
+ page = 1
58
+
59
+ loop do
60
+ response = self.class.new(config, options.merge(page: page))
61
+ response.each { |item| yielder.yield item }
62
+
63
+ raise StopIteration unless response.next_page?
64
+
65
+ page = response.next_page
66
+ end
67
+ end
68
+
69
+ enumerator.lazy
70
+ end
48
71
  end
49
72
  end
50
73
  end
@@ -3,7 +3,13 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class Location < Base
6
+ class Location
7
+ attr_reader :data
8
+
9
+ def initialize(data)
10
+ @data = data
11
+ end
12
+
7
13
  def zip_code
8
14
  data[:zip_code]
9
15
  end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bigdecimal'
4
+ require 'time'
5
+
6
+ module Kentaa
7
+ module Api
8
+ module Resources
9
+ class ManualDonation < Resource
10
+ def object_key
11
+ "Donation_#{id}"
12
+ end
13
+
14
+ def entity
15
+ if action_id
16
+ Kentaa::Api::Resources::Action.new(config, id: action_id)
17
+ elsif team_id
18
+ Kentaa::Api::Resources::Team.new(config, id: team_id)
19
+ elsif project_id
20
+ Kentaa::Api::Resources::Project.new(config, id: project_id)
21
+ elsif segment_id
22
+ Kentaa::Api::Resources::Segment.new(config, id: segment_id)
23
+ else
24
+ Kentaa::Api::Resources::Site.new(config, id: site_id)
25
+ end
26
+ end
27
+
28
+ def site_id
29
+ data[:site_id]
30
+ end
31
+
32
+ def segment_id
33
+ data[:segment_id]
34
+ end
35
+
36
+ def project_id
37
+ data[:project_id]
38
+ end
39
+
40
+ def team_id
41
+ data[:team_id]
42
+ end
43
+
44
+ def action_id
45
+ data[:action_id]
46
+ end
47
+
48
+ def first_name
49
+ data[:first_name]
50
+ end
51
+
52
+ def infix
53
+ data[:infix]
54
+ end
55
+
56
+ def last_name
57
+ data[:last_name]
58
+ end
59
+
60
+ def name
61
+ [first_name, infix, last_name].reject { |s| s.to_s.empty? }.join(" ")
62
+ end
63
+
64
+ def anonymous?
65
+ data[:anonymous]
66
+ end
67
+
68
+ def email
69
+ data[:email]
70
+ end
71
+
72
+ def message
73
+ data[:message]
74
+ end
75
+
76
+ def currency
77
+ data[:currency]
78
+ end
79
+
80
+ def amount
81
+ BigDecimal(data[:amount])
82
+ end
83
+
84
+ def countable?
85
+ data[:countable]
86
+ end
87
+
88
+ def target_url
89
+ data[:target_url]
90
+ end
91
+
92
+ private
93
+
94
+ def load_resource
95
+ request.get("/manual-donations/#{id}", options)
96
+ end
97
+
98
+ def create_resource(attributes)
99
+ request.post("/manual-donations", options, attributes)
100
+ end
101
+
102
+ def update_resource(attributes)
103
+ request.patch("/manual-donations/#{id}", options, attributes)
104
+ end
105
+
106
+ def delete_resource
107
+ request.delete("/manual-donations/#{id}", options)
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end