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