Skip to content

Commit cf45ef5

Browse files
authored
Merge pull request #8 from contentstack/develop
Region support and remove timestamp for cdn request
2 parents 4d1ddb2 + b2865a1 commit cf45ef5

5 files changed

Lines changed: 30 additions & 6 deletions

File tree

lib/contentstack.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "contentstack/version"
44
require "contentstack/client"
5+
require "contentstack/region"
56
require "util"
67

78

@@ -11,6 +12,10 @@
1112
# gem install contentstack
1213
# == Initialize the Stack
1314
# @stack = Contentstack::Client.new("site_api_key", "access_token", "enviroment_name")
15+
# == Initialize the Stack for EU region
16+
# @stack = Contentstack::Client.new("site_api_key", "access_token", "enviroment_name", {"region": Contentstack::Region::EU })
17+
# == Initialize the Stack for custom host
18+
# @stack = Contentstack::Client.new("site_api_key", "access_token", "enviroment_name", {"host": "https://custom-cdn.contentstack.com" })
1419
# == Usage
1520
# ==== Get single entry
1621
# @stack.content_type('blog').entry('<entry_uid_here>').fetch

lib/contentstack/api.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
module Contentstack
88
class API
9-
def self.init_api(api_key, access_token, environment)
10-
@host = "https://cdn.contentstack.io"
9+
def self.init_api(api_key, access_token, environment,host)
10+
@host = host
1111
@api_version = '/v3'
1212
@environment = environment
1313
@headers = {api_key: api_key, access_token: access_token, user_agent: "ruby-sdk/#{Contentstack::VERSION}", environment: @environment}
@@ -43,7 +43,6 @@ def self.send_request(path, q=nil)
4343
q ||= {}
4444

4545
q.merge!(@headers)
46-
q.merge!({timestamp: Time.now.to_i})
4746

4847
query = "?" + q.to_query
4948
# puts "Request URL:- #{@host}#{@api_version}#{path}#{query} \n\n"

lib/contentstack/client.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
module Contentstack
66
class Client
7+
attr_reader :region, :host
78
# Initialize "Built.io Contentstack" Client instance
8-
def initialize(api_key, access_token, environment)
9-
API.init_api(api_key, access_token, environment)
9+
def initialize(api_key, access_token, environment, options={})
10+
@region = options[:region].nil? ? Contentstack::Region::US : options[:region]
11+
@host = options[:host].nil? ? get_default_region_hosts(@region) : options[:host]
12+
API.init_api(api_key, access_token, environment, @host)
1013
end
1114

1215

@@ -25,5 +28,16 @@ def assets
2528
def asset(uid)
2629
Asset.new(uid)
2730
end
31+
32+
private
33+
def get_default_region_hosts(region='us')
34+
case region
35+
when "us"
36+
host = "https://cdn.contentstack.io"
37+
when "eu"
38+
host = "https://eu-cdn.contentstack.com"
39+
end
40+
host
41+
end
2842
end
2943
end

lib/contentstack/region.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Contentstack
2+
class Region
3+
EU='eu'
4+
US='us'
5+
end
6+
end

lib/contentstack/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Contentstack
2-
VERSION = "0.0.3"
2+
VERSION = "0.0.4"
33
end

0 commit comments

Comments
 (0)