Skip to content

Commit 4475b7a

Browse files
Added new method for custom host support with different regions
1 parent 7e82ff6 commit 4475b7a

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

lib/contentstack/client.rb

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def initialize(api_key, delivery_token, environment, options={})
1717
raise Contentstack::Error.new("Envirnoment Field is not valid") if environment.class != String
1818
raise Contentstack::Error.new("Envirnoment Field Should not be Empty") if environment.empty?
1919
@region = options[:region].nil? ? Contentstack::Region::US : options[:region]
20-
@host = options[:host].nil? ? get_default_region_hosts(@region) : options[:host]
20+
# @host = options[:host].nil? ? get_default_region_hosts(@region) : options[:host] #removed for not supporting custom host with regions
21+
@host = get_host_by_region(@region, options) # Added new method for custom host support with different regions
2122
@live_preview = !options.key?(:live_preview) ? {} : options[:live_preview]
2223
@branch = options[:branch].nil? ? "" : options[:branch]
2324
@proxy_details = options[:proxy].nil? ? "" : options[:proxy]
@@ -80,7 +81,7 @@ def sync(params)
8081

8182
private
8283
def get_default_region_hosts(region='us')
83-
host = "https://cdn.contentstack.io"
84+
host = "https://cdn.contentstack.io" #set default host if region is nil
8485
case region
8586
when "us"
8687
host = "https://cdn.contentstack.io"
@@ -93,5 +94,30 @@ def get_default_region_hosts(region='us')
9394
end
9495
host
9596
end
97+
98+
def get_host_by_region(region, options)
99+
if options[:host].nil? && region.present?
100+
host = get_default_region_hosts(region)
101+
elsif options[:host].present? && region.present?
102+
custom_host = options[:host]
103+
case region
104+
when "us"
105+
host = "https://cdn.#{custom_host}"
106+
when "eu"
107+
host = "https://eu-cdn.#{custom_host}"
108+
when "azure-na"
109+
host = "https://azure-na-cdn.#{custom_host}"
110+
when "azure-eu"
111+
host = "https://azure-eu-cdn.#{custom_host}"
112+
end
113+
elsif options[:host].present? && region.empty?
114+
custom_host = options[:host]
115+
host = "https://cdn.#{custom_host}"
116+
else
117+
host = "https://cdn.contentstack.io" #set default host if region and host is empty
118+
end
119+
host
120+
end
121+
96122
end
97123
end

0 commit comments

Comments
 (0)