Class: Micropub::Token
- Inherits:
-
Object
- Object
- Micropub::Token
- Defined in:
- lib/micropub/token.rb
Instance Method Summary collapse
- #config ⇒ Object
-
#initialize(token) ⇒ Token
constructor
A new instance of Token.
- #me ⇒ Object
- #request ⇒ Object
- #response ⇒ Object
- #scope ⇒ Object
- #valid? ⇒ Boolean
- #valid_host? ⇒ Boolean
- #valid_scope? ⇒ Boolean
Constructor Details
#initialize(token) ⇒ Token
Returns a new instance of Token.
8 9 10 |
# File 'lib/micropub/token.rb', line 8 def initialize(token) @token = token end |
Instance Method Details
#config ⇒ Object
51 52 53 |
# File 'lib/micropub/token.rb', line 51 def config Micropub.configuration end |
#me ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/micropub/token.rb', line 33 def me begin URI.parse(response["me"]) rescue URI::InvalidURIError raise ValidationError, "The token endpoint did not contain a valid 'me' URI" end end |
#request ⇒ Object
47 48 49 |
# File 'lib/micropub/token.rb', line 47 def request HTTPClient.get(config.token_endpoint, {}, {'Authorization' => "Bearer #{@token}"}) end |
#response ⇒ Object
41 42 43 44 45 |
# File 'lib/micropub/token.rb', line 41 def response @response ||= begin URI::decode_www_form(request.body).to_h end end |
#scope ⇒ Object
29 30 31 |
# File 'lib/micropub/token.rb', line 29 def scope response["scope"].split(",").map(&:to_sym) end |
#valid? ⇒ Boolean
12 13 14 15 16 17 18 19 |
# File 'lib/micropub/token.rb', line 12 def valid? begin valid_host? && valid_scope? rescue ValidationError false end end |
#valid_host? ⇒ Boolean
25 26 27 |
# File 'lib/micropub/token.rb', line 25 def valid_host? me.host == config.me.host end |
#valid_scope? ⇒ Boolean
21 22 23 |
# File 'lib/micropub/token.rb', line 21 def valid_scope? (config.allowed_scopes & scope).any? end |