From 7c5d11b78754a00d3c9fa1ed6d47085c1c40aaf6 Mon Sep 17 00:00:00 2001 From: Pat Allan Date: Fri, 30 Oct 2015 14:52:09 +0100 Subject: [PATCH] Respect client-specific options over global settings. Particularly useful when different client objects may be used with different credentials (thus, global defaults don't make sense). Seems this worked in 0.4.1, but has broken in more recent commits. --- lib/validic.rb | 9 ++++++++- lib/validic/client.rb | 15 ++++----------- lib/validic/rest/request.rb | 27 ++++++++++++++------------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/lib/validic.rb b/lib/validic.rb index 6a572dd..c4145a5 100644 --- a/lib/validic.rb +++ b/lib/validic.rb @@ -1,7 +1,10 @@ require 'validic/client' module Validic - BASE_URL = "https://api.validic.com/v1" + API_URL = 'https://api.validic.com' + API_VERSION = 'v1' + BASE_URL = "#{API_URL}/#{API_VERSION}" + class << self attr_accessor :api_url, :api_version, @@ -18,3 +21,7 @@ def configure end end end + +Validic.api_url = Validic::API_URL +Validic.api_version = Validic::API_VERSION + diff --git a/lib/validic/client.rb b/lib/validic/client.rb index 3964243..96ae65f 100644 --- a/lib/validic/client.rb +++ b/lib/validic/client.rb @@ -44,10 +44,10 @@ class Client # # @params options[Hash] def initialize(options={}) - @api_url = options.fetch(:api_url, 'https://api.validic.com') - @api_version = options.fetch(:api_version, 'v1') - @access_token = options.fetch(:access_token, Validic.access_token) - @organization_id = options.fetch(:organization_id, Validic.organization_id) + @api_url = options[:api_url] || Validic.api_url + @api_version = options[:api_version] || Validic.api_version + @access_token = options[:access_token] || Validic.access_token + @organization_id = options[:organization_id] || Validic.organization_id end ## @@ -72,12 +72,5 @@ def default_headers user_agent: "Ruby Gem by Validic #{Validic::VERSION}" } end - - def reload_config - Validic.api_url = api_url - Validic.api_version = api_version - Validic.access_token = access_token - Validic.organization_id = organization_id - end end end diff --git a/lib/validic/rest/request.rb b/lib/validic/rest/request.rb index b56a415..8d5b4f2 100644 --- a/lib/validic/rest/request.rb +++ b/lib/validic/rest/request.rb @@ -5,12 +5,12 @@ module Validic module REST module Request def latest(type, options = {}) - organization_id = options[:organization_id] || Validic.organization_id + local_organization_id = options[:organization_id] || organization_id user_id = options.delete(:user_id) if user_id - path = "/#{Validic.api_version}/organizations/#{organization_id}/users/#{user_id}/#{type.to_s}/latest.json" + path = "/#{api_version}/organizations/#{local_organization_id}/users/#{user_id}/#{type.to_s}/latest.json" else - path = "/#{Validic.api_version}/organizations/#{organization_id}/#{type.to_s}/latest.json" + path = "/#{api_version}/organizations/#{local_organization_id}/#{type.to_s}/latest.json" end get(path, options) end @@ -38,25 +38,26 @@ def delete_request(type, options = {}) end def construct_path(type, options) - organization_id = options.delete(:organization_id) || Validic.organization_id + local_organization_id = options.delete(:organization_id) || + organization_id user_id = options.delete(:user_id) activity_id = options.delete(:_id) + if activity_id - path = "/#{Validic.api_version}/organizations/#{organization_id}/users/#{user_id}/#{type.to_s}/#{activity_id}.json" + "/#{api_version}/organizations/#{local_organization_id}/users/#{user_id}/#{type.to_s}/#{activity_id}.json" elsif user_id && type == :users - path = "/#{Validic.api_version}/organizations/#{organization_id}/users/#{user_id}.json" + "/#{api_version}/organizations/#{local_organization_id}/users/#{user_id}.json" elsif user_id - path = "/#{Validic.api_version}/organizations/#{organization_id}/users/#{user_id}/#{type.to_s}.json" + "/#{api_version}/organizations/#{local_organization_id}/users/#{user_id}/#{type.to_s}.json" elsif type == :me - path = "/#{Validic.api_version}/me.json" + "/#{api_version}/me.json" elsif type == :profile - path = "/#{Validic.api_version}/profile.json" + "/#{api_version}/profile.json" elsif type == :organizations - path = "/#{Validic.api_version}/organizations/#{organization_id}.json" + "/#{api_version}/organizations/#{local_organization_id}.json" else - path = "/#{Validic.api_version}/organizations/#{organization_id}/#{type.to_s}.json" + "/#{api_version}/organizations/#{local_organization_id}/#{type.to_s}.json" end - path end def get(path, options) @@ -76,7 +77,7 @@ def delete(path, options) end def request(method, path, options) - options[:access_token] = options[:access_token].nil? ? Validic.access_token : options[:access_token] + options[:access_token] ||= access_token response = connection.send(method) do |request| case method when :get