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
@@ -4,16 +4,19 @@ module Kentaa
4
4
  module Api
5
5
  module Clients
6
6
  class Segments < Base
7
- include Kentaa::Api::Clients::All
7
+ def all(options = {})
8
+ segments = Kentaa::Api::Resources::Segments.new(config, options)
9
+ segments.all
10
+ end
8
11
 
9
12
  def list(options = {})
10
- response = request.get("/segments", options)
11
- Kentaa::Api::Resources::Segments.new(config, response)
13
+ segments = Kentaa::Api::Resources::Segments.new(config, options)
14
+ segments.load
12
15
  end
13
16
 
14
- def get(id)
15
- response = request.get("/segments/#{id}")
16
- Kentaa::Api::Resources::Segment.new(config, response)
17
+ def get(id, options = {})
18
+ segment = Kentaa::Api::Resources::Segment.new(config, options.merge(id: id))
19
+ segment.load
17
20
  end
18
21
  end
19
22
  end
@@ -4,9 +4,9 @@ module Kentaa
4
4
  module Api
5
5
  module Clients
6
6
  class Sites < Base
7
- def current
8
- response = request.get("/sites/current")
9
- Kentaa::Api::Resources::Site.new(config, response)
7
+ def current(options = {})
8
+ site = Kentaa::Api::Resources::Site.new(config, options)
9
+ site.load
10
10
  end
11
11
  end
12
12
  end
@@ -4,16 +4,19 @@ module Kentaa
4
4
  module Api
5
5
  module Clients
6
6
  class Teams < Base
7
- include Kentaa::Api::Clients::All
7
+ def all(options = {})
8
+ teams = Kentaa::Api::Resources::Teams.new(config, options)
9
+ teams.all
10
+ end
8
11
 
9
12
  def list(options = {})
10
- response = request.get("/teams", options)
11
- Kentaa::Api::Resources::Teams.new(config, response)
13
+ teams = Kentaa::Api::Resources::Teams.new(config, options)
14
+ teams.load
12
15
  end
13
16
 
14
- def get(id)
15
- response = request.get("/teams/#{id}")
16
- Kentaa::Api::Resources::Team.new(config, response)
17
+ def get(id, options = {})
18
+ team = Kentaa::Api::Resources::Team.new(config, options.merge(id: id))
19
+ team.load
17
20
  end
18
21
  end
19
22
  end
@@ -4,16 +4,29 @@ module Kentaa
4
4
  module Api
5
5
  module Clients
6
6
  class Users < Base
7
- include Kentaa::Api::Clients::All
7
+ def all(options = {})
8
+ users = Kentaa::Api::Resources::Users.new(config, options)
9
+ users.all
10
+ end
8
11
 
9
12
  def list(options = {})
10
- response = request.get("/users", options)
11
- Kentaa::Api::Resources::Users.new(config, response)
13
+ users = Kentaa::Api::Resources::Users.new(config, options)
14
+ users.load
15
+ end
16
+
17
+ def get(id, options = {})
18
+ user = Kentaa::Api::Resources::User.new(config, options.merge(id: id))
19
+ user.load
20
+ end
21
+
22
+ def create(attributes = {}, options = {})
23
+ user = Kentaa::Api::Resources::Users.new(config, options)
24
+ user.create(attributes)
12
25
  end
13
26
 
14
- def get(id)
15
- response = request.get("/users/#{id}")
16
- Kentaa::Api::Resources::User.new(config, response)
27
+ def update(id, attributes = {}, options = {})
28
+ user = Kentaa::Api::Resources::User.new(config, options.merge(id: id))
29
+ user.save(attributes)
17
30
  end
18
31
  end
19
32
  end
@@ -4,5 +4,23 @@ module Kentaa
4
4
  module Api
5
5
  class Exception < StandardError
6
6
  end
7
+
8
+ class RequestError < Kentaa::Api::Exception
9
+ attr_accessor :response
10
+
11
+ def initialize(response)
12
+ @response = response
13
+
14
+ super(response.message)
15
+ end
16
+
17
+ def errors
18
+ response.errors
19
+ end
20
+
21
+ def http_code
22
+ response.http_code
23
+ end
24
+ end
7
25
  end
8
26
  end
@@ -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
@@ -13,16 +13,53 @@ module Kentaa
13
13
  end
14
14
 
15
15
  def get(path, params = {})
16
+ request(:get, path, params)
17
+ end
18
+
19
+ def post(path, params = {}, body = {})
20
+ request(:post, path, params, body)
21
+ end
22
+
23
+ def patch(path, params = {}, body = {})
24
+ request(:patch, path, params, body)
25
+ end
26
+
27
+ def delete(path, params = {})
28
+ request(:delete, path, params)
29
+ end
30
+
31
+ private
32
+
33
+ def request(http_method, path, params = {}, body = {})
16
34
  uri = URI.parse(File.join(config.api_url, path))
17
35
  uri.query = URI.encode_www_form(params) unless params.empty?
18
36
 
19
- request = Net::HTTP::Get.new(uri)
37
+ case http_method
38
+ when :get
39
+ request = Net::HTTP::Get.new(uri)
40
+ when :post
41
+ request = Net::HTTP::Post.new(uri)
42
+ request.body = body.to_json
43
+ when :patch
44
+ request = Net::HTTP::Patch.new(uri)
45
+ request.body = body.to_json
46
+ when :delete
47
+ request = Net::HTTP::Delete.new(uri)
48
+ else
49
+ raise Kentaa::Api::Exception, "Invalid HTTP method: #{http_method}"
50
+ end
51
+
52
+ request["Accept"] = "application/json"
53
+ request["Content-Type"] = "application/json"
20
54
  request["X-Api-Key"] = config.api_key
55
+ request["User-Agent"] = "Ruby kentaa-api/#{Kentaa::Api::VERSION}"
56
+
57
+ client = Net::HTTP.new(uri.hostname, uri.port)
58
+ client.use_ssl = uri.scheme == "https"
59
+ client.verify_mode = OpenSSL::SSL::VERIFY_PEER
21
60
 
22
61
  begin
23
- response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
24
- http.request(request)
25
- end
62
+ response = Kentaa::Api::Response.new(client.request(request))
26
63
  # Try to catch some common exceptions Net::HTTP might raise.
27
64
  rescue Errno::ETIMEDOUT, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::EHOSTUNREACH,
28
65
  IOError, SocketError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::OpenTimeout,
@@ -30,7 +67,11 @@ module Kentaa
30
67
  raise Kentaa::Api::Exception, e.message
31
68
  end
32
69
 
33
- Kentaa::Api::Response.new(response)
70
+ if response.error?
71
+ raise Kentaa::Api::RequestError, response
72
+ end
73
+
74
+ response
34
75
  end
35
76
  end
36
77
  end
@@ -6,26 +6,20 @@ 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)
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)
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)
26
21
  else
27
- client = Kentaa::Api::Clients::Sites.new(config)
28
- client.current
22
+ Kentaa::Api::Resources::Site.new(config, id: site_id)
29
23
  end
30
24
  end
31
25
 
@@ -50,7 +44,7 @@ module Kentaa
50
44
  end
51
45
 
52
46
  def owner
53
- @owner ||= Kentaa::Api::Resources::User.new(config, data[:owner])
47
+ @owner ||= Kentaa::Api::Resources::User.new(config, data: data[:owner])
54
48
  end
55
49
 
56
50
  def team_captain?
@@ -74,7 +68,7 @@ module Kentaa
74
68
  end
75
69
 
76
70
  def name
77
- [first_name, infix, last_name].compact.join(" ")
71
+ [first_name, infix, last_name].reject { |s| s.to_s.empty? }.join(" ")
78
72
  end
79
73
 
80
74
  def fundraiser_page?
@@ -126,7 +120,7 @@ module Kentaa
126
120
  end
127
121
 
128
122
  def activity
129
- @activity ||= Kentaa::Api::Resources::Activity.new(config, data[:activity])
123
+ @activity ||= Kentaa::Api::Resources::Activity.new(data[:activity])
130
124
  end
131
125
 
132
126
  def previous_participations
@@ -142,11 +136,11 @@ module Kentaa
142
136
  end
143
137
 
144
138
  def registration_fee
145
- @registration_fee ||= Kentaa::Api::Resources::RegistrationFee.new(config, data[:registration_fee])
139
+ @registration_fee ||= Kentaa::Api::Resources::RegistrationFee.new(data[:registration_fee])
146
140
  end
147
141
 
148
142
  def location
149
- @location ||= Kentaa::Api::Resources::Location.new(config, data[:location])
143
+ @location ||= Kentaa::Api::Resources::Location.new(data[:location])
150
144
  end
151
145
 
152
146
  def photos
@@ -155,7 +149,7 @@ module Kentaa
155
149
 
156
150
  if data[:photos]
157
151
  data[:photos].each do |photo|
158
- photos << Kentaa::Api::Resources::Photo.new(config, photo)
152
+ photos << Kentaa::Api::Resources::Photo.new(photo)
159
153
  end
160
154
  end
161
155
 
@@ -169,7 +163,7 @@ module Kentaa
169
163
 
170
164
  if data[:videos]
171
165
  data[:videos].each do |video|
172
- videos << Kentaa::Api::Resources::Video.new(config, video)
166
+ videos << Kentaa::Api::Resources::Video.new(video)
173
167
  end
174
168
  end
175
169
 
@@ -183,7 +177,7 @@ module Kentaa
183
177
 
184
178
  if data[:questions]
185
179
  data[:questions].each do |question|
186
- questions << Kentaa::Api::Resources::Question.new(config, question)
180
+ questions << Kentaa::Api::Resources::Question.new(question)
187
181
  end
188
182
  end
189
183
 
@@ -192,12 +186,34 @@ module Kentaa
192
186
  end
193
187
 
194
188
  def consent
195
- @consent ||= Kentaa::Api::Resources::Consent.new(config, data[:consent]) if data[:consent]
189
+ @consent ||= Kentaa::Api::Resources::Consent.new(data[:consent]) if data[:consent]
196
190
  end
197
191
 
198
192
  def external_reference
199
193
  data[:external_reference]
200
194
  end
195
+
196
+ def donations
197
+ @donations ||= Kentaa::Api::Resources::Donations.new(config, action_id: id)
198
+ end
199
+
200
+ def manual_donations
201
+ @manual_donations ||= Kentaa::Api::Resources::ManualDonations.new(config, action_id: id)
202
+ end
203
+
204
+ private
205
+
206
+ def load_resource
207
+ request.get("/actions/#{id}", options)
208
+ end
209
+
210
+ def create_resource(attributes)
211
+ request.post("/actions", options, attributes)
212
+ end
213
+
214
+ def update_resource(attributes)
215
+ request.patch("/actions/#{id}", options, attributes)
216
+ end
201
217
  end
202
218
  end
203
219
  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)
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)
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]