contentstack 0.6.3.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/check-branch.yml +20 -0
- data/.github/workflows/codeql-analysis.yml +68 -68
- data/.github/workflows/jira.yml +28 -28
- data/.github/workflows/release-gem.yml +30 -30
- data/.github/workflows/sast-scan.yml +10 -10
- data/.github/workflows/sca-scan.yml +15 -15
- data/.github/workflows/secrets-scan.yml +10 -10
- data/.gitignore +11 -11
- data/.yardopts +6 -6
- data/CHANGELOG.md +129 -114
- data/CODEOWNERS +1 -1
- data/CODE_OF_CONDUCT.md +73 -73
- data/Gemfile +2 -2
- data/Gemfile.lock +79 -76
- data/LICENSE.txt +21 -21
- data/README.md +197 -197
- data/SECURITY.md +27 -27
- data/contentstack.gemspec +29 -29
- data/lib/contentstack/api.rb +191 -191
- data/lib/contentstack/asset.rb +68 -68
- data/lib/contentstack/asset_collection.rb +27 -27
- data/lib/contentstack/client.rb +122 -91
- data/lib/contentstack/content_type.rb +53 -53
- data/lib/contentstack/entry.rb +235 -221
- data/lib/contentstack/entry_collection.rb +44 -44
- data/lib/contentstack/error.rb +6 -6
- data/lib/contentstack/query.rb +665 -653
- data/lib/contentstack/region.rb +13 -5
- data/lib/contentstack/sync_result.rb +29 -29
- data/lib/contentstack/version.rb +2 -2
- data/lib/contentstack.rb +31 -31
- data/lib/util.rb +110 -110
- data/rakefile.rb +3 -3
- data/spec/asset_collection_spec.rb +15 -15
- data/spec/asset_spec.rb +47 -47
- data/spec/content_type_spec.rb +80 -80
- data/spec/contentstack_spec.rb +63 -38
- data/spec/entry_collection_spec.rb +41 -41
- data/spec/entry_spec.rb +116 -101
- data/spec/fixtures/asset.json +1 -1
- data/spec/fixtures/asset_collection.json +1 -1
- data/spec/fixtures/category_content_type.json +1 -1
- data/spec/fixtures/category_entry.json +1 -1
- data/spec/fixtures/category_entry_collection.json +1 -1
- data/spec/fixtures/category_entry_collection_without_count.json +1 -1
- data/spec/fixtures/content_types.json +1 -1
- data/spec/fixtures/product_entry.json +1 -1
- data/spec/fixtures/product_entry_collection.json +1 -1
- data/spec/fixtures/sync_init.json +2974 -2974
- data/spec/query_spec.rb +210 -205
- data/spec/spec_helper.rb +180 -180
- data/spec/sync_spec.rb +26 -26
- metadata +4 -3
data/lib/contentstack/api.rb
CHANGED
@@ -1,191 +1,191 @@
|
|
1
|
-
require 'uri'
|
2
|
-
require 'net/http'
|
3
|
-
require 'active_support'
|
4
|
-
require 'active_support/json'
|
5
|
-
require 'open-uri'
|
6
|
-
require 'util'
|
7
|
-
module Contentstack
|
8
|
-
class API
|
9
|
-
using Utility
|
10
|
-
def self.init_api(api_key, delivery_token, environment, host, branch, live_preview, proxy, retry_options)
|
11
|
-
@host = host
|
12
|
-
@api_version = '/v3'
|
13
|
-
@environment = environment
|
14
|
-
@api_key = api_key
|
15
|
-
@access_token = delivery_token
|
16
|
-
@branch = branch
|
17
|
-
@headers = {environment: @environment}
|
18
|
-
@live_preview = live_preview
|
19
|
-
@proxy_details = proxy
|
20
|
-
@timeout = retry_options["timeout"]
|
21
|
-
@retryDelay = retry_options["retryDelay"]
|
22
|
-
@retryLimit = retry_options["retryLimit"]
|
23
|
-
@errorRetry = retry_options["errorRetry"]
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.live_preview_query(query= {})
|
27
|
-
@live_preview[:content_type_uid] = query[:content_type_uid] || query["content_type_uid"]
|
28
|
-
@live_preview[:live_preview] = query[:live_preview] || query["live_preview"]
|
29
|
-
@live_preview[:entry_uid] = query[:entry_uid] || query["entry_uid"]
|
30
|
-
if @live_preview[:content_type_uid].present? && @live_preview[:entry_uid].present?
|
31
|
-
path = "/content_types/#{@live_preview[:content_type_uid]}/entries/#{@live_preview[:entry_uid]}"
|
32
|
-
@live_preview_response = send_preview_request(path)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def self.fetch_content_types(uid="")
|
37
|
-
if !uid.nil? && !uid.empty?
|
38
|
-
path = "/content_types/#{uid}"
|
39
|
-
else
|
40
|
-
path = "/content_types"
|
41
|
-
end
|
42
|
-
fetch_retry(path, {})
|
43
|
-
end
|
44
|
-
|
45
|
-
def self.fetch_entries(content_type, query)
|
46
|
-
if @live_preview[:enable] && @live_preview[:content_type_uid] == content_type
|
47
|
-
path = "/content_types/#{content_type}/entries"
|
48
|
-
send_preview_request(path, query)
|
49
|
-
else
|
50
|
-
path = "/content_types/#{content_type}/entries"
|
51
|
-
fetch_retry(path, query)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def self.fetch_entry(content_type, entry_uid, query)
|
56
|
-
if @live_preview[:enable] && @live_preview[:content_type_uid] == content_type
|
57
|
-
path = "/content_types/#{content_type}/entries/#{entry_uid}"
|
58
|
-
send_preview_request(path, query)
|
59
|
-
else
|
60
|
-
path = "/content_types/#{content_type}/entries/#{entry_uid}"
|
61
|
-
fetch_retry(path, query)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def self.get_assets(asset_uid=nil)
|
66
|
-
path = "/assets"
|
67
|
-
path += "/#{asset_uid}" if !asset_uid.nil?
|
68
|
-
fetch_retry(path)
|
69
|
-
end
|
70
|
-
|
71
|
-
def self.get_sync_items(query)
|
72
|
-
path = "/stacks/sync"
|
73
|
-
fetch_retry(path, query)
|
74
|
-
end
|
75
|
-
|
76
|
-
private
|
77
|
-
def self.fetch_retry(path, query=nil, count=0)
|
78
|
-
response = send_request(path, query)
|
79
|
-
if @errorRetry.include?(response["status_code"].to_i)
|
80
|
-
if count < @retryLimit
|
81
|
-
retryDelay_in_seconds = @retryDelay / 1000 #converting retry_delay from milliseconds into seconds
|
82
|
-
sleep(retryDelay_in_seconds.to_i) #sleep method requires time in seconds as parameter
|
83
|
-
response = fetch_retry(path, query, (count + 1))
|
84
|
-
else
|
85
|
-
raise Contentstack::Error.new(response) #Retry Limit exceeded
|
86
|
-
end
|
87
|
-
else
|
88
|
-
to_render_content(response)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
def self.send_request(path, q=nil)
|
93
|
-
q ||= {}
|
94
|
-
|
95
|
-
q.merge!(@headers)
|
96
|
-
|
97
|
-
query = "?" + q.to_query
|
98
|
-
# puts "Request URL:- #{@host}#{@api_version}#{path}#{query} \n\n"
|
99
|
-
params = {
|
100
|
-
"api_key" => @api_key,
|
101
|
-
"access_token"=> @access_token,
|
102
|
-
"user_agent"=> "ruby-sdk/#{Contentstack::VERSION}",
|
103
|
-
"x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}",
|
104
|
-
"read_timeout" => @timeout
|
105
|
-
}
|
106
|
-
if !@branch.nil? && !@branch.empty?
|
107
|
-
params["branch"] = @branch
|
108
|
-
end
|
109
|
-
|
110
|
-
if @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty?
|
111
|
-
params["proxy"] = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/").to_s
|
112
|
-
end
|
113
|
-
|
114
|
-
if @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].present? && @proxy_details[:password].present?
|
115
|
-
proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/").to_s
|
116
|
-
params[:proxy_http_basic_authentication] = [proxy_uri, @proxy_details[:username], @proxy_details[:password]]
|
117
|
-
end
|
118
|
-
|
119
|
-
begin
|
120
|
-
ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", params).read)
|
121
|
-
rescue OpenURI::HTTPError => error
|
122
|
-
response = error.io
|
123
|
-
#response.status
|
124
|
-
# => ["503", "Service Unavailable"]
|
125
|
-
error_response = JSON.parse(response.string)
|
126
|
-
error_status = {"status_code" => response.status[0], "status_message" => response.status[1]}
|
127
|
-
error = error_response.merge(error_status)
|
128
|
-
raise Contentstack::Error.new(error.to_s)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
def self.send_preview_request(path, q=nil)
|
133
|
-
q ||= {}
|
134
|
-
|
135
|
-
q.merge!({live_preview: (!@live_preview.key?(:live_preview) ? 'init' : @live_preview[:live_preview]),})
|
136
|
-
|
137
|
-
query = "?" + q.to_query
|
138
|
-
preview_host = @live_preview[:host]
|
139
|
-
params = {
|
140
|
-
"api_key" => @api_key,
|
141
|
-
"authorization" => @live_preview[:management_token],
|
142
|
-
"user_agent"=> "ruby-sdk/#{Contentstack::VERSION}",
|
143
|
-
"x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}",
|
144
|
-
"read_timeout" => @timeout
|
145
|
-
}
|
146
|
-
if !@branch.nil? && !@branch.empty?
|
147
|
-
params["branch"] = @branch
|
148
|
-
end
|
149
|
-
|
150
|
-
if @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty?
|
151
|
-
params["proxy"] = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/").to_s
|
152
|
-
end
|
153
|
-
|
154
|
-
if @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].present? && @proxy_details[:password].present?
|
155
|
-
proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/").to_s
|
156
|
-
params[:proxy_http_basic_authentication] = [proxy_uri, @proxy_details[:username], @proxy_details[:password]]
|
157
|
-
end
|
158
|
-
|
159
|
-
begin
|
160
|
-
ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}",params).read)
|
161
|
-
rescue OpenURI::HTTPError => error
|
162
|
-
response = error.io
|
163
|
-
#response.status
|
164
|
-
# => ["503", "Service Unavailable"]
|
165
|
-
error_response = JSON.parse(response.string)
|
166
|
-
error_status = {"status_code" => response.status[0], "status_message" => response.status[1]}
|
167
|
-
error = error_response.merge(error_status)
|
168
|
-
raise Contentstack::Error.new(error.to_s)
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
def self.to_render_content(resp)
|
173
|
-
if resp.class == Hash
|
174
|
-
if resp.key?('uid') && resp['uid'] == @live_preview[:entry_uid]
|
175
|
-
resp = resp.merge(@live_preview_response)
|
176
|
-
else
|
177
|
-
resp_keys = resp.keys
|
178
|
-
resp_keys.each {|key|
|
179
|
-
resp[key] = to_render_content(resp[key])
|
180
|
-
}
|
181
|
-
end
|
182
|
-
elsif resp.class == Array
|
183
|
-
resp.each_with_index {|value, index|
|
184
|
-
resp[index] = to_render_content(value)
|
185
|
-
}
|
186
|
-
end
|
187
|
-
resp
|
188
|
-
end
|
189
|
-
|
190
|
-
end
|
191
|
-
end
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/json'
|
5
|
+
require 'open-uri'
|
6
|
+
require 'util'
|
7
|
+
module Contentstack
|
8
|
+
class API
|
9
|
+
using Utility
|
10
|
+
def self.init_api(api_key, delivery_token, environment, host, branch, live_preview, proxy, retry_options)
|
11
|
+
@host = host
|
12
|
+
@api_version = '/v3'
|
13
|
+
@environment = environment
|
14
|
+
@api_key = api_key
|
15
|
+
@access_token = delivery_token
|
16
|
+
@branch = branch
|
17
|
+
@headers = {environment: @environment}
|
18
|
+
@live_preview = live_preview
|
19
|
+
@proxy_details = proxy
|
20
|
+
@timeout = retry_options["timeout"]
|
21
|
+
@retryDelay = retry_options["retryDelay"]
|
22
|
+
@retryLimit = retry_options["retryLimit"]
|
23
|
+
@errorRetry = retry_options["errorRetry"]
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.live_preview_query(query= {})
|
27
|
+
@live_preview[:content_type_uid] = query[:content_type_uid] || query["content_type_uid"]
|
28
|
+
@live_preview[:live_preview] = query[:live_preview] || query["live_preview"]
|
29
|
+
@live_preview[:entry_uid] = query[:entry_uid] || query["entry_uid"]
|
30
|
+
if @live_preview[:content_type_uid].present? && @live_preview[:entry_uid].present?
|
31
|
+
path = "/content_types/#{@live_preview[:content_type_uid]}/entries/#{@live_preview[:entry_uid]}"
|
32
|
+
@live_preview_response = send_preview_request(path)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.fetch_content_types(uid="")
|
37
|
+
if !uid.nil? && !uid.empty?
|
38
|
+
path = "/content_types/#{uid}"
|
39
|
+
else
|
40
|
+
path = "/content_types"
|
41
|
+
end
|
42
|
+
fetch_retry(path, {})
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.fetch_entries(content_type, query)
|
46
|
+
if @live_preview[:enable] && @live_preview[:content_type_uid] == content_type
|
47
|
+
path = "/content_types/#{content_type}/entries"
|
48
|
+
send_preview_request(path, query)
|
49
|
+
else
|
50
|
+
path = "/content_types/#{content_type}/entries"
|
51
|
+
fetch_retry(path, query)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.fetch_entry(content_type, entry_uid, query)
|
56
|
+
if @live_preview[:enable] && @live_preview[:content_type_uid] == content_type
|
57
|
+
path = "/content_types/#{content_type}/entries/#{entry_uid}"
|
58
|
+
send_preview_request(path, query)
|
59
|
+
else
|
60
|
+
path = "/content_types/#{content_type}/entries/#{entry_uid}"
|
61
|
+
fetch_retry(path, query)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.get_assets(asset_uid=nil)
|
66
|
+
path = "/assets"
|
67
|
+
path += "/#{asset_uid}" if !asset_uid.nil?
|
68
|
+
fetch_retry(path)
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.get_sync_items(query)
|
72
|
+
path = "/stacks/sync"
|
73
|
+
fetch_retry(path, query)
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
def self.fetch_retry(path, query=nil, count=0)
|
78
|
+
response = send_request(path, query)
|
79
|
+
if @errorRetry.include?(response["status_code"].to_i)
|
80
|
+
if count < @retryLimit
|
81
|
+
retryDelay_in_seconds = @retryDelay / 1000 #converting retry_delay from milliseconds into seconds
|
82
|
+
sleep(retryDelay_in_seconds.to_i) #sleep method requires time in seconds as parameter
|
83
|
+
response = fetch_retry(path, query, (count + 1))
|
84
|
+
else
|
85
|
+
raise Contentstack::Error.new(response) #Retry Limit exceeded
|
86
|
+
end
|
87
|
+
else
|
88
|
+
to_render_content(response)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def self.send_request(path, q=nil)
|
93
|
+
q ||= {}
|
94
|
+
|
95
|
+
q.merge!(@headers)
|
96
|
+
|
97
|
+
query = "?" + q.to_query
|
98
|
+
# puts "Request URL:- #{@host}#{@api_version}#{path}#{query} \n\n"
|
99
|
+
params = {
|
100
|
+
"api_key" => @api_key,
|
101
|
+
"access_token"=> @access_token,
|
102
|
+
"user_agent"=> "ruby-sdk/#{Contentstack::VERSION}",
|
103
|
+
"x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}",
|
104
|
+
"read_timeout" => @timeout
|
105
|
+
}
|
106
|
+
if !@branch.nil? && !@branch.empty?
|
107
|
+
params["branch"] = @branch
|
108
|
+
end
|
109
|
+
|
110
|
+
if @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty?
|
111
|
+
params["proxy"] = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/").to_s
|
112
|
+
end
|
113
|
+
|
114
|
+
if @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].present? && @proxy_details[:password].present?
|
115
|
+
proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/").to_s
|
116
|
+
params[:proxy_http_basic_authentication] = [proxy_uri, @proxy_details[:username], @proxy_details[:password]]
|
117
|
+
end
|
118
|
+
|
119
|
+
begin
|
120
|
+
ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", params).read)
|
121
|
+
rescue OpenURI::HTTPError => error
|
122
|
+
response = error.io
|
123
|
+
#response.status
|
124
|
+
# => ["503", "Service Unavailable"]
|
125
|
+
error_response = JSON.parse(response.string)
|
126
|
+
error_status = {"status_code" => response.status[0], "status_message" => response.status[1]}
|
127
|
+
error = error_response.merge(error_status)
|
128
|
+
raise Contentstack::Error.new(error.to_s)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def self.send_preview_request(path, q=nil)
|
133
|
+
q ||= {}
|
134
|
+
|
135
|
+
q.merge!({live_preview: (!@live_preview.key?(:live_preview) ? 'init' : @live_preview[:live_preview]),})
|
136
|
+
|
137
|
+
query = "?" + q.to_query
|
138
|
+
preview_host = @live_preview[:host]
|
139
|
+
params = {
|
140
|
+
"api_key" => @api_key,
|
141
|
+
"authorization" => @live_preview[:management_token],
|
142
|
+
"user_agent"=> "ruby-sdk/#{Contentstack::VERSION}",
|
143
|
+
"x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}",
|
144
|
+
"read_timeout" => @timeout
|
145
|
+
}
|
146
|
+
if !@branch.nil? && !@branch.empty?
|
147
|
+
params["branch"] = @branch
|
148
|
+
end
|
149
|
+
|
150
|
+
if @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty?
|
151
|
+
params["proxy"] = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/").to_s
|
152
|
+
end
|
153
|
+
|
154
|
+
if @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].present? && @proxy_details[:password].present?
|
155
|
+
proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/").to_s
|
156
|
+
params[:proxy_http_basic_authentication] = [proxy_uri, @proxy_details[:username], @proxy_details[:password]]
|
157
|
+
end
|
158
|
+
|
159
|
+
begin
|
160
|
+
ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}",params).read)
|
161
|
+
rescue OpenURI::HTTPError => error
|
162
|
+
response = error.io
|
163
|
+
#response.status
|
164
|
+
# => ["503", "Service Unavailable"]
|
165
|
+
error_response = JSON.parse(response.string)
|
166
|
+
error_status = {"status_code" => response.status[0], "status_message" => response.status[1]}
|
167
|
+
error = error_response.merge(error_status)
|
168
|
+
raise Contentstack::Error.new(error.to_s)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def self.to_render_content(resp)
|
173
|
+
if resp.class == Hash
|
174
|
+
if resp.key?('uid') && resp['uid'] == @live_preview[:entry_uid]
|
175
|
+
resp = resp.merge(@live_preview_response)
|
176
|
+
else
|
177
|
+
resp_keys = resp.keys
|
178
|
+
resp_keys.each {|key|
|
179
|
+
resp[key] = to_render_content(resp[key])
|
180
|
+
}
|
181
|
+
end
|
182
|
+
elsif resp.class == Array
|
183
|
+
resp.each_with_index {|value, index|
|
184
|
+
resp[index] = to_render_content(value)
|
185
|
+
}
|
186
|
+
end
|
187
|
+
resp
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
191
|
+
end
|
data/lib/contentstack/asset.rb
CHANGED
@@ -1,69 +1,69 @@
|
|
1
|
-
require 'util'
|
2
|
-
module Contentstack
|
3
|
-
|
4
|
-
# Asset class to fetch file details on Conentstack server.
|
5
|
-
class Asset
|
6
|
-
using Utility
|
7
|
-
attr_reader :uid, :content_type, :filename, :file_size, :tags, :url
|
8
|
-
|
9
|
-
# @!attribute [r] uid
|
10
|
-
# Contentstack Asset UID for this asset
|
11
|
-
|
12
|
-
# @!attribute [r] content_type
|
13
|
-
# Content Type for the asset. image/png, image/jpeg, application/pdf, video/mp4 etc.
|
14
|
-
|
15
|
-
# @!attribute [r] filename
|
16
|
-
# Name of the asset.
|
17
|
-
|
18
|
-
# @!attribute [r] file_size
|
19
|
-
# Size of the asset.
|
20
|
-
|
21
|
-
# @!attribute [r] tags
|
22
|
-
# Array of tags assigned to the asset.
|
23
|
-
|
24
|
-
# @!attribute [r] url
|
25
|
-
# URL to fetch/render the asset
|
26
|
-
|
27
|
-
# Create instance of an Asset. Accepts either a uid of asset (String) or a complete asset JSON
|
28
|
-
# @param [String/Hash] attrs
|
29
|
-
# Usage for String parameter
|
30
|
-
# @asset = @stack.asset("some_asset_uid")
|
31
|
-
# @asset.fetch
|
32
|
-
#
|
33
|
-
# Usage for Hash parameter
|
34
|
-
# @asset = @stack.asset({
|
35
|
-
# :uid => "some_asset_uid",
|
36
|
-
# :content_type => "file_type", # image/png, image/jpeg, application/pdf, video/mp4 etc.
|
37
|
-
# :filename => "some_file_name",
|
38
|
-
# :file_size => "some_file_size",
|
39
|
-
# :tags => ["tag1", "tag2", "tag3"],
|
40
|
-
# :url => "file_url"
|
41
|
-
# })
|
42
|
-
# @asset.fetch
|
43
|
-
def initialize(attrs)
|
44
|
-
if attrs.class == String
|
45
|
-
@uid = attrs
|
46
|
-
else
|
47
|
-
attrs = attrs.symbolize_keys
|
48
|
-
@uid = attrs[:uid]
|
49
|
-
@content_type = attrs[:content_type]
|
50
|
-
@filename = attrs[:filename]
|
51
|
-
@file_size = attrs[:file_size]
|
52
|
-
@tags = attrs[:tags]
|
53
|
-
@url = attrs[:url]
|
54
|
-
end
|
55
|
-
|
56
|
-
self
|
57
|
-
end
|
58
|
-
|
59
|
-
# Fetch a particular asset using uid.
|
60
|
-
# @asset = @stack.asset('some_asset_uid')
|
61
|
-
# @asset.fetch
|
62
|
-
# puts @asset.url
|
63
|
-
def fetch
|
64
|
-
json = API.get_assets(@uid)
|
65
|
-
# puts "json -- #{json}"
|
66
|
-
self.class.new(json["asset"])
|
67
|
-
end
|
68
|
-
end
|
1
|
+
require 'util'
|
2
|
+
module Contentstack
|
3
|
+
|
4
|
+
# Asset class to fetch file details on Conentstack server.
|
5
|
+
class Asset
|
6
|
+
using Utility
|
7
|
+
attr_reader :uid, :content_type, :filename, :file_size, :tags, :url
|
8
|
+
|
9
|
+
# @!attribute [r] uid
|
10
|
+
# Contentstack Asset UID for this asset
|
11
|
+
|
12
|
+
# @!attribute [r] content_type
|
13
|
+
# Content Type for the asset. image/png, image/jpeg, application/pdf, video/mp4 etc.
|
14
|
+
|
15
|
+
# @!attribute [r] filename
|
16
|
+
# Name of the asset.
|
17
|
+
|
18
|
+
# @!attribute [r] file_size
|
19
|
+
# Size of the asset.
|
20
|
+
|
21
|
+
# @!attribute [r] tags
|
22
|
+
# Array of tags assigned to the asset.
|
23
|
+
|
24
|
+
# @!attribute [r] url
|
25
|
+
# URL to fetch/render the asset
|
26
|
+
|
27
|
+
# Create instance of an Asset. Accepts either a uid of asset (String) or a complete asset JSON
|
28
|
+
# @param [String/Hash] attrs
|
29
|
+
# Usage for String parameter
|
30
|
+
# @asset = @stack.asset("some_asset_uid")
|
31
|
+
# @asset.fetch
|
32
|
+
#
|
33
|
+
# Usage for Hash parameter
|
34
|
+
# @asset = @stack.asset({
|
35
|
+
# :uid => "some_asset_uid",
|
36
|
+
# :content_type => "file_type", # image/png, image/jpeg, application/pdf, video/mp4 etc.
|
37
|
+
# :filename => "some_file_name",
|
38
|
+
# :file_size => "some_file_size",
|
39
|
+
# :tags => ["tag1", "tag2", "tag3"],
|
40
|
+
# :url => "file_url"
|
41
|
+
# })
|
42
|
+
# @asset.fetch
|
43
|
+
def initialize(attrs)
|
44
|
+
if attrs.class == String
|
45
|
+
@uid = attrs
|
46
|
+
else
|
47
|
+
attrs = attrs.symbolize_keys
|
48
|
+
@uid = attrs[:uid]
|
49
|
+
@content_type = attrs[:content_type]
|
50
|
+
@filename = attrs[:filename]
|
51
|
+
@file_size = attrs[:file_size]
|
52
|
+
@tags = attrs[:tags]
|
53
|
+
@url = attrs[:url]
|
54
|
+
end
|
55
|
+
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
# Fetch a particular asset using uid.
|
60
|
+
# @asset = @stack.asset('some_asset_uid')
|
61
|
+
# @asset.fetch
|
62
|
+
# puts @asset.url
|
63
|
+
def fetch
|
64
|
+
json = API.get_assets(@uid)
|
65
|
+
# puts "json -- #{json}"
|
66
|
+
self.class.new(json["asset"])
|
67
|
+
end
|
68
|
+
end
|
69
69
|
end
|
@@ -1,28 +1,28 @@
|
|
1
|
-
require 'contentstack/asset'
|
2
|
-
require 'util'
|
3
|
-
|
4
|
-
module Contentstack
|
5
|
-
# Asset class to fetch details of files on Conentstack server.
|
6
|
-
class AssetCollection
|
7
|
-
using Utility
|
8
|
-
attr_reader :assets
|
9
|
-
|
10
|
-
def initialize(assets_array=nil)
|
11
|
-
if assets_array.nil?
|
12
|
-
@assets = []
|
13
|
-
return self
|
14
|
-
else
|
15
|
-
@assets = assets_array.collect{|a| Asset.new(a)}
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
# Fetch assets uploaded to Contentstack
|
20
|
-
#
|
21
|
-
# Example:
|
22
|
-
# @assets = @stack.assets.fetch
|
23
|
-
def fetch
|
24
|
-
json = API.get_assets
|
25
|
-
self.class.new(json["assets"])
|
26
|
-
end
|
27
|
-
end
|
1
|
+
require 'contentstack/asset'
|
2
|
+
require 'util'
|
3
|
+
|
4
|
+
module Contentstack
|
5
|
+
# Asset class to fetch details of files on Conentstack server.
|
6
|
+
class AssetCollection
|
7
|
+
using Utility
|
8
|
+
attr_reader :assets
|
9
|
+
|
10
|
+
def initialize(assets_array=nil)
|
11
|
+
if assets_array.nil?
|
12
|
+
@assets = []
|
13
|
+
return self
|
14
|
+
else
|
15
|
+
@assets = assets_array.collect{|a| Asset.new(a)}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Fetch assets uploaded to Contentstack
|
20
|
+
#
|
21
|
+
# Example:
|
22
|
+
# @assets = @stack.assets.fetch
|
23
|
+
def fetch
|
24
|
+
json = API.get_assets
|
25
|
+
self.class.new(json["assets"])
|
26
|
+
end
|
27
|
+
end
|
28
28
|
end
|