Skip to content

Commit

Permalink
Add #as_app method to API
Browse files Browse the repository at this point in the history
  • Loading branch information
iwdt committed Feb 24, 2024
1 parent 1900dec commit fdd5b65
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ For configuring a connection through proxy, you can pass `proxy_host`, `proxy_po
client = Shikimori::API::Client.new(proxy_host: 'http://my-proxy', proxy_port: 8080, proxy_user: 'my_proxy_user', proxy_password: 'my_proxy_password')
```

You can also using client as application:

```ruby
Shikimori::API::Client.as_app(access_token: 'access-token') do |client|
animes = client.animes
mangas = client.mangas
# some logic ...
end
```

## Supported Ruby Versions
This library aims to support and is tested against the following Ruby implementations:
* Ruby 3.0
Expand Down
19 changes: 19 additions & 0 deletions lib/shikimori/api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ def initialize(site = DEFAULT_SITE_URL, **options)
@v1 = V1.new(base_url: base_uri.join('api/'), rest: rest)
@v2 = V2.new(base_url: base_uri.join('api/v2/'), rest: rest)
end

# Using shikimori client as application
#
# @yield [Client] client instance
# @example Usage example
# Shikimori::API::Client.as_app(access_token: 'my-access-token') do |client|
# animes = client.v1.animes
# mangas = client.v1.mangas
# # some logic ...
# end
#
# @see #initialize
def self.as_app(site = DEFAULT_SITE_URL, **options)
client = new(site, **options)

yield client

client
end
end
end
end
2 changes: 2 additions & 0 deletions sig/shikimori/api/client.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Shikimori
attr_reader v2: V2

def initialize: (?String site, **untyped) -> void

def self.as_app: (?String site, **untyped) { (Shikimori::API::Client) -> void } -> Shikimori::API::Client
end
end
end
11 changes: 11 additions & 0 deletions spec/shikimori/api/client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

RSpec.describe Shikimori::API::Client do
describe '#as_app' do
it 'yields the client object' do
described_class.as_app do |client|
expect(client).to be_an_instance_of(described_class)
end
end
end
end

0 comments on commit fdd5b65

Please sign in to comment.