forem-ruby 0.1.0.beta4 → 0.1.0.beta5
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.
- checksums.yaml +4 -4
- data/lib/forem/errors.rb +24 -0
- data/lib/forem/resources/badge_achievement.rb +8 -0
- data/lib/forem/services/badge_achievement_service.rb +16 -1
- data/lib/forem/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 83797be860d88f26465e8497d17232bc764baa5c297fde6ff208e50f37fd5a94
|
|
4
|
+
data.tar.gz: 2997d86bd809479c4085c2d2308202a2b1938415d74d0350ca5b84da87f9101f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '095016c6e947721f81f23fbb63e82afb6c5f7b45a2253d83b6151d2f97977d097d1759e5a0fd9f13800f5607914e2a633a01485a8300502e090ee7be7a9760b1'
|
|
7
|
+
data.tar.gz: a7c39b213002376f16d5d06563759ee3cd03ac6ec54889f4bdf1ed9545d57a89334ecd93c2a00afe12549d1b54cd8ba64e0c9d1a87c4baabcf8359ad12c95fd6
|
data/lib/forem/errors.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
1
3
|
module Forem
|
|
2
4
|
# Base error class for all errors raised by the forem-ruby library.
|
|
3
5
|
#
|
|
@@ -45,6 +47,28 @@ module Forem
|
|
|
45
47
|
@code = code
|
|
46
48
|
super(message)
|
|
47
49
|
end
|
|
50
|
+
|
|
51
|
+
# The response body parsed as JSON.
|
|
52
|
+
#
|
|
53
|
+
# Useful when the API returns structured detail alongside an error. An empty
|
|
54
|
+
# or unparseable body yields +nil+.
|
|
55
|
+
#
|
|
56
|
+
# @return [Hash, Array, nil] the parsed body, or +nil+ when there is
|
|
57
|
+
# nothing parseable to return.
|
|
58
|
+
#
|
|
59
|
+
# @example Reading a field the API returned with the error
|
|
60
|
+
# begin
|
|
61
|
+
# client.badge_achievements.create(user_id: 123, badge_id: 45)
|
|
62
|
+
# rescue Forem::ConflictError => e
|
|
63
|
+
# e.parsed_body&.dig("achievement_id")
|
|
64
|
+
# end
|
|
65
|
+
def parsed_body
|
|
66
|
+
return @parsed_body if defined?(@parsed_body)
|
|
67
|
+
|
|
68
|
+
@parsed_body = (JSON.parse(http_body) if http_body && !http_body.empty?)
|
|
69
|
+
rescue JSON::ParserError
|
|
70
|
+
@parsed_body = nil
|
|
71
|
+
end
|
|
48
72
|
end
|
|
49
73
|
|
|
50
74
|
# Raised when the server responds with HTTP 401 Unauthorized.
|
|
@@ -21,6 +21,7 @@ module Forem
|
|
|
21
21
|
# - +rewarding_context_message+ (String) — Rendered from
|
|
22
22
|
# +rewarding_context_message_markdown+ by the API; not writable
|
|
23
23
|
# - +include_default_description+ (Boolean) — Whether the badge description accompanies the message
|
|
24
|
+
# - +metadata+ (ForemObject) — Arbitrary key/value data supplied for context. Defaults to +{}+.
|
|
24
25
|
# - +created_at+ / +updated_at+ (String) — ISO 8601 timestamps; not writable
|
|
25
26
|
#
|
|
26
27
|
# == Authentication
|
|
@@ -38,6 +39,13 @@ module Forem
|
|
|
38
39
|
# )
|
|
39
40
|
# puts achievement.id
|
|
40
41
|
#
|
|
42
|
+
# @example Award a badge with caller-supplied metadata
|
|
43
|
+
# client.badge_achievements.create(
|
|
44
|
+
# user_id: 123,
|
|
45
|
+
# badge_id: 45,
|
|
46
|
+
# metadata: { entitlement_id: "01a0…", source: "core" }
|
|
47
|
+
# )
|
|
48
|
+
#
|
|
41
49
|
# @example Revoke a badge
|
|
42
50
|
# client.badge_achievements.delete(9876)
|
|
43
51
|
#
|
|
@@ -51,6 +51,16 @@ module Forem
|
|
|
51
51
|
|
|
52
52
|
# Award a badge to a user.
|
|
53
53
|
#
|
|
54
|
+
# If the badge cannot be awarded more than once and the user already holds
|
|
55
|
+
# it, this raises {ConflictError} (HTTP 409). The response carries
|
|
56
|
+
# +achievement_id+, the id of the award that blocked the request.
|
|
57
|
+
#
|
|
58
|
+
# begin
|
|
59
|
+
# client.badge_achievements.create(user_id: 123, badge_id: 45)
|
|
60
|
+
# rescue Forem::ConflictError => e
|
|
61
|
+
# existing_id = e.parsed_body&.dig("achievement_id")
|
|
62
|
+
# end
|
|
63
|
+
#
|
|
54
64
|
# @param params [Hash] achievement attributes
|
|
55
65
|
# @option params [Integer] :user_id the user ID (required)
|
|
56
66
|
# @option params [Integer] :badge_id the badge ID (required)
|
|
@@ -58,14 +68,19 @@ module Forem
|
|
|
58
68
|
# shown with the award
|
|
59
69
|
# @option params [Boolean] :include_default_description whether to show
|
|
60
70
|
# the badge's own description alongside the message (default true)
|
|
71
|
+
# @option params [Hash] :metadata arbitrary key/value data stored with the
|
|
72
|
+
# achievement for context
|
|
61
73
|
# @param opts [Hash] per-request options
|
|
62
74
|
# @return [BadgeAchievement] the newly created achievement
|
|
75
|
+
# @raise [ConflictError] on HTTP 409 when the user already holds a badge
|
|
76
|
+
# that cannot be awarded more than once
|
|
63
77
|
# @raise [InvalidRequestError] on HTTP 422
|
|
64
78
|
#
|
|
65
79
|
# @example
|
|
66
80
|
# client.badge_achievements.create(
|
|
67
81
|
# user_id: 123,
|
|
68
|
-
# badge_id: 45
|
|
82
|
+
# badge_id: 45,
|
|
83
|
+
# metadata: { entitlement_id: "01a0…" }
|
|
69
84
|
# )
|
|
70
85
|
#
|
|
71
86
|
# @see https://developers.forem.com/api/v1
|
data/lib/forem/version.rb
CHANGED