Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 34 additions & 27 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,45 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activesupport (7.1.3.2)
activesupport (8.1.1)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
concurrent-ruby (~> 1.0, >= 1.3.1)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
json
logger (>= 1.4.2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
base64 (0.2.0)
bigdecimal (3.1.8)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
contentstack_utils (1.2.0)
activesupport (>= 3.2)
nokogiri (~> 1.11)
crack (1.0.0)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
uri (>= 0.13.1)
addressable (2.8.8)
public_suffix (>= 2.0.2, < 8.0)
base64 (0.3.0)
bigdecimal (3.3.1)
concurrent-ruby (1.3.6)
connection_pool (3.0.2)
contentstack_utils (1.2.1)
activesupport (>= 7.0)
nokogiri (>= 1.11)
crack (1.0.1)
bigdecimal
rexml
diff-lcs (1.5.1)
docile (1.4.0)
drb (2.2.1)
hashdiff (1.1.0)
i18n (1.14.5)
diff-lcs (1.6.2)
docile (1.4.1)
drb (2.2.3)
hashdiff (1.2.1)
i18n (1.14.7)
concurrent-ruby (~> 1.0)
minitest (5.22.3)
mutex_m (0.2.0)
nokogiri (1.15.6-arm64-darwin)
json (2.18.0)
logger (1.7.0)
minitest (5.27.0)
nokogiri (1.18.10-arm64-darwin)
racc (~> 1.4)
public_suffix (5.0.5)
racc (1.7.3)
rexml (3.2.6)
public_suffix (7.0.0)
racc (1.8.1)
rexml (3.4.4)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
Expand All @@ -56,22 +60,25 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.3)
securerandom (0.4.1)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov-html (0.13.2)
simplecov_json_formatter (0.1.4)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
uri (1.1.1)
webmock (3.11.3)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
yard (0.9.36)
yard (0.9.38)

PLATFORMS
arm64-darwin-22
arm64-darwin-24

DEPENDENCIES
contentstack!
Expand Down
6 changes: 3 additions & 3 deletions lib/contentstack/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def self.fetch_retry(path, query=nil, count=0)
sleep(retryDelay_in_seconds.to_i) #sleep method requires time in seconds as parameter
response = fetch_retry(path, query, (count + 1))
else
raise Contentstack::Error.new(response) #Retry Limit exceeded
raise Contentstack::Error.new(Contentstack::ErrorMessages.request_failed(response)) #Retry Limit exceeded
end
else
to_render_content(response)
Expand Down Expand Up @@ -125,7 +125,7 @@ def self.send_request(path, q=nil)
error_response = JSON.parse(response.string)
error_status = {"status_code" => response.status[0], "status_message" => response.status[1]}
error = error_response.merge(error_status)
raise Contentstack::Error.new(error.to_s)
raise Contentstack::Error.new(Contentstack::ErrorMessages.request_error(error))
end
end

Expand Down Expand Up @@ -165,7 +165,7 @@ def self.send_preview_request(path, q=nil)
error_response = JSON.parse(response.string)
error_status = {"status_code" => response.status[0], "status_message" => response.status[1]}
error = error_response.merge(error_status)
raise Contentstack::Error.new(error.to_s)
raise Contentstack::Error.new(Contentstack::ErrorMessages.request_error(error))
end
end

Expand Down
16 changes: 8 additions & 8 deletions lib/contentstack/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class Client
attr_reader :region, :host
# Initialize "Contentstack" Client instance
def initialize(api_key, delivery_token, environment, options={})
raise Contentstack::Error.new("Api Key is not valid") if api_key.class != String
raise Contentstack::Error.new("Api Key Field Should not be Empty") if api_key.empty?
raise Contentstack::Error.new("Delivery Token is not valid") if delivery_token.class != String
raise Contentstack::Error.new("Delivery Token Field Should not be Empty") if delivery_token.empty?
raise Contentstack::Error.new("Envirnoment Field is not valid") if environment.class != String
raise Contentstack::Error.new("Envirnoment Field Should not be Empty") if environment.empty?
raise Contentstack::Error.new(Contentstack::ErrorMessages::API_KEY_INVALID) if api_key.class != String
raise Contentstack::Error.new(Contentstack::ErrorMessages::API_KEY_REQUIRED) if api_key.empty?
raise Contentstack::Error.new(Contentstack::ErrorMessages::DELIVERY_TOKEN_INVALID) if delivery_token.class != String
raise Contentstack::Error.new(Contentstack::ErrorMessages::DELIVERY_TOKEN_REQUIRED) if delivery_token.empty?
raise Contentstack::Error.new(Contentstack::ErrorMessages::ENVIRONMENT_INVALID) if environment.class != String
raise Contentstack::Error.new(Contentstack::ErrorMessages::ENVIRONMENT_REQUIRED) if environment.empty?
@region = options[:region].nil? ? Contentstack::Region::US : options[:region]
# @host = options[:host].nil? ? get_default_region_hosts(@region) : options[:host] #removed for not supporting custom host with regions
@host = get_host_by_region(@region, options) # Added new method for custom host support with different regions
Expand All @@ -32,8 +32,8 @@ def initialize(api_key, delivery_token, environment, options={})
"retryLimit"=> @retryLimit,
"errorRetry" => @errorRetry
}
raise Contentstack::Error.new("Proxy URL Should not be Empty") if @proxy_details.present? && @proxy_details[:url].empty?
raise Contentstack::Error.new("Proxy Port Should not be Empty") if @proxy_details.present? && @proxy_details[:port].empty?
raise Contentstack::Error.new(Contentstack::ErrorMessages::PROXY_URL_REQUIRED) if @proxy_details.present? && @proxy_details[:url].empty?
raise Contentstack::Error.new(Contentstack::ErrorMessages::PROXY_PORT_REQUIRED) if @proxy_details.present? && @proxy_details[:port].empty?
API.init_api(api_key, delivery_token, environment, @host, @branch, @live_preview, @proxy_details, retry_options)
end

Expand Down
20 changes: 20 additions & 0 deletions lib/contentstack/error.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
module Contentstack
# Centralized error messages for the SDK
module ErrorMessages
API_KEY_INVALID = "API Key is invalid. Provide a valid API Key and try again."
API_KEY_REQUIRED = "API Key is required. Provide a valid API Key and try again."
DELIVERY_TOKEN_INVALID = "Delivery Token is invalid. Provide a valid Delivery Token and try again."
DELIVERY_TOKEN_REQUIRED = "Delivery Token is required. Provide a valid Delivery Token and try again."
ENVIRONMENT_INVALID = "Environment is invalid. Provide a valid Environment and try again."
ENVIRONMENT_REQUIRED = "Environment is required. Provide a valid Environment and try again."
PROXY_URL_REQUIRED = "Proxy URL is required. Provide a valid Proxy URL and try again."
PROXY_PORT_REQUIRED = "Proxy Port is required. Provide a valid Proxy Port and try again."

def self.request_failed(response)
"The request could not be completed due to #{response}. Review the details and try again."
end

def self.request_error(error)
"The request encountered an issue due to #{error}. Review the details and try again."
end
end

class Error < StandardError
def initialize(msg="Something Went Wrong.")
super
Expand Down
Loading