kentaa-api 0.2.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.rubocop.yml +22 -4
  4. data/.travis.yml +5 -5
  5. data/Gemfile +4 -2
  6. data/Gemfile.lock +33 -20
  7. data/README.md +320 -66
  8. data/kentaa-api.gemspec +3 -3
  9. data/lib/kentaa/api.rb +12 -4
  10. data/lib/kentaa/api/client.rb +12 -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/donation_forms.rb +24 -0
  14. data/lib/kentaa/api/clients/donations.rb +9 -6
  15. data/lib/kentaa/api/clients/manual_donations.rb +39 -0
  16. data/lib/kentaa/api/clients/newsletter_subscriptions.rb +9 -6
  17. data/lib/kentaa/api/clients/projects.rb +9 -6
  18. data/lib/kentaa/api/clients/recurring_donors.rb +24 -0
  19. data/lib/kentaa/api/clients/segments.rb +9 -6
  20. data/lib/kentaa/api/clients/sites.rb +3 -3
  21. data/lib/kentaa/api/clients/teams.rb +9 -6
  22. data/lib/kentaa/api/clients/users.rb +24 -6
  23. data/lib/kentaa/api/config.rb +4 -0
  24. data/lib/kentaa/api/exception.rb +21 -0
  25. data/lib/kentaa/api/finder.rb +9 -0
  26. data/lib/kentaa/api/request.rb +55 -5
  27. data/lib/kentaa/api/resources/action.rb +40 -20
  28. data/lib/kentaa/api/resources/actions.rb +11 -3
  29. data/lib/kentaa/api/resources/activity.rb +10 -2
  30. data/lib/kentaa/api/resources/address.rb +6 -2
  31. data/lib/kentaa/api/resources/banner.rb +20 -2
  32. data/lib/kentaa/api/resources/base.rb +35 -11
  33. data/lib/kentaa/api/resources/consent.rb +7 -1
  34. data/lib/kentaa/api/resources/contact.rb +83 -0
  35. data/lib/kentaa/api/resources/donation.rb +35 -18
  36. data/lib/kentaa/api/resources/donation_form.rb +100 -0
  37. data/lib/kentaa/api/resources/donation_forms.rb +35 -0
  38. data/lib/kentaa/api/resources/donations.rb +6 -3
  39. data/lib/kentaa/api/resources/error.rb +23 -0
  40. data/lib/kentaa/api/resources/{pagination.rb → list.rb} +26 -3
  41. data/lib/kentaa/api/resources/location.rb +7 -1
  42. data/lib/kentaa/api/resources/manual_donation.rb +122 -0
  43. data/lib/kentaa/api/resources/manual_donations.rb +40 -0
  44. data/lib/kentaa/api/resources/newsletter_subscription.rb +21 -10
  45. data/lib/kentaa/api/resources/newsletter_subscriptions.rb +6 -3
  46. data/lib/kentaa/api/resources/photo.rb +20 -2
  47. data/lib/kentaa/api/resources/project.rb +34 -12
  48. data/lib/kentaa/api/resources/projects.rb +6 -3
  49. data/lib/kentaa/api/resources/question.rb +18 -2
  50. data/lib/kentaa/api/resources/recurring_donor.rb +110 -0
  51. data/lib/kentaa/api/resources/recurring_donors.rb +35 -0
  52. data/lib/kentaa/api/resources/registration_fee.rb +1 -1
  53. data/lib/kentaa/api/resources/resource.rb +50 -3
  54. data/lib/kentaa/api/resources/reward.rb +10 -2
  55. data/lib/kentaa/api/resources/segment.rb +28 -4
  56. data/lib/kentaa/api/resources/segments.rb +6 -3
  57. data/lib/kentaa/api/resources/site.rb +20 -4
  58. data/lib/kentaa/api/resources/team.rb +27 -14
  59. data/lib/kentaa/api/resources/teams.rb +6 -3
  60. data/lib/kentaa/api/resources/user.rb +27 -5
  61. data/lib/kentaa/api/resources/users.rb +16 -3
  62. data/lib/kentaa/api/resources/video.rb +20 -2
  63. data/lib/kentaa/api/response.rb +21 -3
  64. data/lib/kentaa/api/version.rb +1 -1
  65. metadata +24 -16
  66. data/lib/kentaa/api/clients/all.rb +0 -26
  67. data/lib/kentaa/api/resources/status.rb +0 -27
@@ -16,6 +16,12 @@ module Kentaa
16
16
  when "Action"
17
17
  client = Kentaa::Api::Clients::Actions.new(config)
18
18
  client.get(id)
19
+ when "Donation"
20
+ client = Kentaa::Api::Clients::Donations.new(config)
21
+ client.get(id)
22
+ when "NewsletterSubscription"
23
+ client = Kentaa::Api::Clients::NewsletterSubscriptions.new(config)
24
+ client.get(id)
19
25
  when "Project"
20
26
  client = Kentaa::Api::Clients::Projects.new(config)
21
27
  client.get(id)
@@ -28,6 +34,9 @@ module Kentaa
28
34
  when "Team"
29
35
  client = Kentaa::Api::Clients::Teams.new(config)
30
36
  client.get(id)
37
+ when "User"
38
+ client = Kentaa::Api::Clients::Users.new(config)
39
+ client.get(id)
31
40
  end
32
41
  end
33
42
  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
+ logger.debug("[Kentaa-API] Request: #{http_method.upcase} #{uri}") if config.debug?
39
+
40
+ case http_method
41
+ when :get
42
+ request = Net::HTTP::Get.new(uri)
43
+ when :post
44
+ request = Net::HTTP::Post.new(uri)
45
+ request.body = body.to_json
46
+ when :patch
47
+ request = Net::HTTP::Patch.new(uri)
48
+ request.body = body.to_json
49
+ when :delete
50
+ request = Net::HTTP::Delete.new(uri)
51
+ else
52
+ raise Kentaa::Api::Exception, "Invalid HTTP method: #{http_method}"
53
+ end
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
- response = 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,17 @@ module Kentaa
30
70
  raise Kentaa::Api::Exception, e.message
31
71
  end
32
72
 
33
- Kentaa::Api::Response.new(response)
73
+ logger.debug("[Kentaa-API] Response: #{response.http_code}, body: #{response.body}") if config.debug?
74
+
75
+ if response.error?
76
+ raise Kentaa::Api::RequestError, response
77
+ end
78
+
79
+ response
80
+ end
81
+
82
+ def logger
83
+ @logger ||= Logger.new($stdout)
34
84
  end
35
85
  end
36
86
  end
@@ -6,29 +6,27 @@ require 'time'
6
6
  module Kentaa
7
7
  module Api
8
8
  module Resources
9
- class Action < Base
10
- include Kentaa::Api::Resources::Resource
11
-
9
+ class Action < Resource
12
10
  def object_key
13
11
  "Action_#{id}"
14
12
  end
15
13
 
16
14
  def parent
17
15
  if team_id
18
- client = Kentaa::Api::Clients::Teams.new(config)
19
- client.get(team_id)
16
+ Kentaa::Api::Resources::Team.new(config, id: team_id, options: options)
20
17
  elsif project_id
21
- client = Kentaa::Api::Clients::Projects.new(config)
22
- client.get(project_id)
18
+ Kentaa::Api::Resources::Project.new(config, id: project_id, options: options)
23
19
  elsif segment_id
24
- client = Kentaa::Api::Clients::Segments.new(config)
25
- client.get(segment_id)
20
+ Kentaa::Api::Resources::Segment.new(config, id: segment_id, options: options)
26
21
  else
27
- client = Kentaa::Api::Clients::Sites.new(config)
28
- client.current
22
+ Kentaa::Api::Resources::Site.new(config, id: site_id, options: options)
29
23
  end
30
24
  end
31
25
 
26
+ def site
27
+ Kentaa::Api::Resources::Site.new(config, id: site_id, options: options)
28
+ end
29
+
32
30
  def slug
33
31
  data[:slug]
34
32
  end
@@ -50,11 +48,11 @@ module Kentaa
50
48
  end
51
49
 
52
50
  def owner
53
- @owner ||= Kentaa::Api::Resources::User.new(config, data[:owner])
51
+ @owner ||= Kentaa::Api::Resources::User.new(config, data: data[:owner], options: options)
54
52
  end
55
53
 
56
54
  def team_captain?
57
- data[:team_captain]
55
+ data.fetch(:team_captain, false)
58
56
  end
59
57
 
60
58
  def first_name
@@ -126,7 +124,7 @@ module Kentaa
126
124
  end
127
125
 
128
126
  def activity
129
- @activity ||= Kentaa::Api::Resources::Activity.new(config, data[:activity])
127
+ @activity ||= Kentaa::Api::Resources::Activity.new(data[:activity])
130
128
  end
131
129
 
132
130
  def previous_participations
@@ -142,11 +140,11 @@ module Kentaa
142
140
  end
143
141
 
144
142
  def registration_fee
145
- @registration_fee ||= Kentaa::Api::Resources::RegistrationFee.new(config, data[:registration_fee])
143
+ @registration_fee ||= Kentaa::Api::Resources::RegistrationFee.new(data[:registration_fee])
146
144
  end
147
145
 
148
146
  def location
149
- @location ||= Kentaa::Api::Resources::Location.new(config, data[:location])
147
+ @location ||= Kentaa::Api::Resources::Location.new(data[:location])
150
148
  end
151
149
 
152
150
  def photos
@@ -155,7 +153,7 @@ module Kentaa
155
153
 
156
154
  if data[:photos]
157
155
  data[:photos].each do |photo|
158
- photos << Kentaa::Api::Resources::Photo.new(config, photo)
156
+ photos << Kentaa::Api::Resources::Photo.new(photo)
159
157
  end
160
158
  end
161
159
 
@@ -169,7 +167,7 @@ module Kentaa
169
167
 
170
168
  if data[:videos]
171
169
  data[:videos].each do |video|
172
- videos << Kentaa::Api::Resources::Video.new(config, video)
170
+ videos << Kentaa::Api::Resources::Video.new(video)
173
171
  end
174
172
  end
175
173
 
@@ -183,7 +181,7 @@ module Kentaa
183
181
 
184
182
  if data[:questions]
185
183
  data[:questions].each do |question|
186
- questions << Kentaa::Api::Resources::Question.new(config, question)
184
+ questions << Kentaa::Api::Resources::Question.new(question)
187
185
  end
188
186
  end
189
187
 
@@ -192,12 +190,34 @@ module Kentaa
192
190
  end
193
191
 
194
192
  def consent
195
- @consent ||= Kentaa::Api::Resources::Consent.new(config, data[:consent]) if data[:consent]
193
+ @consent ||= Kentaa::Api::Resources::Consent.new(data[:consent]) if data[:consent]
196
194
  end
197
195
 
198
196
  def external_reference
199
197
  data[:external_reference]
200
198
  end
199
+
200
+ def donations
201
+ @donations ||= Kentaa::Api::Resources::Donations.new(config, action_id: id)
202
+ end
203
+
204
+ def manual_donations
205
+ @manual_donations ||= Kentaa::Api::Resources::ManualDonations.new(config, action_id: id)
206
+ end
207
+
208
+ private
209
+
210
+ def load_resource
211
+ request.get("/actions/#{id}", options)
212
+ end
213
+
214
+ def create_resource(attributes)
215
+ request.post("/actions", options, attributes)
216
+ end
217
+
218
+ def update_resource(attributes)
219
+ request.patch("/actions/#{id}", options, attributes)
220
+ end
201
221
  end
202
222
  end
203
223
  end
@@ -3,23 +3,31 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class Actions < Base
6
+ class Actions < List
7
7
  include Enumerable
8
- include Kentaa::Api::Resources::Pagination
9
8
 
10
9
  def each(&block)
11
10
  actions.each(&block)
12
11
  end
13
12
 
13
+ def create(attributes = {})
14
+ action = Kentaa::Api::Resources::Action.new(config, options: options)
15
+ action.save(attributes)
16
+ end
17
+
14
18
  private
15
19
 
20
+ def load_resource
21
+ request.get("/actions", options)
22
+ end
23
+
16
24
  def actions
17
25
  @actions ||= begin
18
26
  actions = []
19
27
 
20
28
  if data
21
29
  data.each do |action|
22
- actions << Kentaa::Api::Resources::Action.new(config, action)
30
+ actions << Kentaa::Api::Resources::Action.new(config, data: action, options: options)
23
31
  end
24
32
  end
25
33
 
@@ -3,8 +3,16 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class Activity < Base
7
- include Kentaa::Api::Resources::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
8
16
 
9
17
  def name
10
18
  data[:name]
@@ -3,8 +3,12 @@
3
3
  module Kentaa
4
4
  module Api
5
5
  module Resources
6
- class Address < Base
7
- include Kentaa::Api::Resources::Resource
6
+ class Address
7
+ attr_reader :data
8
+
9
+ def initialize(data)
10
+ @data = data
11
+ end
8
12
 
9
13
  def address
10
14
  data[:address]
@@ -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 Banner < Base
7
- include Kentaa::Api::Resources::Resource
8
+ class Banner
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]
@@ -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
+ Date.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