kentaa-api 0.3.0 → 0.6.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/.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
data/kentaa-api.gemspec CHANGED
@@ -21,10 +21,10 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
- spec.required_ruby_version = ">= 2.0.0"
24
+ spec.required_ruby_version = ">= 2.4.0"
25
25
 
26
- spec.add_development_dependency "bundler", "~> 1.14"
27
- spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "bundler", "~> 2.0"
27
+ spec.add_development_dependency "rake", "~> 13.0"
28
28
  spec.add_development_dependency "rspec", "~> 3.0"
29
29
  spec.add_development_dependency "webmock", "~> 2.3", ">= 2.3.2"
30
30
  end
data/lib/kentaa/api.rb CHANGED
@@ -1,43 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "api/clients/base"
4
- require_relative "api/clients/all"
5
-
6
- require_relative "api/clients/actions"
7
- require_relative "api/clients/donations"
8
- require_relative "api/clients/newsletter_subscriptions"
9
- require_relative "api/clients/projects"
10
- require_relative "api/clients/segments"
11
- require_relative "api/clients/sites"
12
- require_relative "api/clients/teams"
13
- require_relative "api/clients/users"
14
-
15
3
  require_relative "api/resources/base"
4
+ require_relative "api/resources/error"
16
5
  require_relative "api/resources/list"
17
6
  require_relative "api/resources/resource"
18
7
 
19
8
  require_relative "api/resources/action"
20
- require_relative "api/resources/actions"
21
9
  require_relative "api/resources/activity"
22
10
  require_relative "api/resources/address"
23
11
  require_relative "api/resources/banner"
24
12
  require_relative "api/resources/consent"
13
+ require_relative "api/resources/contact"
14
+ require_relative "api/resources/donation_form"
25
15
  require_relative "api/resources/donation"
26
- require_relative "api/resources/donations"
27
16
  require_relative "api/resources/location"
17
+ require_relative "api/resources/manual_donation"
28
18
  require_relative "api/resources/newsletter_subscription"
29
- require_relative "api/resources/newsletter_subscriptions"
19
+ require_relative "api/resources/performance"
30
20
  require_relative "api/resources/photo"
31
21
  require_relative "api/resources/project"
32
- require_relative "api/resources/projects"
33
- require_relative "api/resources/question"
22
+ require_relative "api/resources/recurring_donor"
34
23
  require_relative "api/resources/registration_fee"
35
24
  require_relative "api/resources/reward"
25
+ require_relative "api/resources/question"
36
26
  require_relative "api/resources/segment"
37
- require_relative "api/resources/segments"
38
27
  require_relative "api/resources/site"
28
+ require_relative "api/resources/sites"
39
29
  require_relative "api/resources/team"
40
- require_relative "api/resources/teams"
41
30
  require_relative "api/resources/user"
42
31
  require_relative "api/resources/users"
43
32
  require_relative "api/resources/video"
@@ -45,8 +34,8 @@ require_relative "api/resources/video"
45
34
  require_relative "api/client"
46
35
  require_relative "api/config"
47
36
  require_relative "api/exception"
48
- require_relative "api/finder"
49
37
  require_relative "api/request"
50
38
  require_relative "api/response"
39
+ require_relative "api/util"
51
40
 
52
41
  require_relative "api/version"
@@ -3,40 +3,52 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  class Client
6
- def initialize(config)
7
- @config = config
6
+ def initialize(options = {})
7
+ @config = Kentaa::Api::Config.new(options)
8
8
  end
9
9
 
10
- def actions
11
- Kentaa::Api::Clients::Actions.new(@config)
10
+ def actions(options = {})
11
+ Kentaa::Api::Resources::List.new(@config, options.merge(resource_class: Kentaa::Api::Resources::Action, endpoint_path: "/actions"))
12
12
  end
13
13
 
14
- def donations
15
- Kentaa::Api::Clients::Donations.new(@config)
14
+ def donations(options = {})
15
+ Kentaa::Api::Resources::List.new(@config, options.merge(resource_class: Kentaa::Api::Resources::Donation, endpoint_path: "/donations"))
16
16
  end
17
17
 
18
- def newsletter_subscriptions
19
- Kentaa::Api::Clients::NewsletterSubscriptions.new(@config)
18
+ def donation_forms(options = {})
19
+ Kentaa::Api::Resources::List.new(@config, options.merge(resource_class: Kentaa::Api::Resources::DonationForm, endpoint_path: "/donation-forms"))
20
20
  end
21
21
 
22
- def projects
23
- Kentaa::Api::Clients::Projects.new(@config)
22
+ def manual_donations(options = {})
23
+ Kentaa::Api::Resources::List.new(@config, options.merge(resource_class: Kentaa::Api::Resources::ManualDonation, endpoint_path: "/manual-donations"))
24
24
  end
25
25
 
26
- def segments
27
- Kentaa::Api::Clients::Segments.new(@config)
26
+ def newsletter_subscriptions(options = {})
27
+ Kentaa::Api::Resources::List.new(@config, options.merge(resource_class: Kentaa::Api::Resources::NewsletterSubscription, endpoint_path: "/newsletter-subscriptions"))
28
28
  end
29
29
 
30
- def sites
31
- Kentaa::Api::Clients::Sites.new(@config)
30
+ def projects(options = {})
31
+ Kentaa::Api::Resources::List.new(@config, options.merge(resource_class: Kentaa::Api::Resources::Project, endpoint_path: "/projects"))
32
32
  end
33
33
 
34
- def teams
35
- Kentaa::Api::Clients::Teams.new(@config)
34
+ def recurring_donors(options = {})
35
+ Kentaa::Api::Resources::List.new(@config, options.merge(resource_class: Kentaa::Api::Resources::RecurringDonor, endpoint_path: "/recurring-donors"))
36
36
  end
37
37
 
38
- def users
39
- Kentaa::Api::Clients::Users.new(@config)
38
+ def segments(options = {})
39
+ Kentaa::Api::Resources::List.new(@config, options.merge(resource_class: Kentaa::Api::Resources::Segment, endpoint_path: "/segments"))
40
+ end
41
+
42
+ def sites(options = {})
43
+ Kentaa::Api::Resources::Sites.new(@config, options)
44
+ end
45
+
46
+ def teams(options = {})
47
+ Kentaa::Api::Resources::List.new(@config, options.merge(resource_class: Kentaa::Api::Resources::Team, endpoint_path: "/teams"))
48
+ end
49
+
50
+ def users(options = {})
51
+ Kentaa::Api::Resources::Users.new(@config, options)
40
52
  end
41
53
  end
42
54
  end
@@ -7,13 +7,16 @@ module Kentaa
7
7
  TEST_URL = "https://api.kentaa.staatklaar.nu/v1"
8
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
@@ -10,12 +10,19 @@ module Kentaa
10
10
 
11
11
  def initialize(response)
12
12
  @response = response
13
+ super()
14
+ end
13
15
 
14
- super(response.message)
16
+ def errors
17
+ response.errors
15
18
  end
16
19
 
17
20
  def http_code
18
- response.code
21
+ response.http_code
22
+ end
23
+
24
+ def to_s
25
+ "#{response.http_code}: #{response.message}"
19
26
  end
20
27
  end
21
28
  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
 
@@ -13,16 +14,55 @@ module Kentaa
13
14
  end
14
15
 
15
16
  def get(path, params = {})
17
+ request(:get, path, params)
18
+ end
19
+
20
+ def post(path, params = {}, body = {})
21
+ request(:post, path, params, body)
22
+ end
23
+
24
+ def patch(path, params = {}, body = {})
25
+ request(:patch, path, params, body)
26
+ end
27
+
28
+ def delete(path, params = {})
29
+ request(:delete, path, params)
30
+ end
31
+
32
+ private
33
+
34
+ def request(http_method, path, params = {}, body = {})
16
35
  uri = URI.parse(File.join(config.api_url, path))
17
36
  uri.query = URI.encode_www_form(params) unless params.empty?
18
37
 
19
- request = Net::HTTP::Get.new(uri)
38
+ case http_method
39
+ when :get
40
+ request = Net::HTTP::Get.new(uri)
41
+ when :post
42
+ request = Net::HTTP::Post.new(uri)
43
+ request.body = body.to_json
44
+ when :patch
45
+ request = Net::HTTP::Patch.new(uri)
46
+ request.body = body.to_json
47
+ when :delete
48
+ request = Net::HTTP::Delete.new(uri)
49
+ else
50
+ raise Kentaa::Api::Exception, "Invalid HTTP method: #{http_method}"
51
+ end
52
+
53
+ logger.debug("[Kentaa-API] Request: #{http_method.upcase} #{uri}") if config.debug?
54
+
55
+ request["Accept"] = "application/json"
56
+ request["Content-Type"] = "application/json"
20
57
  request["X-Api-Key"] = config.api_key
58
+ request["User-Agent"] = "Ruby kentaa-api/#{Kentaa::Api::VERSION}"
59
+
60
+ client = Net::HTTP.new(uri.hostname, uri.port)
61
+ client.use_ssl = uri.scheme == "https"
62
+ client.verify_mode = OpenSSL::SSL::VERIFY_PEER
21
63
 
22
64
  begin
23
- result = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
24
- http.request(request)
25
- end
65
+ response = Kentaa::Api::Response.new(client.request(request))
26
66
  # Try to catch some common exceptions Net::HTTP might raise.
27
67
  rescue Errno::ETIMEDOUT, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::EHOSTUNREACH,
28
68
  IOError, SocketError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::OpenTimeout,
@@ -30,7 +70,7 @@ module Kentaa
30
70
  raise Kentaa::Api::Exception, e.message
31
71
  end
32
72
 
33
- response = Kentaa::Api::Response.new(result)
73
+ logger.debug("[Kentaa-API] Response: #{response.http_code}, body: #{response.body}") if config.debug?
34
74
 
35
75
  if response.error?
36
76
  raise Kentaa::Api::RequestError, response
@@ -38,6 +78,10 @@ module Kentaa
38
78
 
39
79
  response
40
80
  end
81
+
82
+ def logger
83
+ @logger ||= Logger.new($stdout)
84
+ end
41
85
  end
42
86
  end
43
87
  end
@@ -13,16 +13,20 @@ module Kentaa
13
13
 
14
14
  def parent
15
15
  if team_id
16
- Kentaa::Api::Resources::Team.new(config, id: team_id)
16
+ Kentaa::Api::Resources::Team.new(config, id: team_id, options: options)
17
17
  elsif project_id
18
- Kentaa::Api::Resources::Project.new(config, id: project_id)
18
+ Kentaa::Api::Resources::Project.new(config, id: project_id, options: options)
19
19
  elsif segment_id
20
- Kentaa::Api::Resources::Segment.new(config, id: segment_id)
20
+ Kentaa::Api::Resources::Segment.new(config, id: segment_id, options: options)
21
21
  else
22
- Kentaa::Api::Resources::Site.new(config, id: site_id)
22
+ Kentaa::Api::Resources::Site.new(config, id: site_id, options: options)
23
23
  end
24
24
  end
25
25
 
26
+ def site
27
+ Kentaa::Api::Resources::Site.new(config, id: site_id, options: options)
28
+ end
29
+
26
30
  def slug
27
31
  data[:slug]
28
32
  end
@@ -44,11 +48,11 @@ module Kentaa
44
48
  end
45
49
 
46
50
  def owner
47
- @owner ||= Kentaa::Api::Resources::User.new(config, data: data[:owner])
51
+ @owner ||= Kentaa::Api::Resources::User.new(config, data: data[:owner], options: options)
48
52
  end
49
53
 
50
54
  def team_captain?
51
- data[:team_captain]
55
+ data.fetch(:team_captain, false)
52
56
  end
53
57
 
54
58
  def first_name
@@ -120,7 +124,7 @@ module Kentaa
120
124
  end
121
125
 
122
126
  def activity
123
- @activity ||= Kentaa::Api::Resources::Activity.new(config, data: data[:activity])
127
+ @activity ||= Kentaa::Api::Resources::Activity.new(data[:activity])
124
128
  end
125
129
 
126
130
  def previous_participations
@@ -136,11 +140,11 @@ module Kentaa
136
140
  end
137
141
 
138
142
  def registration_fee
139
- @registration_fee ||= Kentaa::Api::Resources::RegistrationFee.new(config, data: data[:registration_fee])
143
+ @registration_fee ||= Kentaa::Api::Resources::RegistrationFee.new(data[:registration_fee])
140
144
  end
141
145
 
142
146
  def location
143
- @location ||= Kentaa::Api::Resources::Location.new(config, data: data[:location])
147
+ @location ||= Kentaa::Api::Resources::Location.new(data[:location])
144
148
  end
145
149
 
146
150
  def photos
@@ -149,7 +153,7 @@ module Kentaa
149
153
 
150
154
  if data[:photos]
151
155
  data[:photos].each do |photo|
152
- photos << Kentaa::Api::Resources::Photo.new(config, data: photo)
156
+ photos << Kentaa::Api::Resources::Photo.new(photo)
153
157
  end
154
158
  end
155
159
 
@@ -163,7 +167,7 @@ module Kentaa
163
167
 
164
168
  if data[:videos]
165
169
  data[:videos].each do |video|
166
- videos << Kentaa::Api::Resources::Video.new(config, data: video)
170
+ videos << Kentaa::Api::Resources::Video.new(video)
167
171
  end
168
172
  end
169
173
 
@@ -177,7 +181,7 @@ module Kentaa
177
181
 
178
182
  if data[:questions]
179
183
  data[:questions].each do |question|
180
- questions << Kentaa::Api::Resources::Question.new(config, data: question)
184
+ questions << Kentaa::Api::Resources::Question.new(question)
181
185
  end
182
186
  end
183
187
 
@@ -186,18 +190,38 @@ module Kentaa
186
190
  end
187
191
 
188
192
  def consent
189
- @consent ||= Kentaa::Api::Resources::Consent.new(config, data: data[:consent]) if data[:consent]
193
+ @consent ||= Kentaa::Api::Resources::Consent.new(data[:consent]) if data[:consent]
190
194
  end
191
195
 
192
196
  def external_reference
193
197
  data[:external_reference]
194
198
  end
195
199
 
196
- protected
200
+ def donations
201
+ @donations ||= Kentaa::Api::Resources::List.new(config, resource_class: Kentaa::Api::Resources::Donation, endpoint_path: "/actions/#{id}/donations")
202
+ end
203
+
204
+ def manual_donations
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")
210
+ end
211
+
212
+ private
197
213
 
198
- def load_resource(options)
214
+ def load_resource
199
215
  request.get("/actions/#{id}", options)
200
216
  end
217
+
218
+ def create_resource(attributes)
219
+ request.post("/actions", options, attributes)
220
+ end
221
+
222
+ def update_resource(attributes)
223
+ request.patch("/actions/#{id}", options, attributes)
224
+ end
201
225
  end
202
226
  end
203
227
  end
@@ -3,7 +3,17 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class Activity < Resource
6
+ class Activity
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 name
8
18
  data[:name]
9
19
  end
@@ -3,7 +3,13 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class Address < Resource
6
+ class Address
7
+ attr_reader :data
8
+
9
+ def initialize(data)
10
+ @data = data
11
+ end
12
+
7
13
  def address
8
14
  data[:address]
9
15
  end