Module: Gibbon::Helpers

Included in:
APIRequest
Defined in:
lib/gibbon/gibbon_helpers.rb

Instance Method Summary collapse

Instance Method Details

#get_data_center_from_api_key(api_key) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/gibbon/gibbon_helpers.rb', line 3

def get_data_center_from_api_key(api_key)
  # Return an empty string for invalid API keys so Gibbon hits the main endpoint
  data_center = ""

  if api_key && api_key["-"]
    # Remove all non-alphanumberic characters in case someone attempts to inject
    # a different domain into the API key (e.g. when consuming user form-provided keys)
    # This approach avoids assuming a 3 letter prefix (e.g. is MC were to create 
    # a us10 DC, this would continue to work), and will continue to hit MC's server
    # rather than a would-be attacker's servers.
    data_center = "#{api_key.split('-').last.gsub(/[^0-9a-z ]/i, '')}."
  end

  data_center
end