hcbv4 0.1.1 → 0.2.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/README.md +16 -53
- data/lib/hcbv4/client.rb +7 -5
- data/lib/hcbv4/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: 769371ccfa3c68e1fb4e70af29889f49e19319c6c4d7152dea54ce56f462d7d4
|
|
4
|
+
data.tar.gz: 00d08d8cb7479e1004bc5a3eaacc87faad6ce1708d38f05d06c14066983fe830
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8b2df71c8f293d0988a18fdb97ac180b756e3d06e196e0314275e458255fb79fd9bbb88124544c43b3792e9c2a1345e49a9a9ea42649f1a17a250952a95cfa13
|
|
7
|
+
data.tar.gz: ed0641418b2cf847ba8396f22ca860373c3cb04d05f6110cda7f7d73348a54d9f3de54704cb7121982ed6772046e46e3f3decb247be19fb463e31b98c821f836
|
data/README.md
CHANGED
|
@@ -30,17 +30,27 @@ client = HCBV4::Client.from_credentials(
|
|
|
30
30
|
)
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
-
the client automatically refreshes expired tokens before each request.
|
|
33
|
+
the client automatically refreshes expired tokens before each request. to persist refreshed tokens, use the `on_token_refresh` callback:
|
|
34
34
|
|
|
35
35
|
```ruby
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
client = HCBV4::Client.from_credentials(
|
|
37
|
+
client_id: ENV["HCB_CLIENT_ID"],
|
|
38
|
+
client_secret: ENV["HCB_CLIENT_SECRET"],
|
|
39
|
+
access_token: user.hcb_access_token,
|
|
40
|
+
refresh_token: user.hcb_refresh_token,
|
|
41
|
+
expires_at: user.hcb_token_expires_at,
|
|
42
|
+
on_token_refresh: ->(token) {
|
|
43
|
+
user.update!(
|
|
44
|
+
hcb_access_token: token.token,
|
|
45
|
+
hcb_refresh_token: token.refresh_token,
|
|
46
|
+
hcb_token_expires_at: token.expires_at
|
|
47
|
+
)
|
|
48
|
+
}
|
|
41
49
|
)
|
|
42
50
|
```
|
|
43
51
|
|
|
52
|
+
the callback is invoked immediately after a token refresh, before the API request is made. this ensures your stored tokens are always up to date.
|
|
53
|
+
|
|
44
54
|
### disabling automatic token refresh
|
|
45
55
|
|
|
46
56
|
if you're managing token refresh yourself, you can disable automatic refresh:
|
|
@@ -498,53 +508,6 @@ tx.update!(memo: "new memo") # => Error: organization.id is nil
|
|
|
498
508
|
|
|
499
509
|
## recipes
|
|
500
510
|
|
|
501
|
-
### token persistence
|
|
502
|
-
|
|
503
|
-
the client automatically refreshes expired tokens. only persist when the token actually changes:
|
|
504
|
-
|
|
505
|
-
```ruby
|
|
506
|
-
class HCBService
|
|
507
|
-
def self.with(user, &block)
|
|
508
|
-
service = new(user)
|
|
509
|
-
block.call(service.client)
|
|
510
|
-
ensure
|
|
511
|
-
service.persist_if_refreshed!
|
|
512
|
-
end
|
|
513
|
-
|
|
514
|
-
def initialize(user)
|
|
515
|
-
@user = user
|
|
516
|
-
@original_token = user.hcb_access_token
|
|
517
|
-
end
|
|
518
|
-
|
|
519
|
-
def client
|
|
520
|
-
@client ||= HCBV4::Client.from_credentials(
|
|
521
|
-
client_id: ENV["HCB_CLIENT_ID"],
|
|
522
|
-
client_secret: ENV["HCB_CLIENT_SECRET"],
|
|
523
|
-
access_token: @user.hcb_access_token,
|
|
524
|
-
refresh_token: @user.hcb_refresh_token,
|
|
525
|
-
expires_at: @user.hcb_token_expires_at
|
|
526
|
-
)
|
|
527
|
-
end
|
|
528
|
-
|
|
529
|
-
def persist_if_refreshed!
|
|
530
|
-
return unless @client
|
|
531
|
-
token = @client.oauth_token
|
|
532
|
-
return if token.token == @original_token
|
|
533
|
-
|
|
534
|
-
@user.update!(
|
|
535
|
-
hcb_access_token: token.token,
|
|
536
|
-
hcb_refresh_token: token.refresh_token,
|
|
537
|
-
hcb_token_expires_at: token.expires_at
|
|
538
|
-
)
|
|
539
|
-
end
|
|
540
|
-
end
|
|
541
|
-
|
|
542
|
-
# usage:
|
|
543
|
-
HCBService.with(current_user) do |client|
|
|
544
|
-
client.organizations
|
|
545
|
-
end
|
|
546
|
-
```
|
|
547
|
-
|
|
548
511
|
### keep your ledger pretty
|
|
549
512
|
|
|
550
513
|
when a card grant is created, it generates a disbursement (transfer) from the org to the grant. you might want to label it:
|
data/lib/hcbv4/client.rb
CHANGED
|
@@ -9,21 +9,22 @@ module HCBV4
|
|
|
9
9
|
DEFAULT_BASE_URL = "https://hcb.hackclub.com"
|
|
10
10
|
API_PATH = "/api/v4"
|
|
11
11
|
|
|
12
|
-
attr_reader :oauth_token, :base_url, :auto_token_refresh
|
|
12
|
+
attr_reader :oauth_token, :base_url, :auto_token_refresh, :on_token_refresh
|
|
13
13
|
|
|
14
|
-
def initialize(oauth_token:, base_url: DEFAULT_BASE_URL, auto_token_refresh: true)
|
|
14
|
+
def initialize(oauth_token:, base_url: DEFAULT_BASE_URL, auto_token_refresh: true, on_token_refresh: nil)
|
|
15
15
|
@oauth_token = oauth_token
|
|
16
16
|
@base_url = base_url
|
|
17
17
|
@auto_token_refresh = auto_token_refresh
|
|
18
|
+
@on_token_refresh = on_token_refresh
|
|
18
19
|
end
|
|
19
20
|
|
|
20
21
|
def self.from_credentials(client_id:, client_secret:, access_token:, refresh_token:, expires_at: nil,
|
|
21
|
-
base_url: DEFAULT_BASE_URL, auto_token_refresh: true)
|
|
22
|
+
base_url: DEFAULT_BASE_URL, auto_token_refresh: true, on_token_refresh: nil)
|
|
22
23
|
oauth_client = OAuth2::Client.new(
|
|
23
24
|
client_id,
|
|
24
25
|
client_secret,
|
|
25
26
|
site: base_url,
|
|
26
|
-
token_url: "/oauth/token"
|
|
27
|
+
token_url: "/api/v4/oauth/token"
|
|
27
28
|
)
|
|
28
29
|
|
|
29
30
|
token = OAuth2::AccessToken.new(
|
|
@@ -33,7 +34,7 @@ module HCBV4
|
|
|
33
34
|
expires_at:
|
|
34
35
|
)
|
|
35
36
|
|
|
36
|
-
new(oauth_token: token, base_url:, auto_token_refresh:)
|
|
37
|
+
new(oauth_token: token, base_url:, auto_token_refresh:, on_token_refresh:)
|
|
37
38
|
end
|
|
38
39
|
|
|
39
40
|
# ─────────────────────────────────────────────────────────────────────────
|
|
@@ -564,6 +565,7 @@ module HCBV4
|
|
|
564
565
|
return unless oauth_token.expired?
|
|
565
566
|
|
|
566
567
|
@oauth_token = oauth_token.refresh!
|
|
568
|
+
on_token_refresh&.call(@oauth_token)
|
|
567
569
|
end
|
|
568
570
|
|
|
569
571
|
def connection
|
data/lib/hcbv4/version.rb
CHANGED