class MediumSdk::Connection::AuthCode

Constants

API_HOST
API_VERSION
OAUTH_AUTHZ_ENDPOINT
OAUTH_HOST
OAUTH_TOKEN_ENDPOINT

Attributes

authcode_client[RW]
client_id[R]
http[RW]
oauth2client[RW]
oauth_redirect_uri[RW]
token[RW]

Public Class Methods

new(opts = {}) click to toggle source
# 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

Public Instance Methods

_add_redirect_uri(opts = {}) click to toggle source
# 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() click to toggle source
# 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 = {}) click to toggle source
# File lib/medium_sdk/connection/auth_code.rb, line 95
def authorize_code(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 = {}) click to toggle source
# File lib/medium_sdk/connection/auth_code.rb, line 77
def authorize_uri(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.authorize_url _add_redirect_uri(opts)
end
init_attributes() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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