class Bunq::Resource
Constants
- APPLICATION_JSON
- NO_PARAMS
Attributes
client[R]
path[R]
Public Class Methods
new(client, path)
click to toggle source
# File lib/bunq/resource.rb, line 13 def initialize(client, path) @client = client @path = path end
Public Instance Methods
append(path)
click to toggle source
# File lib/bunq/resource.rb, line 58 def append(path) Bunq::Resource.new(client, @path + path) end
get(params = {}, &block)
click to toggle source
# File lib/bunq/resource.rb, line 18 def get(params = {}, &block) resource.get({params: params}.merge(bunq_request_headers('GET', params))) do |response, request, result| verify_and_handle_response(response, request, result, &block) end rescue RestClient::Exceptions::Timeout raise Bunq::Timeout end
post(payload, skip_verify: false, encrypt: false, custom_headers: {}, &block)
click to toggle source
# File lib/bunq/resource.rb, line 26 def post(payload, skip_verify: false, encrypt: false, custom_headers: {}, &block) custom_headers = JSON.parse(custom_headers.to_json) body = post_body(payload, custom_headers) body, headers = client.encryptor.encrypt(body) if encrypt headers = headers.to_h.merge(custom_headers) headers = bunq_request_headers('POST', NO_PARAMS, body, headers) resource.post(body, headers) do |response, request, result| if skip_verify handle_response(response, request, result, &block) else verify_and_handle_response(response, request, result, &block) end end rescue RestClient::Exceptions::Timeout raise Bunq::Timeout end
put(payload, encrypt: false, &block)
click to toggle source
# File lib/bunq/resource.rb, line 45 def put(payload, encrypt: false, &block) body = JSON.generate(payload) body, headers = client.encryptor.encrypt(body) if encrypt headers = bunq_request_headers('PUT', NO_PARAMS, body, headers || {}) resource.put(body, headers) do |response, request, result| verify_and_handle_response(response, request, result, &block) end rescue RestClient::Exceptions::Timeout raise Bunq::Timeout end
with_session(&block)
click to toggle source
# File lib/bunq/resource.rb, line 62 def with_session(&block) client.with_session(&block) end
Private Instance Methods
bunq_request_headers(verb, params, payload = nil, headers = {})
click to toggle source
# File lib/bunq/resource.rb, line 85 def bunq_request_headers(verb, params, payload = nil, headers = {}) headers[Bunq::Header::CLIENT_REQUEST_ID] = SecureRandom.uuid unless @path.end_with?('/installation') && verb == 'POST' headers[Bunq::Header::CLIENT_SIGNATURE] = sign_request(verb, params, headers, payload) end headers end
encode_params(path, params)
click to toggle source
# File lib/bunq/resource.rb, line 99 def encode_params(path, params) return path if params.empty? "#{path}?#{URI.escape(params.collect { |k, v| "#{k}=#{v}" }.join('&'))}" end
handle_response(response, _request, _result) { |response| ... }
click to toggle source
# File lib/bunq/resource.rb, line 117 def handle_response(response, _request, _result) if response.code == 200 || response.code == 201 if block_given? yield(response) else JSON.parse(response.body) end elsif (response.code == 409 && Bunq.configuration.sandbox) || response.code == 429 fail TooManyRequestsResponse.new(code: response.code, headers: response.raw_headers, body: response.body) elsif [401, 403].include?(response.code) fail UnauthorisedResponse.new(code: response.code, headers: response.raw_headers, body: response.body) elsif response.code == 404 fail ResourceNotFound.new(code: response.code, headers: response.raw_headers, body: response.body) elsif [491, 503].include?(response.code) fail MaintenanceResponse.new(code: response.code, headers: response.raw_headers, body: response.body) else fail UnexpectedResponse.new(code: response.code, headers: response.raw_headers, body: response.body) end end
post_body(payload, custom_headers)
click to toggle source
# File lib/bunq/resource.rb, line 137 def post_body(payload, custom_headers) if custom_headers.key?(Bunq::Header::CONTENT_TYPE) && custom_headers[Bunq::Header::CONTENT_TYPE] != APPLICATION_JSON payload else JSON.generate(payload) end end
resource()
click to toggle source
# File lib/bunq/resource.rb, line 70 def resource RestClient::Resource.new( "#{client.configuration.base_url}#{path}", { headers: client.headers, timeout: client.configuration.timeout, }.tap do |x| if client.configuration.sandbox x[:user] = client.configuration.sandbox_user x[:password] = client.configuration.sandbox_password end end, ) end
sign_request(_verb, _params, _headers, payload = nil)
click to toggle source
# File lib/bunq/resource.rb, line 95 def sign_request(_verb, _params, _headers, payload = nil) client.signature.create(payload) end
verify_and_handle_response(response, request, result, &block)
click to toggle source
# File lib/bunq/resource.rb, line 105 def verify_and_handle_response(response, request, result, &block) client.signature.verify!(response) if verify_response_signature?(response) handle_response(response, request, result, &block) end
verify_response_signature?(response)
click to toggle source
# File lib/bunq/resource.rb, line 110 def verify_response_signature?(response) return false if client.configuration.disable_response_signature_verification return false if response.code == 491 (100..499).include?(response.code) end