Class: Pokepay::Client
- Inherits:
-
Object
- Object
- Pokepay::Client
- Defined in:
- lib/pokepay_partner_ruby_sdk/client.rb
Instance Method Summary collapse
- #delete(path, body_params) ⇒ Object
- #get(path, body_params) ⇒ Object
-
#initialize(inifile_or_hash) ⇒ Client
constructor
A new instance of Client.
- #is_server_error(res) ⇒ Object
- #is_success(res) ⇒ Object
- #patch(path, body_params) ⇒ Object
- #post(path, body_params) ⇒ Object
- #put(path, body_params) ⇒ Object
- #request(request_class, path, body_params, response_class) ⇒ Object
- #send(request) ⇒ Object
- #terminate ⇒ Object
Constructor Details
#initialize(inifile_or_hash) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/pokepay_partner_ruby_sdk/client.rb', line 15 def initialize(inifile_or_hash) case inifile_or_hash when String then path = File.(inifile_or_hash) if File.exist?(path) ini = IniFile.load(path) else raise "init file does not exist." end @client_id = ini['global']['CLIENT_ID'] @client_secret = ini['global']['CLIENT_SECRET'] @api_base_url = URI.parse(ini['global']['API_BASE_URL']) @ssl_key_file = ini['global']['SSL_KEY_FILE'] @ssl_cert_file = ini['global']['SSL_CERT_FILE'] @timezone = ini['global']['TIMEZONE'] @timeout = ini['global']['TIMEOUT'] when Hash then @client_id = inifile_or_hash[:client_id] @client_secret = inifile_or_hash[:client_secret] @api_base_url = URI.parse(inifile_or_hash[:api_base_url]) @ssl_key_file = inifile_or_hash[:ssl_key_file] @ssl_cert_file = inifile_or_hash[:ssl_cert_file] @timezone = inifile_or_hash[:timezone] @timeout = inifile_or_hash[:timeout] end @http = Net::HTTP.new(@api_base_url.host, @api_base_url.port) if @api_base_url.scheme == 'https' @http.use_ssl = true @http.cert = OpenSSL::X509::Certificate.new(File.read(@ssl_cert_file)) @http.key = OpenSSL::PKey::RSA.new(File.read(@ssl_key_file)) @http.verify_mode = OpenSSL::SSL::VERIFY_PEER end @http.start() @crypto = Pokepay::Crypto.new(@client_secret) end |
Instance Method Details
#delete(path, body_params) ⇒ Object
100 101 102 |
# File 'lib/pokepay_partner_ruby_sdk/client.rb', line 100 def delete(path, body_params) request(Net::HTTP::Delete, path, body_params, nil) end |
#get(path, body_params) ⇒ Object
92 93 94 |
# File 'lib/pokepay_partner_ruby_sdk/client.rb', line 92 def get(path, body_params) request(Net::HTTP::Get, path, body_params, nil) end |
#is_server_error(res) ⇒ Object
56 57 58 59 |
# File 'lib/pokepay_partner_ruby_sdk/client.rb', line 56 def is_server_error(res) code = res.code.to_i 500 <= code and code < 600 end |
#is_success(res) ⇒ Object
51 52 53 54 |
# File 'lib/pokepay_partner_ruby_sdk/client.rb', line 51 def is_success(res) code = res.code.to_i 200 <= code and code < 300 end |
#patch(path, body_params) ⇒ Object
104 105 106 |
# File 'lib/pokepay_partner_ruby_sdk/client.rb', line 104 def patch(path, body_params) request(Net::HTTP::Patch, path, body_params, nil) end |
#post(path, body_params) ⇒ Object
96 97 98 |
# File 'lib/pokepay_partner_ruby_sdk/client.rb', line 96 def post(path, body_params) request(Net::HTTP::Post, path, body_params, nil) end |
#put(path, body_params) ⇒ Object
108 109 110 |
# File 'lib/pokepay_partner_ruby_sdk/client.rb', line 108 def put(path, body_params) request(Net::HTTP::Put, path, body_params, nil) end |
#request(request_class, path, body_params, response_class) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/pokepay_partner_ruby_sdk/client.rb', line 61 def request(request_class, path, body_params, response_class) encrypt_data = { 'request_data' => body_params, 'timestamp' => Time.now.iso8601(6), 'partner_call_id' => SecureRandom.uuid } params = {"partner_client_id" => @client_id, "data" => Base64.urlsafe_encode64(@crypto.encrypt(JSON.generate(encrypt_data))).tr("=", "")} req = request_class.new(path) req.set_form_data(params) if not @http.active? @http.start() end res = @http.request(req) if is_server_error(res) then raise format("Server Error (code: %s)", res.code) end res_map = JSON.parse(res.body) if(res_map["response_data"]) then res.body = JSON.parse(@crypto.decrypt(Base64.urlsafe_decode64(res_map["response_data"])) .force_encoding("utf-8")) else res.body = res_map end if is_success(res) and response_class Pokepay::Response::Response.new(res, response_class.new(res.body)) else res end end |
#send(request) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/pokepay_partner_ruby_sdk/client.rb', line 112 def send(request) request_class = case request.method when "GET" then Net::HTTP::Get when "POST" then Net::HTTP::Post when "DELETE" then Net::HTTP::Delete when "PATCH" then Net::HTTP::Patch when "PUT" then Net::HTTP::Put else raise "Wrong method" end request(request_class, request.path, request.body_params, request.response_class) end |
#terminate ⇒ Object
124 125 126 127 128 |
# File 'lib/pokepay_partner_ruby_sdk/client.rb', line 124 def terminate() if @http.active? @http.finish end end |