yt-auth 0.1.0 → 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/CHANGELOG.md +10 -0
- data/README.md +4 -4
- data/lib/yt/auth.rb +3 -3
- data/lib/yt/auth/version.rb +1 -1
- data/yt-auth.gemspec +1 -1
- metadata +4 -6
- data/lib/yt/auth_error.rb +0 -5
- data/lib/yt/auth_request.rb +0 -91
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f0c8b8b1fb86567a1fac8a9f824c02df621588c0
|
4
|
+
data.tar.gz: bef00004294f1b4f60dfc14c4f47821cd632bdf9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58ddb25205d859933ac217a9250806fa63a6de154710dde3984b4cf011f599631d3e5550bcc28a5909f472f197f984095d7cfce858ab061d8deef1308682080d
|
7
|
+
data.tar.gz: 02444caa168eddd2ca09218690b024baf76f67c6ab675cae7b91269ee4fb1ebd1cf14dd30b6559e19ffd8b79114b682c8cd4af61c207e13960af55ddeb064f27
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,16 @@ For more information about changelogs, check
|
|
6
6
|
[Keep a Changelog](http://keepachangelog.com) and
|
7
7
|
[Vandamme](http://tech-angels.github.io/vandamme).
|
8
8
|
|
9
|
+
## 0.2.0 - 2017-03-29
|
10
|
+
|
11
|
+
**How to upgrade**
|
12
|
+
|
13
|
+
If your code uses `Yt::AuthError` then you must use `Yt::HTTPError` instead.
|
14
|
+
If your code uses `Yt::AuthRequest` then you must use `Yt::HTTPRequest` instead.
|
15
|
+
|
16
|
+
* [ENHANCEMENT] Extract `AuthError` to `HTTPError` in yt-support gem
|
17
|
+
* [ENHANCEMENT] Extract `AuthRequest` to `HTTPRequest` in yt-support gem
|
18
|
+
|
9
19
|
## 0.1.0 - 2017-03-27
|
10
20
|
|
11
21
|
* [FEATURE] Add `AuthError`
|
data/README.md
CHANGED
@@ -12,7 +12,7 @@ The **source code** is available on [GitHub](https://github.com/fullscreen/yt-au
|
|
12
12
|
[](http://www.rubydoc.info/gems/yt-auth/frames)
|
13
13
|
[](http://rubygems.org/gems/yt-auth)
|
14
14
|
|
15
|
-
The Yt::Auth
|
15
|
+
The Yt::Auth class provides two public methods: `url` and `email`.
|
16
16
|
|
17
17
|
Yt::Auth#url
|
18
18
|
------------
|
@@ -42,17 +42,17 @@ Yt::Auth.new(redirect_uri: redirect_uri, code: code).email
|
|
42
42
|
# => "user@example.com"
|
43
43
|
```
|
44
44
|
|
45
|
-
Yt::
|
45
|
+
Yt::HTTPError
|
46
46
|
-------------
|
47
47
|
|
48
|
-
`Yt::
|
48
|
+
`Yt::HTTPError` will be raised whenever something goes wrong during the
|
49
49
|
authentication process. The message of the error will include the details:
|
50
50
|
|
51
51
|
```ruby
|
52
52
|
redirect_uri = 'https://example.com/auth' # REPLACE WITH REAL ONE
|
53
53
|
code = 'this-is-not-a-valid-code'
|
54
54
|
Yt::Auth.new(redirect_uri: redirect_uri, code: code).email
|
55
|
-
# => Yt::
|
55
|
+
# => Yt::HTTPError: Invalid authorization code.
|
56
56
|
```
|
57
57
|
|
58
58
|
|
data/lib/yt/auth.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'yt/config'
|
2
|
-
require 'yt/
|
2
|
+
require 'yt/http_request'
|
3
3
|
|
4
4
|
# An object-oriented Ruby client for YouTube.
|
5
5
|
# @see http://www.rubydoc.info/gems/yt/
|
@@ -27,7 +27,7 @@ module Yt
|
|
27
27
|
|
28
28
|
# @return [String] the email of an authenticated Google account.
|
29
29
|
def email
|
30
|
-
response =
|
30
|
+
response = HTTPRequest.new(email_params).run
|
31
31
|
response.body['email']
|
32
32
|
end
|
33
33
|
|
@@ -51,7 +51,7 @@ module Yt
|
|
51
51
|
|
52
52
|
# @return [Hash] the tokens of an authenticated Google account.
|
53
53
|
def tokens
|
54
|
-
|
54
|
+
HTTPRequest.new(tokens_params).run.body
|
55
55
|
end
|
56
56
|
|
57
57
|
def tokens_params
|
data/lib/yt/auth/version.rb
CHANGED
data/yt-auth.gemspec
CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
23
|
spec.require_paths = ['lib']
|
24
24
|
|
25
|
-
spec.add_dependency 'yt-support', '>= 0.1'
|
25
|
+
spec.add_dependency 'yt-support', '>= 0.1.1'
|
26
26
|
|
27
27
|
spec.add_development_dependency 'bundler', '~> 1.14'
|
28
28
|
spec.add_development_dependency 'rspec', '~> 3.5'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yt-auth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Claudio Baccigalupo
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2017-03-
|
12
|
+
date: 2017-03-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: yt-support
|
@@ -17,14 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version:
|
20
|
+
version: 0.1.1
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version:
|
27
|
+
version: 0.1.1
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: bundler
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -131,8 +131,6 @@ files:
|
|
131
131
|
- bin/setup
|
132
132
|
- lib/yt/auth.rb
|
133
133
|
- lib/yt/auth/version.rb
|
134
|
-
- lib/yt/auth_error.rb
|
135
|
-
- lib/yt/auth_request.rb
|
136
134
|
- yt-auth.gemspec
|
137
135
|
homepage: https://github.com/fullscreen/yt-auth
|
138
136
|
licenses:
|
data/lib/yt/auth_error.rb
DELETED
data/lib/yt/auth_request.rb
DELETED
@@ -1,91 +0,0 @@
|
|
1
|
-
require 'net/http'
|
2
|
-
require 'json'
|
3
|
-
require 'yt/auth_error'
|
4
|
-
|
5
|
-
module Yt
|
6
|
-
# @private
|
7
|
-
# A wrapper around Net::HTTP to send HTTP requests to any web API and
|
8
|
-
# return their result or raise an error if the result is unexpected.
|
9
|
-
class AuthRequest
|
10
|
-
# Initializes an AuthRequest object.
|
11
|
-
def initialize(options = {})
|
12
|
-
@host = options.fetch :host, 'www.googleapis.com'
|
13
|
-
@path = options[:path]
|
14
|
-
@method = options.fetch :method, :get
|
15
|
-
@headers = options.fetch :headers, {}
|
16
|
-
@body = options[:body]
|
17
|
-
@request_format = options.fetch :request_format, :json
|
18
|
-
@error_message = options.fetch :error_message, ->(body) {"Error: #{body}"}
|
19
|
-
end
|
20
|
-
|
21
|
-
# Sends the request and returns the response.
|
22
|
-
def run
|
23
|
-
if response.is_a? Net::HTTPSuccess
|
24
|
-
response.tap do
|
25
|
-
parse_response!
|
26
|
-
end
|
27
|
-
else
|
28
|
-
raise Yt::AuthError, error_message
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
# @return [URI::HTTPS] the (memoized) URI of the request.
|
35
|
-
def uri
|
36
|
-
attributes = {host: @host, path: @path}
|
37
|
-
@uri ||= URI::HTTPS.build attributes
|
38
|
-
end
|
39
|
-
|
40
|
-
# @return [Net::HTTPRequest] the full HTTP request object,
|
41
|
-
# inclusive of headers of request body.
|
42
|
-
def http_request
|
43
|
-
net_http_class = Object.const_get "Net::HTTP::#{@method.capitalize}"
|
44
|
-
@http_request ||= net_http_class.new(uri.request_uri).tap do |request|
|
45
|
-
set_request_body! request
|
46
|
-
set_request_headers! request
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
# Adds the request body to the request in the appropriate format.
|
51
|
-
# if the request body is a JSON Object, transform its keys into camel-case,
|
52
|
-
# since this is the common format for JSON APIs.
|
53
|
-
def set_request_body!(request)
|
54
|
-
if @body
|
55
|
-
request.set_form_data @body
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
# Adds the request headers to the request in the appropriate format.
|
60
|
-
# The User-Agent header is also set to recognize the request, and to
|
61
|
-
# tell the server that gzip compression can be used, since Net::HTTP
|
62
|
-
# supports it and automatically sets the Accept-Encoding header.
|
63
|
-
def set_request_headers!(request)
|
64
|
-
if @request_format == :json
|
65
|
-
request.initialize_http_header 'Content-Type' => 'application/json'
|
66
|
-
end
|
67
|
-
@headers.each do |name, value|
|
68
|
-
request.add_field name, value
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
# Run the request and memoize the response or the server error received.
|
73
|
-
def response
|
74
|
-
@response ||= Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
|
75
|
-
http.request http_request
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
# Replaces the body of the response with the parsed version of the body,
|
80
|
-
# according to the format specified in the AuthRequest.
|
81
|
-
def parse_response!
|
82
|
-
if response.body
|
83
|
-
response.body = JSON response.body
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def error_message
|
88
|
-
@error_message.call response.body
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|