audience-kit 0.2.0 → 0.3.0
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/.yardopts +8 -0
- data/CHANGELOG.md +21 -0
- data/README.md +163 -22
- data/docs/reference.md +219 -0
- data/docs/sign-in.md +197 -0
- data/docs/websites.md +167 -0
- data/lib/audience-kit/color.rb +68 -0
- data/lib/audience-kit/configuration.rb +54 -0
- data/lib/audience-kit/coordinate.rb +76 -0
- data/lib/audience-kit/cover_pass.rb +57 -0
- data/lib/audience-kit/credential.rb +140 -0
- data/lib/audience-kit/credential_store.rb +86 -0
- data/lib/audience-kit/device_description.rb +29 -0
- data/lib/audience-kit/environment.rb +34 -0
- data/lib/audience-kit/error.rb +190 -0
- data/lib/audience-kit/model.rb +136 -0
- data/lib/audience-kit/models.rb +942 -0
- data/lib/audience-kit/operations.rb +501 -0
- data/lib/audience-kit/queries.rb +632 -0
- data/lib/audience-kit/report_target.rb +34 -0
- data/lib/audience-kit/schema_shape.rb +31 -0
- data/lib/audience-kit/session.rb +580 -0
- data/lib/audience-kit/version.rb +6 -1
- data/lib/audience-kit.rb +119 -23
- data/sig/audience/kit.rbs +651 -14
- metadata +23 -5
- data/lib/audience-kit/event.rb +0 -18
- data/lib/audience-kit/venue.rb +0 -18
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Audience
|
|
4
|
+
module Kit
|
|
5
|
+
# A session the API issued, as +POST /v1/token+ (every variant) returns it:
|
|
6
|
+
#
|
|
7
|
+
# { "token": "...", "userId": "...", "audienceId": "...", "role": "member",
|
|
8
|
+
# "endpoint": "https://api.audiencekit.com/v1/audience/<id>/graphql", "expiresAt": "2026-11-26T19:00:00Z" }
|
|
9
|
+
#
|
|
10
|
+
# The credential says everything the client needs about the session, so nothing decodes the JWT. Treat the token
|
|
11
|
+
# like a password: keep it in a {CredentialStore} backed by something private, and never log it ({#inspect}
|
|
12
|
+
# leaves it out).
|
|
13
|
+
class Credential
|
|
14
|
+
# @return [String] the session JWT, sent as +Authorization: Bearer <token>+
|
|
15
|
+
attr_reader :token
|
|
16
|
+
# @return [String, nil] the signed-in user's ID; nil for a service token's session or an API key
|
|
17
|
+
attr_reader :user_id
|
|
18
|
+
# @return [String, nil] the audience the session is bound to; nil for a platform-console session
|
|
19
|
+
attr_reader :audience_id
|
|
20
|
+
# @return [String] +member+, +admin+, +venue_device+, +service+ or +bearer+ (an API key sent as is)
|
|
21
|
+
attr_reader :role
|
|
22
|
+
# @return [String] the GraphQL endpoint the token was issued for, as a full URL
|
|
23
|
+
attr_reader :endpoint
|
|
24
|
+
# @return [String, nil] the venue device this session runs on, for venue-device sessions
|
|
25
|
+
attr_reader :venue_device_id
|
|
26
|
+
# @return [Time, nil] when the session expires; nil when it only ends by signing out
|
|
27
|
+
attr_reader :expires_at
|
|
28
|
+
# @return [String] the API version the credential was issued under
|
|
29
|
+
attr_reader :api_version
|
|
30
|
+
# @return [Array<String>] a service token's scopes, e.g. +public:read+
|
|
31
|
+
attr_reader :scopes
|
|
32
|
+
# @return [String, nil] a service token's audience name
|
|
33
|
+
attr_reader :audience
|
|
34
|
+
|
|
35
|
+
def initialize(token:, endpoint:, user_id: nil, audience_id: nil, role: "member", venue_device_id: nil,
|
|
36
|
+
expires_at: nil, api_version: API_VERSION, scopes: [], audience: nil)
|
|
37
|
+
@token = token
|
|
38
|
+
@endpoint = endpoint
|
|
39
|
+
@user_id = user_id
|
|
40
|
+
@audience_id = audience_id
|
|
41
|
+
@role = role
|
|
42
|
+
@venue_device_id = venue_device_id
|
|
43
|
+
@expires_at = expires_at
|
|
44
|
+
@api_version = api_version
|
|
45
|
+
@scopes = scopes
|
|
46
|
+
@audience = audience
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# A credential from the API's JSON (decoded to a Hash with string keys).
|
|
50
|
+
# @param data [Hash]
|
|
51
|
+
# @return [Credential]
|
|
52
|
+
# @raise [ArgumentError] when +token+ or +endpoint+ is missing, or +expiresAt+ isn't an ISO 8601 time
|
|
53
|
+
def self.from_h(data)
|
|
54
|
+
%w[token endpoint].each do |key|
|
|
55
|
+
raise ArgumentError, "The credential has no #{key}." unless data[key].is_a?(String)
|
|
56
|
+
end
|
|
57
|
+
new(token: data["token"], endpoint: data["endpoint"], user_id: string(data["userId"]),
|
|
58
|
+
audience_id: string(data["audienceId"]), role: string(data["role"]) || "member",
|
|
59
|
+
venue_device_id: string(data["venueDeviceId"]), expires_at: time(data["expiresAt"]),
|
|
60
|
+
api_version: string(data["apiVersion"]) || API_VERSION,
|
|
61
|
+
scopes: data["scopes"].is_a?(Array) ? data["scopes"].grep(String) : [],
|
|
62
|
+
audience: string(data["audience"]))
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# The credential as the API's JSON shape, for storing (e.g. in a Rails session).
|
|
66
|
+
# @return [Hash{String => Object}]
|
|
67
|
+
def to_h
|
|
68
|
+
{
|
|
69
|
+
"token" => token, "userId" => user_id, "audienceId" => audience_id, "role" => role, "endpoint" => endpoint,
|
|
70
|
+
"venueDeviceId" => venue_device_id, "expiresAt" => expires_at&.utc&.iso8601(3), "apiVersion" => api_version
|
|
71
|
+
}.merge({ "scopes" => scopes, "audience" => audience }.reject { |_, v| v.nil? || v == [] })
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Whether the session has expired at +at+ (now by default). Sessions with no expiry never do.
|
|
75
|
+
# @param at [Time]
|
|
76
|
+
# @return [Boolean]
|
|
77
|
+
def expired?(at = Time.now)
|
|
78
|
+
!expires_at.nil? && expires_at <= at
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Whether the session expires within +seconds+ of +at+ (now by default).
|
|
82
|
+
# @param seconds [Numeric]
|
|
83
|
+
# @param at [Time]
|
|
84
|
+
# @return [Boolean]
|
|
85
|
+
def expires_within?(seconds, at = Time.now)
|
|
86
|
+
!expires_at.nil? && expires_at <= at + seconds
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Whether this is a service token's session (no person behind it).
|
|
90
|
+
# @return [Boolean]
|
|
91
|
+
def service?
|
|
92
|
+
role == "service"
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Whether a service token's session may do what +scope+ covers, e.g. +public:read+.
|
|
96
|
+
# @param scope [String]
|
|
97
|
+
# @return [Boolean]
|
|
98
|
+
def scope?(scope)
|
|
99
|
+
scopes.include?(scope)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# The API instance the endpoint is on: its scheme, host and port, and nothing else.
|
|
103
|
+
# @return [String, nil]
|
|
104
|
+
def origin
|
|
105
|
+
uri = URI.parse(endpoint)
|
|
106
|
+
return nil if uri.scheme.nil? || uri.host.nil?
|
|
107
|
+
|
|
108
|
+
default = uri.port == uri.default_port
|
|
109
|
+
"#{uri.scheme}://#{uri.host}#{":#{uri.port}" unless default}"
|
|
110
|
+
rescue URI::InvalidURIError
|
|
111
|
+
nil
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# @return [Boolean]
|
|
115
|
+
def ==(other)
|
|
116
|
+
other.is_a?(Credential) && other.to_h == to_h
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Keeps the token out of logs and consoles.
|
|
120
|
+
# @return [String]
|
|
121
|
+
def inspect
|
|
122
|
+
"#<#{self.class.name} role=#{role} user_id=#{user_id.inspect} audience_id=#{audience_id.inspect} " \
|
|
123
|
+
"expires_at=#{expires_at&.iso8601.inspect} token=[REDACTED]>"
|
|
124
|
+
end
|
|
125
|
+
alias to_s inspect
|
|
126
|
+
|
|
127
|
+
def self.string(value) = value.is_a?(String) ? value : nil
|
|
128
|
+
|
|
129
|
+
def self.time(value)
|
|
130
|
+
return nil if value.nil?
|
|
131
|
+
raise ArgumentError, "expiresAt is not a string." unless value.is_a?(String)
|
|
132
|
+
|
|
133
|
+
Time.iso8601(value)
|
|
134
|
+
rescue ArgumentError
|
|
135
|
+
raise ArgumentError, "#{value} isn't an ISO 8601 time."
|
|
136
|
+
end
|
|
137
|
+
private_class_method :string, :time
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Audience
|
|
4
|
+
module Kit
|
|
5
|
+
# Where a {Session} keeps its credential. Any object with +load+, +save(credential)+ and +delete+ works; back it
|
|
6
|
+
# with something private to the signed-in person (a server-side session, an encrypted cookie), because the token
|
|
7
|
+
# is as good as a password. Raise on failure: the session reports it as {Error::STORAGE}.
|
|
8
|
+
module CredentialStore
|
|
9
|
+
# @return [Credential, nil]
|
|
10
|
+
def load = raise(NotImplementedError)
|
|
11
|
+
|
|
12
|
+
# @param credential [Credential]
|
|
13
|
+
# @return [void]
|
|
14
|
+
def save(credential) = raise(NotImplementedError)
|
|
15
|
+
|
|
16
|
+
# @return [void]
|
|
17
|
+
def delete = raise(NotImplementedError)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Keeps the credential in memory, for as long as the store object lives: the store for a script, a job or one
|
|
21
|
+
# request.
|
|
22
|
+
class MemoryCredentialStore
|
|
23
|
+
include CredentialStore
|
|
24
|
+
|
|
25
|
+
def initialize(credential = nil)
|
|
26
|
+
@credential = credential
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# @return [Credential, nil]
|
|
30
|
+
def load = @credential
|
|
31
|
+
|
|
32
|
+
# @param credential [Credential]
|
|
33
|
+
def save(credential)
|
|
34
|
+
@credential = credential
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Forgets the credential.
|
|
38
|
+
def delete
|
|
39
|
+
@credential = nil
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Keeps the credential in a Hash-like session under +key+: Rails' +session+, Rack's +env["rack.session"]+,
|
|
44
|
+
# Sinatra's +session+. Use a server-side or encrypted session store (Rails' default cookie store is encrypted),
|
|
45
|
+
# and call +reset_session+ when signing someone in, so a session ID planted before sign-in isn't reused.
|
|
46
|
+
#
|
|
47
|
+
# @example In a Rails controller
|
|
48
|
+
# def audience_kit
|
|
49
|
+
# @audience_kit ||= Audience::Kit::Session.new(
|
|
50
|
+
# configuration: Audience::Kit::Configuration.new(host: "hotmess.social"),
|
|
51
|
+
# store: Audience::Kit::SessionCredentialStore.new(session)
|
|
52
|
+
# )
|
|
53
|
+
# end
|
|
54
|
+
class SessionCredentialStore
|
|
55
|
+
include CredentialStore
|
|
56
|
+
|
|
57
|
+
# @return [String]
|
|
58
|
+
attr_reader :key
|
|
59
|
+
|
|
60
|
+
# @param session [#[], #[]=, #delete] the session
|
|
61
|
+
# @param key [String] where in it the credential goes
|
|
62
|
+
def initialize(session, key: "audiencekit.credential")
|
|
63
|
+
@session = session
|
|
64
|
+
@key = key
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# @return [Credential, nil] the stored credential, or nil when there is none or it is unreadable
|
|
68
|
+
def load
|
|
69
|
+
data = @session[key]
|
|
70
|
+
data.is_a?(Hash) ? Credential.from_h(data.transform_keys(&:to_s)) : nil
|
|
71
|
+
rescue ArgumentError
|
|
72
|
+
nil
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# @param credential [Credential]
|
|
76
|
+
def save(credential)
|
|
77
|
+
@session[key] = credential.to_h
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Removes the credential from the session.
|
|
81
|
+
def delete
|
|
82
|
+
@session.delete(key)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Audience
|
|
4
|
+
module Kit
|
|
5
|
+
# The device a session is created on. The API records it alongside the session.
|
|
6
|
+
#
|
|
7
|
+
# @!attribute [r] identifier
|
|
8
|
+
# @return [String] a stable identifier for this install, browser or server
|
|
9
|
+
# @!attribute [r] type
|
|
10
|
+
# @return [String] +web+ for a server or browser client
|
|
11
|
+
# @!attribute [r] model
|
|
12
|
+
# @return [String, nil] the hardware model, when there is one
|
|
13
|
+
# @!attribute [r] version
|
|
14
|
+
# @return [String, nil] the app's marketing version
|
|
15
|
+
# @!attribute [r] build
|
|
16
|
+
# @return [String, nil] the app's build number
|
|
17
|
+
DeviceDescription = Data.define(:identifier, :type, :model, :version, :build) do
|
|
18
|
+
def initialize(identifier:, type: "web", model: nil, version: nil, build: nil)
|
|
19
|
+
super
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# The device as the API's +device+ object.
|
|
23
|
+
# @return [Hash{String => String}]
|
|
24
|
+
def to_body
|
|
25
|
+
{ "identifier" => identifier, "type" => type, "model" => model, "version" => version, "build" => build }.compact
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Audience
|
|
4
|
+
module Kit
|
|
5
|
+
# Which build of the app is calling. Staging builds talk to the production API but sign people in with the
|
|
6
|
+
# audience's staging Facebook app; the client says so with the +X-AudienceKit-Environment: staging+ header.
|
|
7
|
+
# Production builds leave the header out. A value the SDK doesn't know is sent as is.
|
|
8
|
+
module Environment
|
|
9
|
+
# A store build: sends no header.
|
|
10
|
+
PRODUCTION = "production"
|
|
11
|
+
# A staging build, which signs people in with the audience's staging Facebook app.
|
|
12
|
+
STAGING = "staging"
|
|
13
|
+
# The header that carries the environment.
|
|
14
|
+
HEADER = "X-AudienceKit-Environment"
|
|
15
|
+
# The environments this SDK knows.
|
|
16
|
+
KNOWN = [PRODUCTION, STAGING].freeze
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Which of an audience's GraphQL endpoints a request goes to: its path segment under +/v1/audience/<id>/+. This
|
|
20
|
+
# SDK speaks to the consumer view, which grants a signed-in member their own records and nothing more.
|
|
21
|
+
module GraphQLEndpoint
|
|
22
|
+
# +/v1/audience/<id>/graphql+, for the apps people use.
|
|
23
|
+
CONSUMER = "graphql"
|
|
24
|
+
|
|
25
|
+
# The endpoint's path for an audience.
|
|
26
|
+
# @param audience_id [String]
|
|
27
|
+
# @param endpoint [String]
|
|
28
|
+
# @return [String]
|
|
29
|
+
def self.path(audience_id, endpoint = CONSUMER)
|
|
30
|
+
"/v1/audience/#{URI.encode_uri_component(audience_id)}/#{endpoint}"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Audience
|
|
4
|
+
module Kit
|
|
5
|
+
# One entry of a GraphQL response's +errors+ array.
|
|
6
|
+
class GraphQLError
|
|
7
|
+
# @return [String]
|
|
8
|
+
attr_reader :message
|
|
9
|
+
# @return [Array<String, Integer>, nil] the response path of the field that failed, e.g. +["venues", 0]+
|
|
10
|
+
attr_reader :path
|
|
11
|
+
# @return [Hash, nil] extra detail: +code+ and, for INVALID, +fields+ mapping each field to its messages
|
|
12
|
+
attr_reader :extensions
|
|
13
|
+
|
|
14
|
+
def initialize(message:, path: nil, extensions: nil)
|
|
15
|
+
@message = message
|
|
16
|
+
@path = path
|
|
17
|
+
@extensions = extensions
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# @param data [Hash] one entry of +errors+, as decoded JSON
|
|
21
|
+
# @return [GraphQLError]
|
|
22
|
+
def self.from_h(data)
|
|
23
|
+
data = {} unless data.is_a?(Hash)
|
|
24
|
+
new(message: data["message"].is_a?(String) ? data["message"] : "The request failed.",
|
|
25
|
+
path: data["path"].is_a?(Array) ? data["path"] : nil,
|
|
26
|
+
extensions: data["extensions"].is_a?(Hash) ? data["extensions"] : nil)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# +extensions.code+, when the API sent one.
|
|
30
|
+
# @return [String, nil]
|
|
31
|
+
def code
|
|
32
|
+
code = extensions&.dig("code")
|
|
33
|
+
code.is_a?(String) ? code : nil
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Everything a {Session} call can fail with: a {#code} from the API's vocabulary (or the SDK's own, for failures
|
|
38
|
+
# before a response), a message for the person, and what detail there is.
|
|
39
|
+
#
|
|
40
|
+
# Compare {#code} with the constants below. The API sends +extensions.code+ on GraphQL errors and +error+ on REST
|
|
41
|
+
# ones; a code the SDK doesn't know is passed through as the API sent it.
|
|
42
|
+
#
|
|
43
|
+
# @example
|
|
44
|
+
# begin
|
|
45
|
+
# session.rsvp_event(event_id, "ATTENDING")
|
|
46
|
+
# rescue Audience::Kit::Error => e
|
|
47
|
+
# redirect_to sign_in_path if e.unauthenticated?
|
|
48
|
+
# flash[:alert] = e.message
|
|
49
|
+
# end
|
|
50
|
+
class Error < StandardError
|
|
51
|
+
# The API rejected the session (HTTP 401). The session has already forgotten the credential; sign in again.
|
|
52
|
+
UNAUTHENTICATED = "UNAUTHENTICATED"
|
|
53
|
+
# Signed in, but not allowed here (HTTP 403).
|
|
54
|
+
FORBIDDEN = "FORBIDDEN"
|
|
55
|
+
# The audience, record or host doesn't exist (HTTP 404).
|
|
56
|
+
NOT_FOUND = "NOT_FOUND"
|
|
57
|
+
# The input was refused; {#fields} says which parts.
|
|
58
|
+
INVALID = "INVALID"
|
|
59
|
+
# Too many requests (HTTP 429).
|
|
60
|
+
RATE_LIMITED = "RATE_LIMITED"
|
|
61
|
+
# The change clashed with the record's state (HTTP 409).
|
|
62
|
+
CONFLICT = "CONFLICT"
|
|
63
|
+
# The API failed (HTTP 5xx, or a GraphQL error it couldn't classify).
|
|
64
|
+
INTERNAL = "INTERNAL"
|
|
65
|
+
# There is no stored credential; sign in first.
|
|
66
|
+
NOT_SIGNED_IN = "NOT_SIGNED_IN"
|
|
67
|
+
# The API refused the Facebook, Google, Apple or review-code login (HTTP 401).
|
|
68
|
+
SIGN_IN_REJECTED = "SIGN_IN_REJECTED"
|
|
69
|
+
# Sign-in failed (HTTP 400). The message is the API's when it sent one.
|
|
70
|
+
SIGN_IN_FAILED = "SIGN_IN_FAILED"
|
|
71
|
+
# Any other unsuccessful HTTP status; see {#status}.
|
|
72
|
+
HTTP = "HTTP"
|
|
73
|
+
# The request never got a response: offline, timed out, TLS failure and so on.
|
|
74
|
+
NETWORK = "NETWORK"
|
|
75
|
+
# The response wasn't the JSON the SDK expected.
|
|
76
|
+
DECODING = "DECODING"
|
|
77
|
+
# A GraphQL error with no +extensions.code+, such as a validation failure of the document itself.
|
|
78
|
+
GRAPHQL = "GRAPHQL"
|
|
79
|
+
# The configuration is missing something this call needs, e.g. a host.
|
|
80
|
+
MISSING_CONFIGURATION = "MISSING_CONFIGURATION"
|
|
81
|
+
# The credential store couldn't save the session.
|
|
82
|
+
STORAGE = "STORAGE"
|
|
83
|
+
# The API no longer serves this SDK version (HTTP 426); see {#minimum_sdk_version}.
|
|
84
|
+
UPDATE_REQUIRED = "UPDATE_REQUIRED"
|
|
85
|
+
# The API no longer serves the API version this SDK speaks (HTTP 400 +unsupported_api_version+).
|
|
86
|
+
UNSUPPORTED_API_VERSION = "UNSUPPORTED_API_VERSION"
|
|
87
|
+
# The REST body's +error+ for an API version the API no longer serves.
|
|
88
|
+
UNSUPPORTED_API_VERSION_ERROR = "unsupported_api_version"
|
|
89
|
+
|
|
90
|
+
# @return [String] one of the constants above, or whatever the API sent
|
|
91
|
+
attr_reader :code
|
|
92
|
+
# @return [Integer, nil] the HTTP status, when there was a response
|
|
93
|
+
attr_reader :status
|
|
94
|
+
# @return [Hash{String => Array<String>}] INVALID only: the API's messages per field
|
|
95
|
+
attr_reader :fields
|
|
96
|
+
# @return [Array<GraphQLError>] the GraphQL response's +errors+, when it returned any
|
|
97
|
+
attr_reader :errors
|
|
98
|
+
# @return [String, nil] UPDATE_REQUIRED only: the oldest SDK version the API serves
|
|
99
|
+
attr_reader :minimum_sdk_version
|
|
100
|
+
|
|
101
|
+
def initialize(message = "The request failed.", code: HTTP, status: nil, fields: {}, errors: [],
|
|
102
|
+
minimum_sdk_version: nil)
|
|
103
|
+
super(message)
|
|
104
|
+
@code = code
|
|
105
|
+
@status = status
|
|
106
|
+
@fields = fields
|
|
107
|
+
@errors = errors
|
|
108
|
+
@minimum_sdk_version = minimum_sdk_version
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
class << self
|
|
112
|
+
# @!visibility private
|
|
113
|
+
def not_signed_in = new("You're not signed in.", code: NOT_SIGNED_IN)
|
|
114
|
+
|
|
115
|
+
# @!visibility private
|
|
116
|
+
def unauthorized = new("Your session has ended. Sign in again to continue.", code: UNAUTHENTICATED, status: 401)
|
|
117
|
+
|
|
118
|
+
# @!visibility private
|
|
119
|
+
def sign_in_rejected
|
|
120
|
+
new("Sign-in was refused. The login isn't valid for this app.", code: SIGN_IN_REJECTED, status: 401)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# @!visibility private
|
|
124
|
+
def sign_in_failed(message) = new(message || "Sign-in failed.", code: SIGN_IN_FAILED, status: 400)
|
|
125
|
+
|
|
126
|
+
# @!visibility private
|
|
127
|
+
def forbidden(message = nil) = new(message || "You don't have access to this.", code: FORBIDDEN, status: 403)
|
|
128
|
+
|
|
129
|
+
# @!visibility private
|
|
130
|
+
def not_found(message = nil) = new(message || "That isn't available.", code: NOT_FOUND, status: 404)
|
|
131
|
+
|
|
132
|
+
# @!visibility private
|
|
133
|
+
def http(status, code = nil, message = nil)
|
|
134
|
+
new(message || "The server responded with status #{status}.", code: code || HTTP, status: status)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# @!visibility private
|
|
138
|
+
def network(error) = new(error.message, code: NETWORK)
|
|
139
|
+
|
|
140
|
+
# @!visibility private
|
|
141
|
+
def decoding(detail)
|
|
142
|
+
new("The server sent a response the app didn't understand. #{detail}", code: DECODING)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# @!visibility private
|
|
146
|
+
def missing_configuration(detail)
|
|
147
|
+
new("AudienceKit isn't configured: #{detail}", code: MISSING_CONFIGURATION)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# @!visibility private
|
|
151
|
+
def storage(detail) = new("The session couldn't be saved. #{detail}", code: STORAGE)
|
|
152
|
+
|
|
153
|
+
# @!visibility private
|
|
154
|
+
def update_required(minimum, message)
|
|
155
|
+
new(message || "This version of the app is out of date. Update it to keep going.",
|
|
156
|
+
code: UPDATE_REQUIRED, status: 426, minimum_sdk_version: minimum)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# The GraphQL operation returned +errors+: the code and message are the first one's, and INVALID errors'
|
|
160
|
+
# +extensions.fields+ become {#fields}.
|
|
161
|
+
# @param errors [Array<GraphQLError>]
|
|
162
|
+
# @return [Error]
|
|
163
|
+
def graphql(errors)
|
|
164
|
+
first = errors.first
|
|
165
|
+
fields = {}
|
|
166
|
+
sent = first&.extensions&.dig("fields")
|
|
167
|
+
if sent.is_a?(Hash)
|
|
168
|
+
sent.each do |field, value|
|
|
169
|
+
fields[field.to_s] = Array(value).grep(String)
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
new(first&.message || "The request failed.", code: first&.code || GRAPHQL, fields: fields, errors: errors)
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Whether the API refused the session ({UNAUTHENTICATED}).
|
|
177
|
+
# @return [Boolean]
|
|
178
|
+
def unauthenticated?
|
|
179
|
+
code == UNAUTHENTICATED
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# Whether the API refused the query because it asks for a field this audience's schema doesn't have: one a
|
|
183
|
+
# module adds that the audience hasn't installed (or one from a newer API).
|
|
184
|
+
# @return [Boolean]
|
|
185
|
+
def undefined_field?
|
|
186
|
+
errors.any? { |error| error.code == "undefinedField" }
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Audience
|
|
4
|
+
module Kit
|
|
5
|
+
# A result from the API: typed readers for what apps read most, over the API's JSON exactly as it came back.
|
|
6
|
+
#
|
|
7
|
+
# Every model is also readable as the Hash it was made from (+venue["locale"]["name"]+), with the keys the schema
|
|
8
|
+
# names (camelCase), so fields the model has no reader for are still there. {#to_h} returns that Hash and
|
|
9
|
+
# {#to_json} writes it back out. Models are read-only.
|
|
10
|
+
class Model
|
|
11
|
+
# @return [Hash{String => Object}] the API's JSON for this object, as it came back
|
|
12
|
+
attr_reader :raw
|
|
13
|
+
# @deprecated The 0.2 name for {#raw}.
|
|
14
|
+
alias data raw
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
# The model for +data+, or nil when it isn't an object (the field was null, or its module isn't installed).
|
|
18
|
+
# @param data [Object]
|
|
19
|
+
# @return [Model, nil]
|
|
20
|
+
def from(data)
|
|
21
|
+
data.is_a?(Hash) ? new(data) : nil
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# The models for a JSON list, skipping nulls: a connection's +nodes+ or a plain list.
|
|
25
|
+
# @param data [Object]
|
|
26
|
+
# @return [Array<Model>]
|
|
27
|
+
def list(data)
|
|
28
|
+
data = data["nodes"] if data.is_a?(Hash) && data.key?("nodes")
|
|
29
|
+
return [] unless data.is_a?(Array)
|
|
30
|
+
|
|
31
|
+
data.grep(Hash).map { |item| new(item) }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# @return [Array<Symbol>] the readers {.attribute} declared, in order
|
|
35
|
+
def attributes
|
|
36
|
+
@attributes ||= []
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
# Declares a reader for a field of the JSON. +type+ is +:string+, +:integer+, +:boolean+, +:time+,
|
|
42
|
+
# +:strings+, +:hashes+, +:hash+, a model class's name (a String, so models can refer to each other) or a
|
|
43
|
+
# one-element Array of one, for a list. +path+ is where the field is (+"startAt"+ by default for
|
|
44
|
+
# +:start_at+), +default+ what to read when it is missing.
|
|
45
|
+
def attribute(name, type = :string, path: nil, default: nil)
|
|
46
|
+
keys = Array(path || name.to_s.gsub(/_([a-z])/) { Regexp.last_match(1).upcase })
|
|
47
|
+
attributes << name
|
|
48
|
+
define_method(name) do
|
|
49
|
+
@values.fetch(name) do
|
|
50
|
+
@values[name] = Model.cast(dig(*keys), type, default)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# @param raw [Hash] the API's JSON for this object, decoded with string keys
|
|
57
|
+
def initialize(raw)
|
|
58
|
+
@raw = raw.freeze
|
|
59
|
+
@values = {}
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# A field of the JSON, by the schema's name.
|
|
63
|
+
# @param key [String, Symbol]
|
|
64
|
+
# @return [Object, nil]
|
|
65
|
+
def [](key)
|
|
66
|
+
raw[key.to_s]
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# The JSON at a path of keys, or nil where any of it is missing.
|
|
70
|
+
# @return [Object, nil]
|
|
71
|
+
def dig(*keys)
|
|
72
|
+
raw.dig(*keys.map(&:to_s))
|
|
73
|
+
rescue TypeError
|
|
74
|
+
nil
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# @return [Hash{String => Object}]
|
|
78
|
+
def to_h = raw
|
|
79
|
+
|
|
80
|
+
# @return [String]
|
|
81
|
+
def to_json(*) = raw.to_json(*)
|
|
82
|
+
|
|
83
|
+
# @return [Boolean]
|
|
84
|
+
def ==(other)
|
|
85
|
+
other.instance_of?(self.class) && other.raw == raw
|
|
86
|
+
end
|
|
87
|
+
alias eql? ==
|
|
88
|
+
|
|
89
|
+
# @return [Integer]
|
|
90
|
+
def hash = [self.class, raw].hash
|
|
91
|
+
|
|
92
|
+
# @return [String]
|
|
93
|
+
def inspect
|
|
94
|
+
shown = self.class.attributes.first(4).map { |name| "#{name}=#{public_send(name).inspect}" }
|
|
95
|
+
"#<#{self.class.name} #{shown.join(" ")}>"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# @!visibility private
|
|
99
|
+
def self.cast(value, type, default)
|
|
100
|
+
result = case type
|
|
101
|
+
when :string then value.is_a?(String) || value.is_a?(Numeric) ? value.to_s : nil
|
|
102
|
+
when :integer then integer(value)
|
|
103
|
+
when :boolean then value if [true, false].include?(value)
|
|
104
|
+
when :time then time(value)
|
|
105
|
+
when :strings then value.is_a?(Array) ? value.grep(String) : []
|
|
106
|
+
when :hashes then hashes(value)
|
|
107
|
+
when :hash then value.is_a?(Hash) ? value : nil
|
|
108
|
+
when Array then Kit.const_get(type.first).list(value)
|
|
109
|
+
else Kit.const_get(type).from(value)
|
|
110
|
+
end
|
|
111
|
+
result.nil? ? default : result
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# @!visibility private
|
|
115
|
+
def self.integer(value)
|
|
116
|
+
case value
|
|
117
|
+
when Integer then value
|
|
118
|
+
when Float, /\A-?\d+(\.\d+)?\z/ then value.to_i
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# @!visibility private
|
|
123
|
+
def self.time(value)
|
|
124
|
+
value.is_a?(String) && !value.empty? ? Time.iso8601(value) : nil
|
|
125
|
+
rescue ArgumentError
|
|
126
|
+
nil
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# @!visibility private
|
|
130
|
+
def self.hashes(value)
|
|
131
|
+
value = value["nodes"] if value.is_a?(Hash)
|
|
132
|
+
value.is_a?(Array) ? value.grep(Hash) : []
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|