Class: HelperService::Service
- Inherits:
-
Object
- Object
- HelperService::Service
- Defined in:
- lib/netpay_ecommerce_ruby/helpers/helper_service.rb
Instance Method Summary collapse
- #delete ⇒ Object
- #get ⇒ Object
-
#initialize(options) ⇒ Service
constructor
A new instance of Service.
- #post ⇒ Object
- #send_request ⇒ Object
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 @endpoint = .fetch(:service_name, nil) @method = .fetch(:method, nil) @security = .fetch(:security_token, false) @host = .fetch(:host, nil) @body = .fetch(:body, nil) @token = .fetch(:token, nil) @transport = .fetch(:transport_method, nil) end |
Instance Method Details
#delete ⇒ Object
79 80 81 |
# File 'lib/netpay_ecommerce_ruby/helpers/helper_service.rb', line 79 def delete end |
#get ⇒ Object
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 |
#post ⇒ Object
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_request ⇒ Object
17 18 19 |
# File 'lib/netpay_ecommerce_ruby/helpers/helper_service.rb', line 17 def send_request self.send @method end |