Sha256: ce4305e1ff44525627d7bdc9bb75662c978b7c06ef87aaaebaf532bf016e797c

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

module AccountKit
  class API
    ME_URL = "https://graph.accountkit.com/#{VERSION}/me".freeze
    GRANT_TYPE = 'authorization_code'.freeze

    attr_accessor :app_id, :app_secret, :version

    def initialize(options = {})
      @app_id = options[:app_id] || ENV['ACCOUNT_KIT_APP_ID']
      @app_secret = options[:app_secret] || ENV['ACCOUNT_KIT_APP_SECRET']
      @version = options[:version] || ENV['ACCOUNT_KIT_VERSION']
    end

    def access_token(code)
      uri = build_access_token_uri(code)
      send_payload(uri)
    end

    def me(access_token)
      uri = build_me_uri(access_token)
      send_payload(uri)
    end

    private

    def send_payload(uri)
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true

      request = Net::HTTP::Get.new(uri.request_uri)
      request.content_type = 'application/json'

      http.request(request)
    end

    def build_access_token_uri(code)
      uri = URI(access_token_url)
      uri.query = URI.encode_www_form(access_token_params(code))
      uri
    end

    def build_me_uri(access_token)
      uri = URI(me_url)
      uri.query = URI.encode_www_form(access_token: access_token)
      uri
    end

    def access_token_params(code)
      {
        grant_type: GRANT_TYPE,
        code: code,
        access_token: app_access_token
      }
    end

    def access_token_url
      "https://graph.accountkit.com/#{@version}/access_token"
    end

    def me_url
      "https://graph.accountkit.com/#{@version}/me"
    end

    def app_access_token
      ['AA', @app_id, @app_secret].join('|')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
account_kit-0.1.0 lib/account_kit/api.rb