Sha256: c8c70130842fc587c77a035f09ff9316dab372842b6c2f7def2fd0bdda4b7889

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

require "cookie_jar/version"
require 'rest-client'
require 'json'
require 'oauth2'

module CookieJar
  module Wallet
    WALLET_HOST = "http://localhost:3003"
    AA_HOST = "http://localhost:3002"

    class WalletBase
      attr_accessor :url, :client, :token, :user_id, :account_id

      def initialize(client_id, client_secret, username, password)
        self.client = OAuth2::Client.new(client_id, client_secret, :site => CookieJar::Wallet::AA_HOST)
        self.token = init_token(username, password)
        self.user_id = get_user_id
      end

      def init_token(username, password)
        self.client.password.get_token(username, password)
      end

      def get_user_id
        JSON.parse(self.token.get('/get_user').body)["id"]
      end

      def get_token
        if self.token.expired?
          self.token = token.refresh!
        end

        self.token
      end

      def get(args={})
        access_token = get_token.token
        begin
          response = RestClient.get self.url, :params => args.merge!({:access_token => access_token})
          JSON.parse(response)
        rescue => exception
          {:code => exception.response.code, :message => exception.message}
        end
      end

      def post(args={})
        access_token = get_token.token
        begin
          response = RestClient.post self.url, args.merge!(:access_token => access_token)
          JSON.parse(response)
        rescue => exception
          {:code => exception.response.code, :message => exception.message}
        end
      end

      def put(args)
        access_token = get_token.token
        begin
          response = RestClient.put self.url, args.merge!(:access_token => access_token)
          JSON.parse(response)
        rescue => exception
          {:code => exception.response.code, :message => exception.message}
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cookie_jar-0.0.1 lib/cookie_jar/wallet_base.rb