kentaa-api 0.4.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +414 -66
  4. data/lib/kentaa/api/client.rb +32 -20
  5. data/lib/kentaa/api/config.rb +13 -6
  6. data/lib/kentaa/api/deprecation.rb +13 -0
  7. data/lib/kentaa/api/exception.rb +5 -2
  8. data/lib/kentaa/api/request.rb +37 -19
  9. data/lib/kentaa/api/resources/action.rb +50 -11
  10. data/lib/kentaa/api/resources/base.rb +18 -5
  11. data/lib/kentaa/api/resources/billing.rb +51 -0
  12. data/lib/kentaa/api/resources/company.rb +218 -0
  13. data/lib/kentaa/api/resources/company_package.rb +39 -0
  14. data/lib/kentaa/api/resources/consent.rb +77 -4
  15. data/lib/kentaa/api/resources/contact.rb +2 -2
  16. data/lib/kentaa/api/resources/donation.rb +45 -7
  17. data/lib/kentaa/api/resources/donation_form.rb +104 -0
  18. data/lib/kentaa/api/resources/list.rb +61 -3
  19. data/lib/kentaa/api/resources/manual_donation.rb +17 -7
  20. data/lib/kentaa/api/resources/newsletter_subscription.rb +30 -4
  21. data/lib/kentaa/api/resources/performance.rb +70 -0
  22. data/lib/kentaa/api/resources/performance_photo.rb +36 -0
  23. data/lib/kentaa/api/resources/project.rb +36 -4
  24. data/lib/kentaa/api/resources/question.rb +8 -0
  25. data/lib/kentaa/api/resources/recurring_donor.rb +110 -0
  26. data/lib/kentaa/api/resources/resource.rb +6 -6
  27. data/lib/kentaa/api/resources/segment.rb +26 -2
  28. data/lib/kentaa/api/resources/site.rb +7 -3
  29. data/lib/kentaa/api/{clients → resources}/sites.rb +2 -2
  30. data/lib/kentaa/api/resources/team.rb +11 -7
  31. data/lib/kentaa/api/resources/user.rb +27 -3
  32. data/lib/kentaa/api/resources/users.rb +5 -27
  33. data/lib/kentaa/api/util.rb +17 -0
  34. data/lib/kentaa/api/version.rb +1 -1
  35. data/lib/kentaa/api.rb +41 -50
  36. metadata +23 -46
  37. data/.gitignore +0 -16
  38. data/.rspec +0 -2
  39. data/.rubocop.yml +0 -64
  40. data/.travis.yml +0 -12
  41. data/Gemfile +0 -10
  42. data/Gemfile.lock +0 -71
  43. data/Rakefile +0 -10
  44. data/bin/console +0 -15
  45. data/bin/setup +0 -8
  46. data/kentaa-api.gemspec +0 -30
  47. data/lib/kentaa/api/clients/actions.rb +0 -34
  48. data/lib/kentaa/api/clients/base.rb +0 -15
  49. data/lib/kentaa/api/clients/donations.rb +0 -24
  50. data/lib/kentaa/api/clients/manual_donations.rb +0 -39
  51. data/lib/kentaa/api/clients/newsletter_subscriptions.rb +0 -24
  52. data/lib/kentaa/api/clients/projects.rb +0 -24
  53. data/lib/kentaa/api/clients/segments.rb +0 -24
  54. data/lib/kentaa/api/clients/teams.rb +0 -24
  55. data/lib/kentaa/api/clients/users.rb +0 -34
  56. data/lib/kentaa/api/finder.rb +0 -44
  57. data/lib/kentaa/api/resources/actions.rb +0 -40
  58. data/lib/kentaa/api/resources/donations.rb +0 -35
  59. data/lib/kentaa/api/resources/manual_donations.rb +0 -40
  60. data/lib/kentaa/api/resources/newsletter_subscriptions.rb +0 -35
  61. data/lib/kentaa/api/resources/projects.rb +0 -35
  62. data/lib/kentaa/api/resources/segments.rb +0 -35
  63. data/lib/kentaa/api/resources/teams.rb +0 -35
@@ -3,17 +3,20 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  class Config
6
- LIVE_URL = "https://api.kentaa.nl/v1"
7
- TEST_URL = "https://api.kentaa.staatklaar.nu/v1"
8
- DEV_URL = "http://api.lvh.me:3000/v1"
6
+ LIVE_URL = 'https://api.kentaa.nl/v1'
7
+ TEST_URL = 'https://api.kentaa.staatklaar.nu/v1'
8
+ DEV_URL = 'http://api.lvh.me:3000/v1'
9
9
 
10
- attr_accessor :api_key, :options
10
+ attr_accessor :options
11
11
 
12
- def initialize(api_key, options = {})
13
- @api_key = api_key
12
+ def initialize(options = {})
14
13
  @options = options
15
14
  end
16
15
 
16
+ def api_key
17
+ options.fetch(:api_key)
18
+ end
19
+
17
20
  def api_url
18
21
  case environment
19
22
  when :test
@@ -34,6 +37,10 @@ module Kentaa
34
37
  :live
35
38
  end
36
39
  end
40
+
41
+ def debug?
42
+ options.fetch(:debug, false)
43
+ end
37
44
  end
38
45
  end
39
46
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kentaa
4
+ module Api
5
+ module Deprecation
6
+ module_function
7
+
8
+ def warn(message, callstack = caller)
9
+ Kernel.warn("DEPRECATION WARNING: #{message} (called from #{callstack.first})")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -10,8 +10,7 @@ module Kentaa
10
10
 
11
11
  def initialize(response)
12
12
  @response = response
13
-
14
- super(response.message)
13
+ super()
15
14
  end
16
15
 
17
16
  def errors
@@ -21,6 +20,10 @@ module Kentaa
21
20
  def http_code
22
21
  response.http_code
23
22
  end
23
+
24
+ def to_s
25
+ "#{response.http_code}: #{response.message}"
26
+ end
24
27
  end
25
28
  end
26
29
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'logger'
3
4
  require 'net/http'
4
5
  require 'uri'
5
6
 
@@ -12,50 +13,63 @@ module Kentaa
12
13
  @config = config
13
14
  end
14
15
 
15
- def get(path, params = {})
16
- request(:get, path, params)
16
+ def get(path, params = {}, options = {})
17
+ request(:get, path, params, nil, options)
17
18
  end
18
19
 
19
- def post(path, params = {}, body = {})
20
- request(:post, path, params, body)
20
+ def post(path, params = {}, body = {}, options = {})
21
+ request(:post, path, params, body, options)
21
22
  end
22
23
 
23
- def patch(path, params = {}, body = {})
24
- request(:patch, path, params, body)
24
+ def patch(path, params = {}, body = {}, options = {})
25
+ request(:patch, path, params, body, options)
25
26
  end
26
27
 
27
- def delete(path, params = {})
28
- request(:delete, path, params)
28
+ def delete(path, params = {}, options = {})
29
+ request(:delete, path, params, nil, options)
29
30
  end
30
31
 
31
32
  private
32
33
 
33
- def request(http_method, path, params = {}, body = {})
34
+ def request(http_method, path, params = {}, body = nil, options = {})
34
35
  uri = URI.parse(File.join(config.api_url, path))
35
36
  uri.query = URI.encode_www_form(params) unless params.empty?
36
37
 
38
+ content_type = options.fetch(:content_type, 'application/json')
39
+
40
+ # Body can be passed as an IO-like object or an object that can be serialized to JSON.
41
+ if body
42
+ if body.respond_to?(:read)
43
+ body = body.read
44
+ elsif body.respond_to?(:to_json)
45
+ body = body.to_json
46
+ end
47
+ end
48
+
37
49
  case http_method
38
50
  when :get
39
51
  request = Net::HTTP::Get.new(uri)
40
52
  when :post
41
53
  request = Net::HTTP::Post.new(uri)
42
- request.body = body.to_json
54
+ request.body = body
43
55
  when :patch
44
56
  request = Net::HTTP::Patch.new(uri)
45
- request.body = body.to_json
57
+ request.body = body
46
58
  when :delete
47
59
  request = Net::HTTP::Delete.new(uri)
48
60
  else
49
61
  raise Kentaa::Api::Exception, "Invalid HTTP method: #{http_method}"
50
62
  end
51
63
 
52
- request["Accept"] = "application/json"
53
- request["Content-Type"] = "application/json"
54
- request["X-Api-Key"] = config.api_key
55
- request["User-Agent"] = "Ruby kentaa-api/#{Kentaa::Api::VERSION}"
64
+ logger.debug("[Kentaa-API] Request: #{http_method.upcase} #{uri}") if config.debug?
65
+
66
+ request['Accept'] = 'application/json'
67
+ request['Content-Type'] = content_type
68
+ request['X-Api-Key'] = config.api_key
69
+ request['User-Agent'] = "Ruby kentaa-api/#{Kentaa::Api::VERSION}"
56
70
 
57
71
  client = Net::HTTP.new(uri.hostname, uri.port)
58
- client.use_ssl = uri.scheme == "https"
72
+ client.use_ssl = uri.scheme == 'https'
59
73
  client.verify_mode = OpenSSL::SSL::VERIFY_PEER
60
74
 
61
75
  begin
@@ -67,12 +81,16 @@ module Kentaa
67
81
  raise Kentaa::Api::Exception, e.message
68
82
  end
69
83
 
70
- if response.error?
71
- raise Kentaa::Api::RequestError, response
72
- end
84
+ logger.debug("[Kentaa-API] Response: #{response.http_code}, body: #{response.body}") if config.debug?
85
+
86
+ raise Kentaa::Api::RequestError, response if response.error?
73
87
 
74
88
  response
75
89
  end
90
+
91
+ def logger
92
+ @logger ||= Logger.new($stdout)
93
+ end
76
94
  end
77
95
  end
78
96
  end
@@ -7,22 +7,29 @@ module Kentaa
7
7
  module Api
8
8
  module Resources
9
9
  class Action < Resource
10
+ WHO_TYPE_OWNER = 'owner'
11
+ WHO_TYPE_OTHER = 'other'
12
+
10
13
  def object_key
11
14
  "Action_#{id}"
12
15
  end
13
16
 
14
17
  def parent
15
18
  if team_id
16
- Kentaa::Api::Resources::Team.new(config, id: team_id)
19
+ Kentaa::Api::Resources::Team.new(config, id: team_id, options: options)
17
20
  elsif project_id
18
- Kentaa::Api::Resources::Project.new(config, id: project_id)
21
+ Kentaa::Api::Resources::Project.new(config, id: project_id, options: options)
19
22
  elsif segment_id
20
- Kentaa::Api::Resources::Segment.new(config, id: segment_id)
23
+ Kentaa::Api::Resources::Segment.new(config, id: segment_id, options: options)
21
24
  else
22
- Kentaa::Api::Resources::Site.new(config, id: site_id)
25
+ Kentaa::Api::Resources::Site.new(config, id: site_id, options: options)
23
26
  end
24
27
  end
25
28
 
29
+ def site
30
+ Kentaa::Api::Resources::Site.new(config, id: site_id, options: options)
31
+ end
32
+
26
33
  def slug
27
34
  data[:slug]
28
35
  end
@@ -44,11 +51,23 @@ module Kentaa
44
51
  end
45
52
 
46
53
  def owner
47
- @owner ||= Kentaa::Api::Resources::User.new(config, data: data[:owner])
54
+ @owner ||= Kentaa::Api::Resources::User.new(config, data: data[:owner], options: options)
48
55
  end
49
56
 
50
57
  def team_captain?
51
- data[:team_captain]
58
+ data.fetch(:team_captain, false)
59
+ end
60
+
61
+ def who_type
62
+ data[:who_type]
63
+ end
64
+
65
+ def owner?
66
+ who_type == WHO_TYPE_OWNER
67
+ end
68
+
69
+ def other?
70
+ who_type == WHO_TYPE_OTHER
52
71
  end
53
72
 
54
73
  def first_name
@@ -68,7 +87,7 @@ module Kentaa
68
87
  end
69
88
 
70
89
  def name
71
- [first_name, infix, last_name].reject { |s| s.to_s.empty? }.join(" ")
90
+ [first_name, infix, last_name].reject { |s| s.to_s.empty? }.join(' ')
72
91
  end
73
92
 
74
93
  def fundraiser_page?
@@ -120,7 +139,7 @@ module Kentaa
120
139
  end
121
140
 
122
141
  def activity
123
- @activity ||= Kentaa::Api::Resources::Activity.new(data[:activity])
142
+ @activity ||= Kentaa::Api::Resources::Activity.new(data[:activity]) if data[:activity]
124
143
  end
125
144
 
126
145
  def previous_participations
@@ -186,19 +205,39 @@ module Kentaa
186
205
  end
187
206
 
188
207
  def consent
208
+ Kentaa::Api::Deprecation.warn('#consent is deprecated. Please use #consents instead.', caller)
209
+
189
210
  @consent ||= Kentaa::Api::Resources::Consent.new(data[:consent]) if data[:consent]
190
211
  end
191
212
 
213
+ def consents
214
+ @consents ||= begin
215
+ consents = []
216
+
217
+ if data[:consents]
218
+ data[:consents].each do |consent|
219
+ consents << Kentaa::Api::Resources::Consent.new(consent)
220
+ end
221
+ end
222
+
223
+ consents
224
+ end
225
+ end
226
+
192
227
  def external_reference
193
228
  data[:external_reference]
194
229
  end
195
230
 
196
231
  def donations
197
- @donations ||= Kentaa::Api::Resources::Donations.new(config, action_id: id)
232
+ @donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Donation, endpoint_path: "/actions/#{id}/donations")
198
233
  end
199
234
 
200
235
  def manual_donations
201
- @manual_donations ||= Kentaa::Api::Resources::ManualDonations.new(config, action_id: id)
236
+ @manual_donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::ManualDonation, endpoint_path: "/actions/#{id}/manual-donations")
237
+ end
238
+
239
+ def performances(options = {})
240
+ @performances ||= Kentaa::Api::Resources::List.new(config, options.merge(resource_class: Kentaa::Api::Resources::Performance, endpoint_path: "/actions/#{id}/performances"))
202
241
  end
203
242
 
204
243
  private
@@ -208,7 +247,7 @@ module Kentaa
208
247
  end
209
248
 
210
249
  def create_resource(attributes)
211
- request.post("/actions", options, attributes)
250
+ request.post('/actions', options, attributes)
212
251
  end
213
252
 
214
253
  def update_resource(attributes)
@@ -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
 
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kentaa
4
+ module Api
5
+ module Resources
6
+ class Billing
7
+ attr_reader :data
8
+
9
+ def initialize(data)
10
+ @data = data
11
+ end
12
+
13
+ def attn
14
+ data[:attn]
15
+ end
16
+
17
+ def street
18
+ data[:street]
19
+ end
20
+
21
+ def house_number
22
+ data[:house_number]
23
+ end
24
+
25
+ def house_number_addition
26
+ data[:house_number_addition]
27
+ end
28
+
29
+ def address
30
+ data[:address]
31
+ end
32
+
33
+ def address2
34
+ data[:address2]
35
+ end
36
+
37
+ def zipcode
38
+ data[:zipcode]
39
+ end
40
+
41
+ def city
42
+ data[:city]
43
+ end
44
+
45
+ def country
46
+ data[:country]
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,218 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bigdecimal'
4
+ require 'time'
5
+
6
+ module Kentaa
7
+ module Api
8
+ module Resources
9
+ class Company < Resource
10
+ def object_key
11
+ "Company_#{id}"
12
+ end
13
+
14
+ def parent
15
+ if project_id
16
+ Kentaa::Api::Resources::Project.new(config, id: project_id, options: options)
17
+ elsif segment_id
18
+ Kentaa::Api::Resources::Segment.new(config, id: segment_id, options: options)
19
+ else
20
+ Kentaa::Api::Resources::Site.new(config, id: site_id, options: options)
21
+ end
22
+ end
23
+
24
+ def site
25
+ Kentaa::Api::Resources::Site.new(config, id: site_id, options: options)
26
+ end
27
+
28
+ def slug
29
+ data[:slug]
30
+ end
31
+
32
+ def site_id
33
+ data[:site_id]
34
+ end
35
+
36
+ def segment_id
37
+ data[:segment_id]
38
+ end
39
+
40
+ def project_id
41
+ data[:project_id]
42
+ end
43
+
44
+ def owner
45
+ @owner ||= Kentaa::Api::Resources::User.new(config, data: data[:owner], options: options)
46
+ end
47
+
48
+ def members
49
+ @members ||= begin
50
+ members = []
51
+
52
+ if data[:members]
53
+ data[:members].each do |member|
54
+ members << Kentaa::Api::Resources::Action.new(config, data: member, options: options)
55
+ end
56
+ end
57
+
58
+ members
59
+ end
60
+ end
61
+
62
+ def name
63
+ data[:name]
64
+ end
65
+
66
+ def title
67
+ data[:title]
68
+ end
69
+
70
+ def description
71
+ data[:description]
72
+ end
73
+
74
+ def target_amount
75
+ data[:target_amount]
76
+ end
77
+
78
+ def total_amount
79
+ BigDecimal(data[:total_amount])
80
+ end
81
+
82
+ def total_donations
83
+ data[:total_donations]
84
+ end
85
+
86
+ def target_amount_achieved?
87
+ data[:target_amount_achieved]
88
+ end
89
+
90
+ def visible?
91
+ data[:visible]
92
+ end
93
+
94
+ def countable?
95
+ data[:countable]
96
+ end
97
+
98
+ def closed?
99
+ data[:closed]
100
+ end
101
+
102
+ def ended?
103
+ data[:ended]
104
+ end
105
+
106
+ def end_date
107
+ Time.parse(data[:end_date]) if data[:end_date]
108
+ end
109
+
110
+ def url
111
+ data[:url]
112
+ end
113
+
114
+ def donate_url
115
+ data[:donate_url]
116
+ end
117
+
118
+ def member_sign_up_url
119
+ data[:member_sign_up_url]
120
+ end
121
+
122
+ def photos
123
+ @photos ||= begin
124
+ photos = []
125
+
126
+ if data[:photos]
127
+ data[:photos].each do |photo|
128
+ photos << Kentaa::Api::Resources::Photo.new(photo)
129
+ end
130
+ end
131
+
132
+ photos
133
+ end
134
+ end
135
+
136
+ def videos
137
+ @videos ||= begin
138
+ videos = []
139
+
140
+ if data[:videos]
141
+ data[:videos].each do |video|
142
+ videos << Kentaa::Api::Resources::Video.new(video)
143
+ end
144
+ end
145
+
146
+ videos
147
+ end
148
+ end
149
+
150
+ def questions
151
+ @questions ||= begin
152
+ questions = []
153
+
154
+ if data[:questions]
155
+ data[:questions].each do |question|
156
+ questions << Kentaa::Api::Resources::Question.new(question)
157
+ end
158
+ end
159
+
160
+ questions
161
+ end
162
+ end
163
+
164
+ def company_package
165
+ @company_package ||= Kentaa::Api::Resources::CompanyPackage.new(data[:package]) if data[:package]
166
+ end
167
+
168
+ def activity
169
+ @activity ||= Kentaa::Api::Resources::Activity.new(data[:activity]) if data[:activity]
170
+ end
171
+
172
+ def contact
173
+ @contact ||= Kentaa::Api::Resources::Contact.new(data[:contact]) if data[:contact]
174
+ end
175
+
176
+ def external_reference
177
+ data[:external_reference]
178
+ end
179
+
180
+ def commerce_number
181
+ data[:commerce_number]
182
+ end
183
+
184
+ def consents
185
+ @consents ||= begin
186
+ consents = []
187
+
188
+ if data[:consents]
189
+ data[:consents].each do |consent|
190
+ consents << Kentaa::Api::Resources::Consent.new(consent)
191
+ end
192
+ end
193
+
194
+ consents
195
+ end
196
+ end
197
+
198
+ def billing
199
+ @billing ||= Kentaa::Api::Resources::Billing.new(data[:billing]) if data[:billing]
200
+ end
201
+
202
+ def donations
203
+ @donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Donation, endpoint_path: "/companies/#{id}/donations")
204
+ end
205
+
206
+ def manual_donations
207
+ @manual_donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::ManualDonation, endpoint_path: "/companies/#{id}/manual-donations")
208
+ end
209
+
210
+ private
211
+
212
+ def load_resource
213
+ request.get("/companies/#{id}", options)
214
+ end
215
+ end
216
+ end
217
+ end
218
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kentaa
4
+ module Api
5
+ module Resources
6
+ class CompanyPackage
7
+ attr_reader :data
8
+
9
+ def initialize(data)
10
+ @data = data
11
+ end
12
+
13
+ def id
14
+ data[:id]
15
+ end
16
+
17
+ def created_at
18
+ Time.parse(data[:created_at]) if data[:created_at]
19
+ end
20
+
21
+ def updated_at
22
+ Time.parse(data[:updated_at]) if data[:updated_at]
23
+ end
24
+
25
+ def amount
26
+ data[:amount]
27
+ end
28
+
29
+ def title
30
+ data[:title]
31
+ end
32
+
33
+ def description
34
+ data[:description]
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end