Module: ContentstackUtils::Endpoint

Defined in:
lib/contentstack_utils/endpoint.rb

Constant Summary collapse

REGIONS_URL =
'https://artifacts.contentstack.com/regions.json'
REGIONS_FILE =
File.expand_path('../assets/regions.json', __FILE__)

Class Method Summary collapse

Class Method Details

.get_contentstack_endpoint(region: 'us', service: '', omit_https: false) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/contentstack_utils/endpoint.rb', line 13

def get_contentstack_endpoint(region: 'us', service: '', omit_https: false)
  raise ArgumentError, 'Empty region provided' if region.nil? || region.to_s.strip.empty?

  normalized = region.to_s.strip.downcase
  regions = load_regions

  region_row = find_region_by_id_or_alias(regions, normalized)
  raise ArgumentError, "Invalid region: #{region}" if region_row.nil?

  endpoints = region_row['endpoints']

  if service.nil? || service.to_s.strip.empty?
    return omit_https ? strip_https_from_map(endpoints) : endpoints.dup
  end

  url = endpoints[service.to_s]
  raise ArgumentError, "Service \"#{service}\" not found for region \"#{region}\"" if url.nil?

  omit_https ? strip_https(url) : url
end

.refresh_regionsObject



34
35
36
37
38
39
# File 'lib/contentstack_utils/endpoint.rb', line 34

def refresh_regions
  download_and_save(REGIONS_FILE)
  @regions_data = nil
  load_regions
  true
end

.reset_cacheObject



41
42
43
# File 'lib/contentstack_utils/endpoint.rb', line 41

def reset_cache
  @regions_data = nil
end