mangadex 5.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +81 -0
- data/LICENSE.txt +21 -0
- data/README.md +42 -0
- data/Rakefile +6 -0
- data/bin/console +23 -0
- data/bin/setup +8 -0
- data/lib/extensions.rb +12 -0
- data/lib/mangadex/README.md +93 -0
- data/lib/mangadex/api/context.rb +53 -0
- data/lib/mangadex/api/response.rb +104 -0
- data/lib/mangadex/api/user.rb +48 -0
- data/lib/mangadex/api/version.rb +21 -0
- data/lib/mangadex/api.rb +5 -0
- data/lib/mangadex/artist.rb +13 -0
- data/lib/mangadex/auth.rb +56 -0
- data/lib/mangadex/author.rb +101 -0
- data/lib/mangadex/chapter.rb +105 -0
- data/lib/mangadex/content_rating.rb +75 -0
- data/lib/mangadex/cover_art.rb +93 -0
- data/lib/mangadex/custom_list.rb +127 -0
- data/lib/mangadex/internal/definition.rb +162 -0
- data/lib/mangadex/internal/request.rb +121 -0
- data/lib/mangadex/internal/with_attributes.rb +120 -0
- data/lib/mangadex/internal.rb +3 -0
- data/lib/mangadex/manga.rb +188 -0
- data/lib/mangadex/mangadex_object.rb +62 -0
- data/lib/mangadex/relationship.rb +46 -0
- data/lib/mangadex/report_reason.rb +39 -0
- data/lib/mangadex/scanlation_group.rb +97 -0
- data/lib/mangadex/sorbet.rb +42 -0
- data/lib/mangadex/tag.rb +10 -0
- data/lib/mangadex/types.rb +24 -0
- data/lib/mangadex/upload.rb +78 -0
- data/lib/mangadex/user.rb +103 -0
- data/lib/mangadex/version.rb +4 -0
- data/lib/mangadex.rb +35 -0
- data/mangadex.gemspec +35 -0
- data/sorbet/config +3 -0
- data/sorbet/rbi/gems/activesupport.rbi +1267 -0
- data/sorbet/rbi/gems/coderay.rbi +285 -0
- data/sorbet/rbi/gems/concurrent-ruby.rbi +1662 -0
- data/sorbet/rbi/gems/domain_name.rbi +52 -0
- data/sorbet/rbi/gems/http-accept.rbi +101 -0
- data/sorbet/rbi/gems/http-cookie.rbi +119 -0
- data/sorbet/rbi/gems/i18n.rbi +133 -0
- data/sorbet/rbi/gems/method_source.rbi +64 -0
- data/sorbet/rbi/gems/mime-types-data.rbi +17 -0
- data/sorbet/rbi/gems/mime-types.rbi +218 -0
- data/sorbet/rbi/gems/netrc.rbi +51 -0
- data/sorbet/rbi/gems/pry.rbi +1898 -0
- data/sorbet/rbi/gems/psych.rbi +471 -0
- data/sorbet/rbi/gems/rake.rbi +660 -0
- data/sorbet/rbi/gems/rest-client.rbi +454 -0
- data/sorbet/rbi/gems/rspec-core.rbi +1939 -0
- data/sorbet/rbi/gems/rspec-expectations.rbi +1150 -0
- data/sorbet/rbi/gems/rspec-mocks.rbi +1100 -0
- data/sorbet/rbi/gems/rspec-support.rbi +280 -0
- data/sorbet/rbi/gems/rspec.rbi +15 -0
- data/sorbet/rbi/gems/tzinfo.rbi +586 -0
- data/sorbet/rbi/gems/unf.rbi +19 -0
- data/sorbet/rbi/hidden-definitions/errors.txt +3942 -0
- data/sorbet/rbi/hidden-definitions/hidden.rbi +8210 -0
- data/sorbet/rbi/sorbet-typed/lib/activesupport/>=6/activesupport.rbi +37 -0
- data/sorbet/rbi/sorbet-typed/lib/activesupport/all/activesupport.rbi +1850 -0
- data/sorbet/rbi/sorbet-typed/lib/minitest/all/minitest.rbi +108 -0
- data/sorbet/rbi/sorbet-typed/lib/rake/all/rake.rbi +645 -0
- data/sorbet/rbi/sorbet-typed/lib/rspec-core/all/rspec-core.rbi +1891 -0
- data/sorbet/rbi/todo.rbi +7 -0
- metadata +243 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1c4ab078c2f38cc5519044c3b5984fe4061b431632b543766bc5b20b219d4a50
|
4
|
+
data.tar.gz: fe668a1df2fc8704676733ac0017443a5714e3716f0dec97debde112ab7ee3b1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c34c0b8c9e4f3d09c0f870165c02fe272ebc62af3f4145b0a6744ba9d31323d87ba25367018893b48bc6cc098a6b9315d6e97a1384956429eb3e6615b8055d89
|
7
|
+
data.tar.gz: 2218c590eea3bdff50ba1bd76a6f18737cf57ba327482f316fe2284365a5cee2767866f9510fbf2244bf8f99ca1c196698c5e03692101d2d619ffd45461260a4
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.0.1
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at me@akinyele.ca. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mangadex (5.3.0)
|
5
|
+
activesupport (~> 6.1)
|
6
|
+
psych (~> 4.0.1)
|
7
|
+
rest-client (~> 2.1)
|
8
|
+
sorbet-runtime
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
activesupport (6.1.4.1)
|
14
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
15
|
+
i18n (>= 1.6, < 2)
|
16
|
+
minitest (>= 5.1)
|
17
|
+
tzinfo (~> 2.0)
|
18
|
+
zeitwerk (~> 2.3)
|
19
|
+
coderay (1.1.3)
|
20
|
+
concurrent-ruby (1.1.9)
|
21
|
+
diff-lcs (1.4.4)
|
22
|
+
domain_name (0.5.20190701)
|
23
|
+
unf (>= 0.0.5, < 1.0.0)
|
24
|
+
http-accept (1.7.0)
|
25
|
+
http-cookie (1.0.4)
|
26
|
+
domain_name (~> 0.5)
|
27
|
+
i18n (1.8.10)
|
28
|
+
concurrent-ruby (~> 1.0)
|
29
|
+
method_source (1.0.0)
|
30
|
+
mime-types (3.3.1)
|
31
|
+
mime-types-data (~> 3.2015)
|
32
|
+
mime-types-data (3.2021.0901)
|
33
|
+
minitest (5.14.4)
|
34
|
+
netrc (0.11.0)
|
35
|
+
pry (0.14.1)
|
36
|
+
coderay (~> 1.1)
|
37
|
+
method_source (~> 1.0)
|
38
|
+
psych (4.0.1)
|
39
|
+
rake (10.5.0)
|
40
|
+
rest-client (2.1.0)
|
41
|
+
http-accept (>= 1.7.0, < 2.0)
|
42
|
+
http-cookie (>= 1.0.2, < 2.0)
|
43
|
+
mime-types (>= 1.16, < 4.0)
|
44
|
+
netrc (~> 0.8)
|
45
|
+
rspec (3.10.0)
|
46
|
+
rspec-core (~> 3.10.0)
|
47
|
+
rspec-expectations (~> 3.10.0)
|
48
|
+
rspec-mocks (~> 3.10.0)
|
49
|
+
rspec-core (3.10.1)
|
50
|
+
rspec-support (~> 3.10.0)
|
51
|
+
rspec-expectations (3.10.1)
|
52
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
53
|
+
rspec-support (~> 3.10.0)
|
54
|
+
rspec-mocks (3.10.2)
|
55
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
56
|
+
rspec-support (~> 3.10.0)
|
57
|
+
rspec-support (3.10.2)
|
58
|
+
sorbet (0.5.9152)
|
59
|
+
sorbet-static (= 0.5.9152)
|
60
|
+
sorbet-runtime (0.5.9152)
|
61
|
+
sorbet-static (0.5.9152-universal-darwin-20)
|
62
|
+
tzinfo (2.0.4)
|
63
|
+
concurrent-ruby (~> 1.0)
|
64
|
+
unf (0.1.4)
|
65
|
+
unf_ext
|
66
|
+
unf_ext (0.0.8)
|
67
|
+
zeitwerk (2.4.2)
|
68
|
+
|
69
|
+
PLATFORMS
|
70
|
+
x86_64-darwin-20
|
71
|
+
|
72
|
+
DEPENDENCIES
|
73
|
+
bundler (~> 2.2.19)
|
74
|
+
mangadex!
|
75
|
+
pry
|
76
|
+
rake (~> 10.0)
|
77
|
+
rspec (~> 3.0)
|
78
|
+
sorbet
|
79
|
+
|
80
|
+
BUNDLED WITH
|
81
|
+
2.2.19
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Akinyele Cafe-Febrissy
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Mangadex
|
2
|
+
|
3
|
+
Welcome to `mangadex`, your next favourite Ruby gem for interacting with [Mangadex](https://mangadex.org).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'mangadex'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install mangadex
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Please note that I tried my best to follow Mangadex's naming conventions for [their documentation](https://api.mangadex.org).
|
24
|
+
To find out how to use the gem, you're welcome to [check this out](lib/mangadex).
|
25
|
+
|
26
|
+
## Development
|
27
|
+
|
28
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
29
|
+
|
30
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/thedrummeraki/mangadex. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
35
|
+
|
36
|
+
## License
|
37
|
+
|
38
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
39
|
+
|
40
|
+
## Code of Conduct
|
41
|
+
|
42
|
+
Everyone interacting in the Mangadex project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/thedrummeraki/mangadex/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mangadex"
|
5
|
+
|
6
|
+
username, password = [
|
7
|
+
ENV['MD_USERNAME'],
|
8
|
+
ENV['MD_PASSWORD'],
|
9
|
+
]
|
10
|
+
|
11
|
+
if username && password
|
12
|
+
Mangadex::Auth.login(username, password)
|
13
|
+
end
|
14
|
+
|
15
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
16
|
+
# with your gem easier. You can also use a different console, if you like.
|
17
|
+
|
18
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
19
|
+
require "pry"
|
20
|
+
Pry.start
|
21
|
+
|
22
|
+
# require "irb"
|
23
|
+
# IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/extensions.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# typed: true
|
2
|
+
require "active_support/hash_with_indifferent_access"
|
3
|
+
|
4
|
+
class Hash
|
5
|
+
def with_indifferent_access
|
6
|
+
ActiveSupport::HashWithIndifferentAccess.new(self)
|
7
|
+
end
|
8
|
+
|
9
|
+
def nested_under_indifferent_access
|
10
|
+
ActiveSupport::HashWithIndifferentAccess.new(self)
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
## `Mangadex`
|
2
|
+
|
3
|
+
This is documentation for the `Mangadex` module.
|
4
|
+
|
5
|
+
### Directory
|
6
|
+
#### Sub-modules
|
7
|
+
|
8
|
+
- [`Mangadex::Api`](#)
|
9
|
+
- [`Mangadex::Internal`](#)
|
10
|
+
|
11
|
+
#### Fetchable/Resources
|
12
|
+
- [`Mangadex::Artist`](#)
|
13
|
+
- [`Mangadex::Auth`](#mangadexauth)
|
14
|
+
- [`Mangadex::Author`](#)
|
15
|
+
- [`Mangadex::Chapter`](#)
|
16
|
+
- [`Mangadex::ContentRating`](#)
|
17
|
+
- [`Mangadex::CoverArt`](#)
|
18
|
+
- [`Mangadex::CustomList`](#)
|
19
|
+
- [`Mangadex::Manga`](#)
|
20
|
+
- [`Mangadex::Relationship`](#)
|
21
|
+
- [`Mangadex::ReportReason`](#)
|
22
|
+
- [`Mangadex::ScanlationGroup`](#)
|
23
|
+
- [`Mangadex::Tag`](#)
|
24
|
+
- [`Mangadex::Upload`](#)
|
25
|
+
- [`Mangadex::User`](#)
|
26
|
+
|
27
|
+
#### Other classes
|
28
|
+
- [`Mangadex::MangadexObject`](#)
|
29
|
+
- [`Mangadex::Types`](#)
|
30
|
+
- [`Mangadex::Version`](#)
|
31
|
+
|
32
|
+
### Fetchable/Resources
|
33
|
+
|
34
|
+
These refer to the resources that live on Mangadex, or general "namespace"-like
|
35
|
+
concepts on which one can fetch information.
|
36
|
+
|
37
|
+
### `Mangadex::Artist`
|
38
|
+
|
39
|
+
> See [`Mangadex::Author`](#)
|
40
|
+
|
41
|
+
This is a sub-class of `Mangadex::Author`. It has the same attributes as an author.
|
42
|
+
|
43
|
+
### `Mangadex::Auth`
|
44
|
+
|
45
|
+
All things authentication.
|
46
|
+
|
47
|
+
#### `login`
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
Mangadex::Auth.login(username, password)
|
51
|
+
```
|
52
|
+
|
53
|
+
Login with your username and password. Upon successful login, the user will be available in a context from
|
54
|
+
`Mangadex::Api::Context.user`. This variable can be used anywhere in your application. More info [here](#).
|
55
|
+
|
56
|
+
> - Returns `Mangadex::Api::Response` if request fails.
|
57
|
+
> - Returns `true` if user is logged in.
|
58
|
+
> - Returns `false` if the user was not successfully logged in.
|
59
|
+
|
60
|
+
#### `check_token`
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
Mangadex::Auth.check_token
|
64
|
+
```
|
65
|
+
|
66
|
+
Check the status of your token, whether you're authenticated or not.
|
67
|
+
|
68
|
+
> - Returns a hash with the token information.
|
69
|
+
|
70
|
+
#### `logout`
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
Mangadex::Auth.logout
|
74
|
+
```
|
75
|
+
|
76
|
+
Logs out the current user. Sets `Mangadex::Api::Content.user` to `nil`.
|
77
|
+
|
78
|
+
> - Returns `Mangadex::Api::Response` if request fails.
|
79
|
+
> - Returns `true` if request is considered successful.
|
80
|
+
> - Idempotent request.
|
81
|
+
|
82
|
+
#### `refresh_token`
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
Mangadex::Auth.refresh_token
|
86
|
+
```
|
87
|
+
|
88
|
+
Manually cause a token refresh.
|
89
|
+
> Please note that simply calling `Mangadex::Api::Content.user` ensures that the token is valid. More info [here](#).
|
90
|
+
|
91
|
+
> - Returns `nil` if user is not logged (ie: `Mangadex::Api::Content.user` is `nil`)
|
92
|
+
> - Returns `true` if the refresh is successful
|
93
|
+
> - Returns `false` if the refresh is [not successful](#).
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# typed: true
|
2
|
+
module Mangadex
|
3
|
+
module Api
|
4
|
+
class Context
|
5
|
+
@@user = nil
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def user
|
9
|
+
@@user&.with_valid_session
|
10
|
+
end
|
11
|
+
|
12
|
+
def user=(user)
|
13
|
+
if user.is_a?(Mangadex::Api::User)
|
14
|
+
@@user = user
|
15
|
+
elsif user.is_a?(Mangadex::User)
|
16
|
+
@@user = Mangadex::Api::User.new(
|
17
|
+
user.id,
|
18
|
+
data: user,
|
19
|
+
)
|
20
|
+
elsif user.is_a?(Hash)
|
21
|
+
user = user.with_indifferent_access
|
22
|
+
|
23
|
+
@@user = Mangadex::Api::User.new(
|
24
|
+
user[:mangadex_user_id],
|
25
|
+
session: user[:session],
|
26
|
+
refresh: user[:refresh],
|
27
|
+
)
|
28
|
+
elsif user.nil?
|
29
|
+
@@user = nil
|
30
|
+
else
|
31
|
+
raise ArgumentError, "Must be an instance of #{Mangadex::Api::User}, #{Mangadex::User} or Hash"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def with_user(user)
|
36
|
+
current_user = @@user
|
37
|
+
@@user = user
|
38
|
+
response = yield
|
39
|
+
@@user = current_user
|
40
|
+
response
|
41
|
+
ensure
|
42
|
+
@@user = current_user
|
43
|
+
end
|
44
|
+
|
45
|
+
def without_user
|
46
|
+
with_user(nil) do
|
47
|
+
yield
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# typed: true
|
2
|
+
module Mangadex
|
3
|
+
module Api
|
4
|
+
class Response < Mangadex::MangadexObject
|
5
|
+
extend T::Sig
|
6
|
+
extend T::Generic
|
7
|
+
|
8
|
+
attr_accessor :result, :response, :errors, :data
|
9
|
+
attr_accessor :limit, :offset, :total
|
10
|
+
|
11
|
+
def self.attributes_to_inspect
|
12
|
+
%i(result errors limit offset total data)
|
13
|
+
end
|
14
|
+
|
15
|
+
class Error < Mangadex::MangadexObject
|
16
|
+
attr_accessor :id, :status, :title, :detail
|
17
|
+
|
18
|
+
def self.attributes_to_inspect
|
19
|
+
%i(id status title detail)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Collection < Array
|
24
|
+
def to_s
|
25
|
+
result_text = count == 0 || count > 1 ? "results" : "result"
|
26
|
+
"[...#{count} #{result_text}]"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
sig { params(data: Hash).returns(T.any(Mangadex::Api::Response, Hash)) }
|
31
|
+
def self.coerce(data)
|
32
|
+
if data['errors']
|
33
|
+
coerce_errors(data)
|
34
|
+
elsif data['response'] == 'entity'
|
35
|
+
coerce_entity(data)
|
36
|
+
elsif data['response'] == 'collection'
|
37
|
+
coerce_collection(data)
|
38
|
+
else
|
39
|
+
data
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def self.coerce_errors(data)
|
46
|
+
new(
|
47
|
+
result: data['result'],
|
48
|
+
response: data['response'],
|
49
|
+
errors: (
|
50
|
+
data['errors'].map do |error_data|
|
51
|
+
Error.new(
|
52
|
+
id: error_data['id'],
|
53
|
+
status: error_data['status'],
|
54
|
+
title: error_data['title'],
|
55
|
+
detail: error_data['detail'],
|
56
|
+
)
|
57
|
+
end
|
58
|
+
),
|
59
|
+
)
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.coerce_entity(data)
|
63
|
+
object_type = data['type'] || data.dig('data', 'type')
|
64
|
+
|
65
|
+
# Derive the class name from the type. "Convention over configuration"
|
66
|
+
class_from_data = "Mangadex::#{object_type.split('_').collect(&:capitalize).join}"
|
67
|
+
return unless Object.const_defined?(class_from_data)
|
68
|
+
|
69
|
+
klass = Object.const_get(class_from_data)
|
70
|
+
new(
|
71
|
+
result: data['result'],
|
72
|
+
response: data['response'],
|
73
|
+
data: klass.from_data(data['data'] || data),
|
74
|
+
)
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.coerce_collection(data)
|
78
|
+
new(
|
79
|
+
result: data['result'],
|
80
|
+
response: data['response'],
|
81
|
+
limit: data['limit'],
|
82
|
+
offset: data['offset'],
|
83
|
+
total: data['total'],
|
84
|
+
data: (
|
85
|
+
Collection.new(
|
86
|
+
data['data'].map do |entity_data|
|
87
|
+
object_type = entity_data['type']
|
88
|
+
class_from_data = "Mangadex::#{object_type.split('_').collect(&:capitalize).join}"
|
89
|
+
return unless Object.const_defined?(class_from_data)
|
90
|
+
|
91
|
+
klass = Object.const_get(class_from_data)
|
92
|
+
klass.from_data(entity_data)
|
93
|
+
end
|
94
|
+
)
|
95
|
+
),
|
96
|
+
)
|
97
|
+
end
|
98
|
+
|
99
|
+
def errored?
|
100
|
+
Array(errors).any?
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# typed: true
|
2
|
+
module Mangadex
|
3
|
+
module Api
|
4
|
+
class User
|
5
|
+
attr_accessor :mangadex_user_id, :session, :refresh, :session_valid_until
|
6
|
+
attr_reader :data
|
7
|
+
|
8
|
+
def initialize(mangadex_user_id, session: nil, refresh: nil, data: nil)
|
9
|
+
raise ArgumentError, 'Missing mangadex_user_id' if mangadex_user_id.to_s.empty?
|
10
|
+
|
11
|
+
@mangadex_user_id = mangadex_user_id
|
12
|
+
@session = session
|
13
|
+
@session_valid_until = session ? Time.now + (14 * 60) : nil
|
14
|
+
@refresh = refresh
|
15
|
+
@data = data
|
16
|
+
end
|
17
|
+
|
18
|
+
# nil: Nothing happened, no need to refresh the token
|
19
|
+
# true: The tokens were successfully refreshed
|
20
|
+
# false: Error: refresh token empty or could not refresh the token on the server
|
21
|
+
def refresh!
|
22
|
+
return false if refresh.nil?
|
23
|
+
|
24
|
+
response = Mangadex::Api::Context.without_user do
|
25
|
+
Mangadex::Internal::Request.post('/auth/refresh', payload: { token: refresh })
|
26
|
+
end
|
27
|
+
return false unless response['token']
|
28
|
+
|
29
|
+
@session_valid_until = Time.now + (14 * 60)
|
30
|
+
@refresh = response.dig('token', 'refresh')
|
31
|
+
@session = response.dig('token', 'session')
|
32
|
+
|
33
|
+
true
|
34
|
+
end
|
35
|
+
|
36
|
+
def with_valid_session
|
37
|
+
session_expired? && refresh!
|
38
|
+
self
|
39
|
+
ensure
|
40
|
+
self
|
41
|
+
end
|
42
|
+
|
43
|
+
def session_expired?
|
44
|
+
@session_valid_until.nil? || @session_valid_until <= Time.now
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# typed: true
|
2
|
+
require "psych"
|
3
|
+
|
4
|
+
module Mangadex
|
5
|
+
module Api
|
6
|
+
VERSION = -> do
|
7
|
+
version = Psych.load(
|
8
|
+
RestClient.get(
|
9
|
+
'https://api.mangadex.org/api.yaml',
|
10
|
+
).body,
|
11
|
+
).dig('info', 'version')
|
12
|
+
|
13
|
+
if version != Mangadex::VERSION
|
14
|
+
warn(
|
15
|
+
"[Warning] This gem is compatible with #{Mangadex::VERSION} but it looks like Mangadex is at #{version}",
|
16
|
+
"[Warning] Check out #{Mangadex::Internal::Request::BASE_URI} for more information.",
|
17
|
+
)
|
18
|
+
end
|
19
|
+
end.call
|
20
|
+
end
|
21
|
+
end
|
data/lib/mangadex/api.rb
ADDED