Class: Kiik::Wallet::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kiik/wallet/client.rb

Constant Summary collapse

ALLOWED_TYPES =
{
    :client => [ :create, :detail ],
    :user => [ :create ],
    :creditcard => [ :create, :detail ],
    :transaction => [ :create ]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wallet, options) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
21
22
# File 'lib/kiik/wallet/client.rb', line 14

def initialize(wallet,options)
    @id = options[:api_id]
    @name = options[:name]
    @email = options[:email]
    @secret = options[:api_secret]
    @access_token = options[:access_token]
    @wallet = wallet
    @base_uri = '/clients/#{id}'
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



12
13
14
# File 'lib/kiik/wallet/client.rb', line 12

def access_token
  @access_token
end

#emailObject (readonly)

Returns the value of attribute email.



12
13
14
# File 'lib/kiik/wallet/client.rb', line 12

def email
  @email
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/kiik/wallet/client.rb', line 12

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/kiik/wallet/client.rb', line 12

def name
  @name
end

#secretObject (readonly)

Returns the value of attribute secret.



12
13
14
# File 'lib/kiik/wallet/client.rb', line 12

def secret
  @secret
end

Instance Method Details

#cardObject



87
88
89
# File 'lib/kiik/wallet/client.rb', line 87

def card
    @card ||= Kiik::Wallet::Creditcard.new(self)
end

#clientObject



28
29
30
# File 'lib/kiik/wallet/client.rb', line 28

def client
    @wallet.client
end

#create(type, options = {}, &block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/kiik/wallet/client.rb', line 69

def create(type, options={},&block)
    allow(type,:create)

    token = options.delete(:token)
    case type
    when :user
        user.create(options,&block)
    when :creditcard
        card.create(token,options,&block)
    when :transaction
        transaction.create(token,options.delete(:credit_card),options,&block)
    end
end

#detail(type, options = {}, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kiik/wallet/client.rb', line 41

def detail(type,options={},&block)
    allow(type,:detail)

    case type
    when :client
        request(:get, base_uri,{}, &block)
    when :creditcard
        token = options.delete(:token)
        card.detail(token,options,&block)
    end

end

#list(type, options = {}, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/kiik/wallet/client.rb', line 54

def list(type,options={},&block)
    allow(type,:list)

    case type
    when :creditcard
        token = options.delete(:token)
        card.list(token,&block)
    end

end

#request(verb, uri, params = {}, &block) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/kiik/wallet/client.rb', line 95

def request(verb, uri, params = {}, &block)
    response = @wallet.client.request(verb, "/#{@wallet.version}#{uri}", request_params(params))

    if block_given?
        yield(response.parsed)
    else
        response.parsed
    end
end

#set_secret(secret, &block) ⇒ Object



65
66
67
# File 'lib/kiik/wallet/client.rb', line 65

def set_secret(secret,&block)
    request(:put, base_uri, { :client => { :api_secret => secret } },&block)
end

#tokenObject



24
25
26
# File 'lib/kiik/wallet/client.rb', line 24

def token
    @wallet.token
end

#transactionObject



91
92
93
# File 'lib/kiik/wallet/client.rb', line 91

def transaction
    @transaction ||= Kiik::Wallet::Transaction.new(self)
end

#use_token(new_token, &block) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/kiik/wallet/client.rb', line 32

def use_token(new_token,&block)
  @wallet.client.token = new_token
  begin
    block.call(self)
  ensure
    @wallet.client.token =  @wallet.token
  end
end

#userObject



83
84
85
# File 'lib/kiik/wallet/client.rb', line 83

def user
    @user ||= Kiik::Wallet::User.new(self)
end