Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config to get hashes' keys in snake_case #238

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions lib/mangopay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'benchmark'
require 'logger'
require 'time'
require 'active_support/core_ext/hash'

# helpers
require 'mangopay/version'
Expand Down Expand Up @@ -54,7 +55,7 @@ class Configuration
:client_id, :client_apiKey,
:temp_dir, :log_file, :http_timeout,
:http_max_retries, :http_open_timeout,
:logger, :use_ssl
:logger, :use_ssl, :snakify_response_keys

def preproduction
@preproduction || false
Expand Down Expand Up @@ -83,6 +84,10 @@ def use_ssl?

true
end

def snakify_response_keys?
@snakify_response_keys || false
end
end

class << self
Expand Down Expand Up @@ -111,9 +116,9 @@ def configuration
config = Thread.current[:mangopay_configuration]

config ||
( @last_configuration_set &&
(self.configuration = @last_configuration_set.dup)) ||
(self.configuration = MangoPay::Configuration.new)
( @last_configuration_set &&
(self.configuration = @last_configuration_set.dup)) ||
(self.configuration = MangoPay::Configuration.new)
end

def configure
Expand Down Expand Up @@ -190,7 +195,11 @@ def request(method, url, params={}, filters={}, headers_or_idempotency_key = nil
}
end

data
if configuration.snakify_response_keys?
transform_keys_to_snake_case(data)
else
data
end
end

# Retrieve a previous response by idempotency_key
Expand All @@ -200,15 +209,18 @@ def fetch_response(idempotency_key)
request(:get, url)
end

def camelize_keys(hash)
transform_keys_to_camel_case(hash)
end
private

def user_agent
{
bindings_version: VERSION,
lang: 'ruby',
lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
platform: RUBY_PLATFORM,
uname: get_uname
bindings_version: VERSION,
lang: 'ruby',
lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
platform: RUBY_PLATFORM,
uname: get_uname
}
end

Expand All @@ -221,9 +233,9 @@ def get_uname
def request_headers
auth_token = AuthorizationToken::Manager.get_token
headers = {
'User-Agent' => "MangoPay V2 SDK Ruby Bindings #{VERSION}",
'Authorization' => "#{auth_token['token_type']} #{auth_token['access_token']}",
'Content-Type' => 'application/json'
'User-Agent' => "MangoPay V2 SDK Ruby Bindings #{VERSION}",
'Authorization' => "#{auth_token['token_type']} #{auth_token['access_token']}",
'Content-Type' => 'application/json'
}
begin
headers.update('x_mangopay_client_user_agent' => JSON.dump(user_agent))
Expand Down Expand Up @@ -294,5 +306,28 @@ def logger
def logs_required?
!configuration.log_file.nil? || !configuration.logger.nil?
end

def transform_keys_to_snake_case(response)
_deep_transform_keys_in_object!(response, &:underscore)
end

def transform_keys_to_camel_case(hash)
_deep_transform_keys_in_object!(hash, &:camelize)
end

def _deep_transform_keys_in_object!(object, &block)
case object
when Hash
object.keys.each do |key|
value = object.delete(key)
object[yield(key)] = _deep_transform_keys_in_object!(value, &block)
end
object
when Array
object.map! { |e| _deep_transform_keys_in_object!(e, &block) }
else
object
end
end
end
end
2 changes: 1 addition & 1 deletion lib/mangopay/authorization_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get
f.flock(File::LOCK_SH)
txt = f.read
f.close
YAML.load(txt) || nil
YAML.safe_load(txt, permitted_classes: [Time]) || nil
rescue Errno::ENOENT
nil
end
Expand Down
15 changes: 11 additions & 4 deletions lib/mangopay/ubo_declaration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@ def fetch(user_id, id, idempotency_key = nil)
end

def update(user_id, id, params = {}, idempotency_key = nil)
request_params = {
Status: params['Status'],
Ubos: params['Ubos']
}
request_params = if MangoPay.configuration.snakify_response_keys?
{
Status: params['status'],
Ubos: params['ubos']
}
else
{
Status: params['Status'],
Ubos: params['Ubos']
}
end
MangoPay.request(:put, url(user_id, id), request_params, {}, idempotency_key)
end
end
Expand Down
3 changes: 2 additions & 1 deletion mangopay.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ Gem::Specification.new do |s|
s.homepage = 'http://docs.mangopay.com/'
s.license = 'MIT'

s.required_ruby_version = '>= 1.9.2'
s.required_ruby_version = '>= 2.2.2'

s.add_dependency('multi_json', '>= 1.7.7')
s.add_dependency('activesupport', '>= 5.0')

s.add_development_dependency('rake', '>= 10.1.0')
s.add_development_dependency('rspec', '>= 3.0.0')
Expand Down
1 change: 0 additions & 1 deletion spec/mangopay/bank_account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def create(params)
expect(created['AccountNumber']).to eq('234234234234')
expect(created['BIC']).to eq('BINAADADXXX')
end

end

describe 'FETCH' do
Expand Down
9 changes: 9 additions & 0 deletions spec/mangopay/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@
end
end

describe '.snakify_response_keys??' do
let(:configuration) { MangoPay::Configuration.new }

it 'defaults to false' do
expect(configuration.snakify_response_keys?).to eq(false)
end
end


describe 'logger' do
around(:each) do |example|
c = MangoPay.configuration
Expand Down
28 changes: 28 additions & 0 deletions spec/mangopay/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,33 @@
expect(events.count).to be >= 1
expect(events.count {|e| e['ResourceId'] == resourceId}).to be >= 1
end

context 'when snakify_response_keys is true' do
include_context 'snakify_response_keys'
it 'accepts filtering params' do

# let's have at least 2 events
payin = new_payin_card_direct2
create_new_payout_bankwire2(payin)

# get all #
events = MangoPay::Event.fetch
expect(events).to be_kind_of(Array)
expect(events.count).to be >= 2

# only one per page #
events = MangoPay::Event.fetch({'per_page' => 1})
expect(events).to be_kind_of(Array)
expect(events.count).to eq 1

# filter by date
eventDate = events[0]['date']
resource_id = events[0]['resource_id']
events = MangoPay::Event.fetch({'AfterDate' => eventDate - 1, 'BeforeDate' => eventDate + 1}) # this is equivalent to: events = MangoPay::Event.fetch(MangoPay.camelize_keys('after_date' => eventDate - 1, 'before_date' => eventDate + 1))
expect(events).to be_kind_of(Array)
expect(events.count).to be >= 1
expect(events.count {|e| e['resource_id'] == resource_id}).to be >= 1
end
end
end
end
38 changes: 38 additions & 0 deletions spec/mangopay/kyc_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
expect(new_document['RefusedReasonMessage']).to be_nil
expect(new_document['Flags']).to match_array([])
end

context 'when snakify_response_keys is true' do
include_context 'snakify_response_keys'
it 'creates a document' do
expect(new_document2['id']).to_not be_nil
expect(new_document2['type']).to eq('IDENTITY_PROOF')
expect(new_document2['status']).to eq('CREATED')
expect(new_document2['refused_reason_type']).to be_nil
expect(new_document2['refused_reason_message']).to be_nil
expect(new_document2['flags']).to match_array([])
end
end
end

describe 'UPDATE' do
Expand All @@ -23,6 +35,20 @@
expect(updated_document['Id']).to eq(new_document['Id'])
expect(updated_document['Status']).to eq('VALIDATION_ASKED')
end

context 'when snakify_response_keys is true' do
include_context 'snakify_response_keys'
it 'updates a document' do
fnm = __FILE__.sub('.rb', '.png')
MangoPay::KycDocument.create_page(new_natural_user['id'], new_document2['id'], nil, fnm)

updated_document = MangoPay::KycDocument.update(new_natural_user['id'], new_document2['id'], {
Status: 'VALIDATION_ASKED'
})
expect(updated_document['id']).to eq(new_document2['id'])
expect(updated_document['status']).to eq('VALIDATION_ASKED')
end
end
end

describe 'FETCH' do
Expand Down Expand Up @@ -113,6 +139,18 @@ def create_page(file)
expect(consult).not_to be_nil
expect(consult).to be_kind_of(Array)
end

context 'when snakify_response_keys is true' do
include_context 'snakify_response_keys'
it 'creates document pages consult' do
fnm = __FILE__.sub('.rb', '.png')
MangoPay::KycDocument.create_page(new_natural_user['id'], new_document2['id'], nil, fnm)

consult = MangoPay::KycDocument.create_documents_consult(new_document2['id'])
expect(consult).not_to be_nil
expect(consult).to be_kind_of(Array)
end
end
end
end
end
Loading