Class: MediumSdk::Connection::AuthCode
- Inherits:
-
Object
- Object
- MediumSdk::Connection::AuthCode
- Defined in:
- lib/medium_sdk/connection/auth_code.rb
Constant Summary collapse
- OAUTH_HOST =
'https://medium.com'
- OAUTH_AUTHZ_ENDPOINT =
'/m/oauth/authorize'
- OAUTH_TOKEN_ENDPOINT =
'/m/oauth/token'
- API_HOST =
'https://api.medium.com'
- API_VERSION =
'v1'
Instance Attribute Summary collapse
-
#authcode_client ⇒ Object
Returns the value of attribute authcode_client.
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#http ⇒ Object
Returns the value of attribute http.
-
#oauth2client ⇒ Object
Returns the value of attribute oauth2client.
-
#oauth_redirect_uri ⇒ Object
Returns the value of attribute oauth_redirect_uri.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #_add_redirect_uri(opts = {}) ⇒ Object
- #api_version_uri ⇒ Object
- #authorize_code(code, opts = {}) ⇒ Object
- #authorize_uri(opts = {}) ⇒ Object
- #init_attributes ⇒ Object
-
#initialize(opts = {}) ⇒ AuthCode
constructor
A new instance of AuthCode.
- #new_auth_code_client ⇒ Object
- #new_oauth2_client ⇒ Object
- #set_token(token) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ AuthCode
Returns a new instance of AuthCode.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/medium_sdk/connection/auth_code.rb', line 22 def initialize(opts = {}) init_attributes @client_id = opts[:client_id] if opts.key? :client_id @client_secret = opts[:client_secret] if opts.key? :client_secret @oauth_redirect_uri = opts[:redirect_uri] if opts.key? :redirect_uri @scope = opts[:scope] if opts.key? :scope @instance_headers = opts[:instance_headers] if opts.key? :instance_headers @oauth2client = new_oauth2_client @authcode_client = new_auth_code_client end |
Instance Attribute Details
#authcode_client ⇒ Object
Returns the value of attribute authcode_client.
16 17 18 |
# File 'lib/medium_sdk/connection/auth_code.rb', line 16 def authcode_client @authcode_client end |
#client_id ⇒ Object (readonly)
Returns the value of attribute client_id.
14 15 16 |
# File 'lib/medium_sdk/connection/auth_code.rb', line 14 def client_id @client_id end |
#http ⇒ Object
Returns the value of attribute http.
19 20 21 |
# File 'lib/medium_sdk/connection/auth_code.rb', line 19 def http @http end |
#oauth2client ⇒ Object
Returns the value of attribute oauth2client.
17 18 19 |
# File 'lib/medium_sdk/connection/auth_code.rb', line 17 def oauth2client @oauth2client end |
#oauth_redirect_uri ⇒ Object
Returns the value of attribute oauth_redirect_uri.
18 19 20 |
# File 'lib/medium_sdk/connection/auth_code.rb', line 18 def oauth_redirect_uri @oauth_redirect_uri end |
#token ⇒ Object
Returns the value of attribute token.
20 21 22 |
# File 'lib/medium_sdk/connection/auth_code.rb', line 20 def token @token end |
Instance Method Details
#_add_redirect_uri(opts = {}) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/medium_sdk/connection/auth_code.rb', line 88 def _add_redirect_uri(opts = {}) if !opts.key?(:redirect_uri) && @oauth_redirect_uri.to_s.length > 0 opts[:redirect_uri] = @oauth_redirect_uri.to_s end return opts end |
#api_version_uri ⇒ Object
73 74 75 |
# File 'lib/medium_sdk/connection/auth_code.rb', line 73 def api_version_uri() return File.join API_HOST, API_VERSION end |
#authorize_code(code, opts = {}) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/medium_sdk/connection/auth_code.rb', line 95 def (code, opts = {}) #token = @oauth2client.auth_code.get_token(code, _add_redirect_uri(opts)) params = { code: code, client_id: @client_id, client_secret: @client_secret, redirect_uri: @oauth_redirect_uri, grant_type: 'authorization_code' } res = @authcode_client.post '/v1/tokens', params set_token res.body return @token end |
#authorize_uri(opts = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/medium_sdk/connection/auth_code.rb', line 77 def (opts = {}) @oauth2client = new_oauth2_client() unless @oauth2client opts.merge!({ 'client_id' => @client_id, 'response_type' => 'code'}) if ! opts.key(:scope) && ! opts.key('scope') && @scope opts.merge!({ 'scope' => @scope }) end @oauth2client.auth_code. _add_redirect_uri(opts) end |
#init_attributes ⇒ Object
33 34 35 36 37 |
# File 'lib/medium_sdk/connection/auth_code.rb', line 33 def init_attributes() @token = nil @http = nil @instance_headers = nil end |
#new_auth_code_client ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/medium_sdk/connection/auth_code.rb', line 109 def new_auth_code_client return Faraday.new(url: API_HOST) do |faraday| faraday.request :url_encoded # form-encode POST params faraday.response :json, content_type: /\bjson$/ faraday.response :logger # log requests to STDOUT faraday.adapter Faraday.default_adapter # make requests with Net::HTTP end end |
#new_oauth2_client ⇒ Object
118 119 120 121 122 123 |
# File 'lib/medium_sdk/connection/auth_code.rb', line 118 def new_oauth2_client return OAuth2::Client.new(@client_id, @client_secret, site: OAUTH_HOST, authorize_url: OAUTH_AUTHZ_ENDPOINT, token_url: OAUTH_TOKEN_ENDPOINT) end |
#set_token(token) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/medium_sdk/connection/auth_code.rb', line 39 def set_token(token) if token.is_a? Hash token = OAuth2::AccessToken::from_hash @oauth2client, token elsif token.is_a? String if token =~ /^\s*{.+}\s*$/ token_hash = MultiJson.decode(token) token = OAuth2::AccessToken::from_hash @oauth2client, token_hash else token = { 'access_token' => token } token = OAuth2::AccessToken::from_hash @oauth2client, token end end unless token.is_a? OAuth2::AccessToken raise "Token is not a OAuth2::AccessToken" end @token = token @http = Faraday.new(url: api_version_uri()) do |conn| conn.request :oauth2_refresh, @token conn.request :multipart conn.request :json if @instance_headers.is_a? Hash @instance_headers.each do |k,v| conn.headers[k] = v end end conn.response :json, content_type: /\bjson$/ conn.response :logger conn.adapter Faraday.default_adapter end end |