Class: SlidePay::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/slidepay/client.rb', line 5

def initialize(options={})
  if options[:endpoint]
    @endpoint = options[:endpoint]
  end
  if options[:email]
    @email = options[:email]
  end
  if options[:password]
    @password = options[:password]
  end
  if options[:token]
    @token = options[:token]
  end
  if options[:api_key]
    @api_key = options[:api_key]
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



3
4
5
# File 'lib/slidepay/client.rb', line 3

def api_key
  @api_key
end

#emailObject

Returns the value of attribute email.



3
4
5
# File 'lib/slidepay/client.rb', line 3

def email
  @email
end

#endpointObject

Returns the value of attribute endpoint.



3
4
5
# File 'lib/slidepay/client.rb', line 3

def endpoint
  @endpoint
end

#passwordObject

Returns the value of attribute password.



3
4
5
# File 'lib/slidepay/client.rb', line 3

def password
  @password
end

#tokenObject

Returns the value of attribute token.



3
4
5
# File 'lib/slidepay/client.rb', line 3

def token
  @token
end

Instance Method Details

#authenticate(email = nil, password = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/slidepay/client.rb', line 27

def authenticate(email=nil, password=nil)
  email = email || @email
  password = password || @password

  response = SlidePay.retrieve_token(email, password)

  if response.was_successful?
    @endpoint = "#{response.endpoint}"
    @token = response.data
    true
  else
    false
  end
end

#delete(request_params) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/slidepay/client.rb', line 64

def delete(request_params)
  options = {}
  if request_params.is_a? String
    options = { :path => request_params, :api_key => @api_key, :token => @token, :endpoint => @endpoint }
  else
    options = request_params.merge! api_key: @api_key, token: @token, endpoint: @endpoint
  end
  SlidePay.delete(options)
end

#destroy(resource) ⇒ Object



98
99
100
# File 'lib/slidepay/client.rb', line 98

def destroy(resource)
  resource.destroy(api_key: @api_key, token: @token, endpoint: @endpoint)
end

#get(request_params) ⇒ Object

Base Request Methods



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

def get(request_params)
  options = {}
  if request_params.is_a? String
    options = { :path => request_params, :api_key => @api_key, :token => @token, :endpoint => @endpoint }
  else
    options = request_params.merge! api_key: @api_key, token: @token, endpoint: @endpoint
  end

  SlidePay.get(options)
end

#is_authenticated?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/slidepay/client.rb', line 23

def is_authenticated?
  @token != nil || @api_key != nil
end

#list(resource) ⇒ Object

Resource Methods



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/slidepay/client.rb', line 76

def list(resource)
  response = SlidePay.get(path: resource.url_root, api_key: @api_key, token: @token, endpoint: @endpoint)
  if response.was_successful?
    resources = []
    response.data.each do |resource_instance|
      resources.push resource.class.new(resource_instance)
    end
  else
    resources = []
  end

  resources
end

#post(request_params) ⇒ Object



59
60
61
62
# File 'lib/slidepay/client.rb', line 59

def post(request_params)
  request_params.merge! api_key: @api_key, token: @token, endpoint: @endpoint
  SlidePay.post(request_params)
end

#put(request_params) ⇒ Object



54
55
56
57
# File 'lib/slidepay/client.rb', line 54

def put(request_params)
  request_params.merge! api_key: @api_key, token: @token, endpoint: @endpoint
  SlidePay.put(request_params)
end

#retrieve(resource) ⇒ Object



90
91
92
# File 'lib/slidepay/client.rb', line 90

def retrieve(resource)
  resource.retrieve(api_key: @api_key, token: @token, endpoint: @endpoint)
end

#save(resource) ⇒ Object



94
95
96
# File 'lib/slidepay/client.rb', line 94

def save(resource)
  resource.save(api_key: @api_key, token: @token, endpoint: @endpoint)
end