anilibria-api-ruby 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/anilibria/api/client.rb +27 -11
- data/lib/anilibria/api/exceptions/api_error.rb +2 -1
- data/lib/anilibria/api/exceptions/auth_error.rb +16 -5
- data/lib/anilibria/api/exceptions/response_error.rb +3 -2
- data/lib/anilibria/api/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69881766a2287e012e03871b9997435a9d8f7080eed1377c267847b865c9a0be
|
4
|
+
data.tar.gz: c70122a1d4aafea3218cc3f9b9323df801289733f394e7b1f5e847dff72ea466
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c25155e4d2fe623944cfa58b6b5ff8836132c4963980db42ea52cc0431301c052dd4accb81722d36d9d8faf148bd26c7d064f10bbfc92602744d13b45fa84b17
|
7
|
+
data.tar.gz: 57c3db01725fe8fc00a193269777b9d491816503b996ad105fda3c6210d1d5d9b3c6014317c8138cb8772614b982ecd209f3fe81452de6b27aa3886f84df096f
|
data/Gemfile.lock
CHANGED
data/lib/anilibria/api/client.rb
CHANGED
@@ -63,7 +63,7 @@ module Anilibria
|
|
63
63
|
def auth(mail, passwd)
|
64
64
|
response = auth_response(mail, passwd)
|
65
65
|
|
66
|
-
return unless response
|
66
|
+
return unless auth_successful?(response)
|
67
67
|
|
68
68
|
response[:sessionId]
|
69
69
|
end
|
@@ -71,7 +71,12 @@ module Anilibria
|
|
71
71
|
def auth!(mail, passwd)
|
72
72
|
response = auth_response(mail, passwd)
|
73
73
|
|
74
|
-
|
74
|
+
unless auth_successful?(response)
|
75
|
+
raise(
|
76
|
+
Exceptions::AuthError.new(response),
|
77
|
+
'Failed authorization attempt'
|
78
|
+
)
|
79
|
+
end
|
75
80
|
|
76
81
|
response[:sessionId]
|
77
82
|
end
|
@@ -82,23 +87,34 @@ module Anilibria
|
|
82
87
|
|
83
88
|
private
|
84
89
|
|
90
|
+
def check_response!(response)
|
91
|
+
return if response.status == 200
|
92
|
+
|
93
|
+
if !response.body.respond_to?(:to_hash) ||
|
94
|
+
!response.body[:error].respond_to?(:to_hash)
|
95
|
+
raise Exceptions::ResponseError.new(
|
96
|
+
'Unexpected response from API', response
|
97
|
+
)
|
98
|
+
end
|
99
|
+
|
100
|
+
raise Exceptions::ApiError.new(response), response.body.dig(:error, :message)
|
101
|
+
end
|
102
|
+
|
85
103
|
def auth_response(mail, passwd)
|
86
104
|
response = connection.post(
|
87
105
|
AUTH_ENDPOINT,
|
88
106
|
{ mail: mail, passwd: passwd }
|
89
107
|
)
|
90
108
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
return if response.status == 200
|
96
|
-
|
97
|
-
if !response.body.respond_to?(:to_hash) || !response.body.key?(:error)
|
98
|
-
raise Exceptions::ResponseError.new('Unexpected response from API', response)
|
109
|
+
begin
|
110
|
+
JSON.parse(response.body, { symbolize_names: true })
|
111
|
+
rescue JSON::ParserError
|
112
|
+
{}
|
99
113
|
end
|
114
|
+
end
|
100
115
|
|
101
|
-
|
116
|
+
def auth_successful?(response)
|
117
|
+
response[:key] == 'success' && response.key?(:sessionId)
|
102
118
|
end
|
103
119
|
end
|
104
120
|
end
|
@@ -2,14 +2,25 @@ module Anilibria
|
|
2
2
|
module Api
|
3
3
|
module Exceptions
|
4
4
|
class AuthError < Base
|
5
|
-
attr_reader :
|
5
|
+
attr_reader :data
|
6
|
+
|
7
|
+
def initialize(data = {})
|
8
|
+
@data = data
|
6
9
|
|
7
|
-
def initialize(response_body)
|
8
|
-
@err = response_body[:err]
|
9
|
-
@key = response_body[:key]
|
10
|
-
@mes = response_body[:mes]
|
11
10
|
super("#{mes} (#{key})")
|
12
11
|
end
|
12
|
+
|
13
|
+
def err
|
14
|
+
data[:err]
|
15
|
+
end
|
16
|
+
|
17
|
+
def mes
|
18
|
+
data[:mes]
|
19
|
+
end
|
20
|
+
|
21
|
+
def key
|
22
|
+
data[:key]
|
23
|
+
end
|
13
24
|
end
|
14
25
|
end
|
15
26
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: anilibria-api-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roman Kozachenko
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-02-
|
11
|
+
date: 2022-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-struct
|