Class: HelperService::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/netpay_ecommerce_ruby/helpers/helper_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Service

Returns a new instance of Service.



7
8
9
10
11
12
13
14
15
# File 'lib/netpay_ecommerce_ruby/helpers/helper_service.rb', line 7

def initialize options
  @endpoint  = options.fetch(:service_name, nil)
  @method    = options.fetch(:method, nil)
  @security  = options.fetch(:security_token, false)
  @host      = options.fetch(:host, nil)
  @body      = options.fetch(:body, nil)
  @token     = options.fetch(:token, nil)
  @transport = options.fetch(:transport_method, nil)
end

Instance Method Details

#deleteObject



79
80
81
# File 'lib/netpay_ecommerce_ruby/helpers/helper_service.rb', line 79

def delete
  
end

#getObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/netpay_ecommerce_ruby/helpers/helper_service.rb', line 21

def get
  uri = URI("#{@host}#{@endpoint}#{@body}")
  http = Net::HTTP.new(uri.host, uri.port)

  if uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  request = Net::HTTP::Get.new(uri)
  request["authorization"] = @token
  request["Content-Type"] = 'application/json; charset=utf-8'
  request["accept"] = 'application/json'

  response = http.request(request)

  if ("200".."299").to_a.include? response.code
    [JSON.parse(response.body), response.code]
  elsif ("400".."499").to_a.include? response.code
    [JSON.parse(response.body), response.code]
  end
end

#postObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/netpay_ecommerce_ruby/helpers/helper_service.rb', line 44

def post
  url = URI("#{@host}#{@endpoint}")

  http = Net::HTTP.new(url.host, url.port)

  if url.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  request = Net::HTTP::Post.new(url)
  request["Content-Type"] = 'application/json; charset=utf-8'
  request["Accept"] = 'application/json'
  if @security
    request["authorization"] = @token
  end
  if @transport == "json"
    request.body = @body
  else
    if @body.class == Hash
      request.body = @body.to_json
    else
      request.body = @body
    end
  end

  response = http.request(request)
  
  if ("200".."299").to_a.include? response.code
    [JSON.parse(response.body), response.code]
  elsif ("400".."499").to_a.include? response.code
    [JSON.parse(response.body), response.code]
  end
end

#send_requestObject



17
18
19
# File 'lib/netpay_ecommerce_ruby/helpers/helper_service.rb', line 17

def send_request
  self.send @method
end