Class: PayWithExtend::Client

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

Defined Under Namespace

Classes: ResponseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



11
12
13
14
# File 'lib/pay_with_extend/client.rb', line 11

def initialize
  # Singin
  self.refresh_token
end

Instance Attribute Details

#api_tokenObject

Returns the value of attribute api_token.



9
10
11
# File 'lib/pay_with_extend/client.rb', line 9

def api_token
  @api_token
end

Instance Method Details

#create_virtual_card(params) ⇒ Object



76
77
78
# File 'lib/pay_with_extend/client.rb', line 76

def create_virtual_card(params)
  post("/virtualcards", params, @api_token)
end

#get(endpoint, token = nil, secured = false) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pay_with_extend/client.rb', line 49

def get(endpoint, token = nil, secured = false)
  endpoint_url = secured ? "#{PayWithExtend.configuration.secure_base_url}#{endpoint}" : "#{PayWithExtend.configuration.base_url}#{endpoint}"
  headers = {
    "Content-Type" => "application/json",
    "Accept" => PayWithExtend.configuration.api_version,
  }
  headers["Authorization"] = "Bearer #{token}" if !token.nil?
  begin
    response = RestClient::Request.execute(method: :get, url: endpoint_url, headers: headers)
    JSON.parse(response.body)
  rescue RestClient::ExceptionWithResponse => exception
    JSON.parse(exception.response.body)
  rescue JSON::ParserError => exception
    puts exception.response
  rescue RestClient::Unauthorized, RestClient::Forbidden => exception
    puts exception.response
  end
end

#get_secured_virtual_card(card_id) ⇒ Object



72
73
74
# File 'lib/pay_with_extend/client.rb', line 72

def get_secured_virtual_card(card_id)
  get("/virtualcards/#{card_id}", @api_token, true)
end

#get_virtual_card(card_id) ⇒ Object



68
69
70
# File 'lib/pay_with_extend/client.rb', line 68

def get_virtual_card(card_id)
  get("/virtualcards/#{card_id}", @api_token, false)
end

#post(endpoint, body, token = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pay_with_extend/client.rb', line 24

def post(endpoint, body, token = nil)
  endpoint_url = "#{PayWithExtend.configuration.base_url}#{endpoint}"
  headers = {
    "Content-Type" => "application/json",
    "Accept" => PayWithExtend.configuration.api_version,
  }
  headers["Authorization"] = "Bearer #{token}" if !token.nil?
  begin
    response = RestClient::Request.execute(method: :post, url: endpoint_url, payload: body.to_json, headers: headers)
    if endpoint === "/signin"
      result = JSON.parse(response.body)
      @api_token = result["token"] if !result["token"].blank?
      return "Token refreshed"
    else
      JSON.parse(response.body)
    end
  rescue RestClient::ExceptionWithResponse => exception
    JSON.parse(exception.response.body)
  rescue JSON::ParserError => exception
    puts exception.response
  rescue RestClient::Unauthorized, RestClient::Forbidden => exception
    puts exception.response
  end
end

#refresh_tokenObject



16
17
18
19
20
21
22
# File 'lib/pay_with_extend/client.rb', line 16

def refresh_token
  request_body = {
    email: PayWithExtend.configuration.,
    password: PayWithExtend.configuration.,
  }
  post("/signin", request_body)
end