Class: SecupayRuby::Requests::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/secupay_ruby/requests/base.rb

Direct Known Subclasses

Cancel, Capture, GetTypes, Init, RequestApiKey, Status

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, payment: nil, body: {}) ⇒ Base

Returns a new instance of Base.



27
28
29
30
31
# File 'lib/secupay_ruby/requests/base.rb', line 27

def initialize(api_key: , payment: nil, body: {})
  @api_key = api_key
  @payment = payment
  @body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



25
26
27
# File 'lib/secupay_ruby/requests/base.rb', line 25

def body
  @body
end

#paymentObject (readonly)

Returns the value of attribute payment.



25
26
27
# File 'lib/secupay_ruby/requests/base.rb', line 25

def payment
  @payment
end

Class Method Details

.post(api_key:, payment: nil, body: {}) ⇒ Object



18
19
20
21
22
# File 'lib/secupay_ruby/requests/base.rb', line 18

def post(api_key: , payment: nil, body: {})
  new(api_key: api_key,
      payment: payment,
      body: body).post
end

Instance Method Details

#defaultsObject



45
46
47
48
49
# File 'lib/secupay_ruby/requests/base.rb', line 45

def defaults
  {
    apikey: @api_key.key
  }
end

#http_requestObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/secupay_ruby/requests/base.rb', line 51

def http_request
  @request ||= begin
    request = Net::HTTP::Post.new(uri.request_uri,
                                  {
                                    "Content-Type" => "application/json; charset=utf-8;",
                                    "Accept" => "application/json;"
                                  })

    request.body = { data: defaults.merge(body) }.to_json

    request
  end
end

#http_responseObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/secupay_ruby/requests/base.rb', line 65

def http_response
  @response ||= begin
    response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
      http.request(http_request)
    end

    json_response = JSON.parse(response.body)

    response = SecupayRuby::Requests::Response.new(http_status: response.header.code,
                                                   status: json_response["status"],
                                                   data: json_response["data"],
                                                   errors: json_response["errors"])

    raise RequestError.new(response.errors) if response.failed?

    response
  end
end

#pathObject



41
42
43
# File 'lib/secupay_ruby/requests/base.rb', line 41

def path
  raise NotImplementedError.new "Abstract method!"
end

#postObject



33
34
35
# File 'lib/secupay_ruby/requests/base.rb', line 33

def post
  http_response
end

#uriObject



37
38
39
# File 'lib/secupay_ruby/requests/base.rb', line 37

def uri
  @uri ||= URI.parse([SecupayRuby.config.host, path].join("/"))
end