|
| 1 | +require 'json' |
| 2 | +require 'net/http' |
| 3 | +require 'uri' |
| 4 | +require 'fileutils' |
| 5 | +require 'contentstack/error' |
| 6 | + |
| 7 | +module Contentstack |
| 8 | + # Centralised endpoint resolver. Reads region→service→URL mappings from the |
| 9 | + # bundled regions.json and falls back to the live registry when the file is |
| 10 | + # absent. Delegating to ContentstackUtils.get_contentstack_endpoint is |
| 11 | + # preferred when that gem exposes the method (PR #41 of contentstack-utils-ruby). |
| 12 | + class Endpoint |
| 13 | + REGISTRY_URL = 'https://raw.githubusercontent.com/contentstack/contentstack-utils-ruby/main/lib/data/regions.json' |
| 14 | + DATA_FILE_PATH = File.join(File.dirname(File.dirname(__FILE__)), 'data', 'regions.json') |
| 15 | + |
| 16 | + DEFAULT_SERVICE = 'cda' |
| 17 | + |
| 18 | + # Resolve a Contentstack service URL for the given region and service. |
| 19 | + # |
| 20 | + # Contentstack::Endpoint.get_contentstack_endpoint('eu') |
| 21 | + # # => "https://eu-cdn.contentstack.com" |
| 22 | + # |
| 23 | + # Contentstack::Endpoint.get_contentstack_endpoint('us', 'cma') |
| 24 | + # # => "https://api.contentstack.io" |
| 25 | + # |
| 26 | + # When +custom_host+ is supplied the region CDN prefix is derived from |
| 27 | + # regions.json and prepended to the custom domain, preserving the |
| 28 | + # existing SDK behaviour for host-override configurations. |
| 29 | + def self.get_contentstack_endpoint(region, service = DEFAULT_SERVICE, custom_host = nil) |
| 30 | + region_key = region.to_s.downcase |
| 31 | + service_key = service.to_s.downcase |
| 32 | + |
| 33 | + if custom_host.nil? || custom_host.to_s.empty? |
| 34 | + # Prefer the utils SDK when it ships endpoint resolution (PR #41) |
| 35 | + if defined?(ContentstackUtils) && ContentstackUtils.respond_to?(:get_contentstack_endpoint) |
| 36 | + return ContentstackUtils.get_contentstack_endpoint(region_key, service_key) |
| 37 | + end |
| 38 | + resolve_standard(region_key, service_key) |
| 39 | + else |
| 40 | + resolve_custom_host(region_key, service_key, custom_host) |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + # Download and persist the latest region metadata from the registry. |
| 45 | + # Equivalent to `composer refresh-regions` in the PHP SDK. |
| 46 | + # |
| 47 | + # Rake: bundle exec rake refresh_regions |
| 48 | + def self.refresh_regions |
| 49 | + data = fetch_from_registry |
| 50 | + FileUtils.mkdir_p(File.dirname(DATA_FILE_PATH)) |
| 51 | + File.write(DATA_FILE_PATH, JSON.pretty_generate(data)) |
| 52 | + data |
| 53 | + end |
| 54 | + |
| 55 | + private |
| 56 | + |
| 57 | + def self.resolve_standard(region_key, service_key) |
| 58 | + regions = load_regions |
| 59 | + unless regions.key?(region_key) |
| 60 | + raise Contentstack::Error.new( |
| 61 | + Contentstack::ErrorMessages.region_invalid(region_key, regions.keys) |
| 62 | + ) |
| 63 | + end |
| 64 | + unless regions[region_key].key?(service_key) |
| 65 | + raise Contentstack::Error.new( |
| 66 | + Contentstack::ErrorMessages.service_invalid(service_key, regions[region_key].keys) |
| 67 | + ) |
| 68 | + end |
| 69 | + regions[region_key][service_key] |
| 70 | + end |
| 71 | + |
| 72 | + # Derive the CDN subdomain prefix from regions.json and combine with the |
| 73 | + # caller-supplied custom domain, e.g. "eu-cdn" + "example.com" → |
| 74 | + # "https://eu-cdn.example.com". |
| 75 | + def self.resolve_custom_host(region_key, service_key, custom_host) |
| 76 | + regions = load_regions |
| 77 | + if regions.key?(region_key) && regions[region_key].key?(service_key) |
| 78 | + standard_url = regions[region_key][service_key] |
| 79 | + prefix = URI.parse(standard_url).host.split('.').first |
| 80 | + "https://#{prefix}.#{custom_host}" |
| 81 | + else |
| 82 | + "https://cdn.#{custom_host}" |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + def self.load_regions |
| 87 | + if File.exist?(DATA_FILE_PATH) |
| 88 | + JSON.parse(File.read(DATA_FILE_PATH)) |
| 89 | + else |
| 90 | + warn '[Contentstack] regions.json not found locally — fetching from registry...' |
| 91 | + data = fetch_from_registry |
| 92 | + begin |
| 93 | + FileUtils.mkdir_p(File.dirname(DATA_FILE_PATH)) |
| 94 | + File.write(DATA_FILE_PATH, JSON.pretty_generate(data)) |
| 95 | + rescue => e |
| 96 | + warn "[Contentstack] Could not cache regions.json: #{e.message}" |
| 97 | + end |
| 98 | + data |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + def self.fetch_from_registry |
| 103 | + uri = URI.parse(REGISTRY_URL) |
| 104 | + response = Net::HTTP.get_response(uri) |
| 105 | + unless response.is_a?(Net::HTTPSuccess) |
| 106 | + raise Contentstack::Error.new( |
| 107 | + "Failed to fetch region metadata from registry (HTTP #{response.code}). " \ |
| 108 | + 'Ensure network access and try again.' |
| 109 | + ) |
| 110 | + end |
| 111 | + JSON.parse(response.body) |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments