contentstack 0.6.1 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/codeql-analysis.yml +68 -68
  3. data/.github/workflows/jira.yml +28 -0
  4. data/.github/workflows/release-gem.yml +31 -0
  5. data/.github/workflows/sast-scan.yml +10 -10
  6. data/.github/workflows/sca-scan.yml +15 -13
  7. data/.github/workflows/secrets-scan.yml +10 -10
  8. data/.gitignore +11 -11
  9. data/.yardopts +6 -6
  10. data/CHANGELOG.md +114 -78
  11. data/CODEOWNERS +1 -1
  12. data/CODE_OF_CONDUCT.md +73 -73
  13. data/Gemfile +2 -2
  14. data/Gemfile.lock +76 -77
  15. data/LICENSE.txt +21 -21
  16. data/README.md +197 -197
  17. data/SECURITY.md +27 -27
  18. data/contentstack.gemspec +29 -29
  19. data/lib/contentstack/api.rb +191 -212
  20. data/lib/contentstack/asset.rb +68 -68
  21. data/lib/contentstack/asset_collection.rb +27 -27
  22. data/lib/contentstack/client.rb +91 -91
  23. data/lib/contentstack/content_type.rb +53 -53
  24. data/lib/contentstack/entry.rb +221 -221
  25. data/lib/contentstack/entry_collection.rb +44 -44
  26. data/lib/contentstack/error.rb +6 -6
  27. data/lib/contentstack/query.rb +653 -653
  28. data/lib/contentstack/region.rb +5 -5
  29. data/lib/contentstack/sync_result.rb +29 -29
  30. data/lib/contentstack/version.rb +2 -2
  31. data/lib/contentstack.rb +31 -31
  32. data/lib/util.rb +110 -110
  33. data/rakefile.rb +3 -3
  34. data/spec/asset_collection_spec.rb +15 -15
  35. data/spec/asset_spec.rb +47 -47
  36. data/spec/content_type_spec.rb +80 -80
  37. data/spec/contentstack_spec.rb +38 -38
  38. data/spec/entry_collection_spec.rb +41 -41
  39. data/spec/entry_spec.rb +101 -101
  40. data/spec/fixtures/asset.json +1 -1
  41. data/spec/fixtures/asset_collection.json +1 -1
  42. data/spec/fixtures/category_content_type.json +1 -1
  43. data/spec/fixtures/category_entry.json +1 -1
  44. data/spec/fixtures/category_entry_collection.json +1 -1
  45. data/spec/fixtures/category_entry_collection_without_count.json +1 -1
  46. data/spec/fixtures/content_types.json +1 -1
  47. data/spec/fixtures/product_entry.json +1 -1
  48. data/spec/fixtures/product_entry_collection.json +1 -1
  49. data/spec/fixtures/sync_init.json +2974 -2974
  50. data/spec/query_spec.rb +205 -205
  51. data/spec/spec_helper.rb +180 -180
  52. data/spec/sync_spec.rb +26 -26
  53. metadata +7 -17
@@ -1,212 +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
- begin
111
- if @proxy_details.empty?
112
- ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", params).read)
113
- elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].present? && @proxy_details[:password].present?
114
- proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/")
115
- proxy_username = @proxy_details[:username]
116
- proxy_password = @proxy_details[:password]
117
-
118
- if !@branch.nil? && !@branch.empty?
119
- ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", :proxy_http_basic_authentication => [proxy_uri, proxy_username, proxy_password], "api_key" => @api_key, "access_token"=> @access_token, "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}", "read_timeout" => @timeout, "branch" => @branch).read)
120
- else
121
- ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", :proxy_http_basic_authentication => [proxy_uri, proxy_username, proxy_password], "api_key" => @api_key, "access_token"=> @access_token, "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}", "read_timeout" => @timeout).read)
122
- end
123
- elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty?
124
- proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/")
125
-
126
- if !@branch.nil? && !@branch.empty?
127
- ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", "proxy" => proxy_uri, "api_key" => @api_key, "access_token"=> @access_token, "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}", "read_timeout" => @timeout, "branch" => @branch).read)
128
- else
129
- ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", "proxy" => proxy_uri, "api_key" => @api_key, "access_token"=> @access_token, "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}", "read_timeout" => @timeout).read)
130
- end
131
- end
132
- rescue OpenURI::HTTPError => error
133
- response = error.io
134
- #response.status
135
- # => ["503", "Service Unavailable"]
136
- error_response = JSON.parse(response.string)
137
- error_status = {"status_code" => response.status[0], "status_message" => response.status[1]}
138
- error = error_response.merge(error_status)
139
- raise Contentstack::Error.new(error.to_s)
140
- end
141
- end
142
-
143
- def self.send_preview_request(path, q=nil)
144
- q ||= {}
145
-
146
- q.merge!({live_preview: (!@live_preview.key?(:live_preview) ? 'init' : @live_preview[:live_preview]),})
147
-
148
- query = "?" + q.to_query
149
- preview_host = @live_preview[:host]
150
- params = {
151
- "api_key" => @api_key,
152
- "authorization" => @live_preview[:management_token],
153
- "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}",
154
- "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}",
155
- "read_timeout" => @timeout
156
- }
157
- if !@branch.nil? && !@branch.empty?
158
- params["branch"] = @branch
159
- end
160
- begin
161
- if @proxy_details.empty?
162
- ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}",params).read)
163
- elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].present? && @proxy_details[:password].present?
164
- proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/")
165
- proxy_username = @proxy_details[:username]
166
- proxy_password = @proxy_details[:password]
167
-
168
- if !@branch.nil? && !@branch.empty?
169
- ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}", :proxy_http_basic_authentication => [proxy_uri, proxy_username, proxy_password], "api_key" => @api_key, "authorization" => @live_preview[:management_token], "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}", "read_timeout" => @timeout, "branch" => @branch).read)
170
- else
171
- ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}", :proxy_http_basic_authentication => [proxy_uri, proxy_username, proxy_password], "api_key" => @api_key, "authorization" => @live_preview[:management_token], "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}", "read_timeout" => @timeout).read)
172
- end
173
- elsif @proxy_details.present? && @proxy_details[:url].present? && @proxy_details[:port].present? && @proxy_details[:username].empty? && @proxy_details[:password].empty?
174
- proxy_uri = URI.parse("http://#{@proxy_details[:url]}:#{@proxy_details[:port]}/")
175
-
176
- if !@branch.nil? && !@branch.empty?
177
- ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}", "proxy" => proxy_uri, "api_key" => @api_key, "authorization" => @live_preview[:management_token], "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}", "read_timeout" => @timeout, "branch" => @branch).read)
178
- else
179
- ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}", "proxy" => proxy_uri, "api_key" => @api_key, "authorization" => @live_preview[:management_token], "user_agent"=> "ruby-sdk/#{Contentstack::VERSION}", "x-user-agent" => "ruby-sdk/#{Contentstack::VERSION}", "read_timeout" => @timeout).read)
180
- end
181
- end
182
- rescue OpenURI::HTTPError => error
183
- response = error.io
184
- #response.status
185
- # => ["503", "Service Unavailable"]
186
- error_response = JSON.parse(response.string)
187
- error_status = {"status_code" => response.status[0], "status_message" => response.status[1]}
188
- error = error_response.merge(error_status)
189
- raise Contentstack::Error.new(error.to_s)
190
- end
191
- end
192
-
193
- def self.to_render_content(resp)
194
- if resp.class == Hash
195
- if resp.key?('uid') && resp['uid'] == @live_preview[:entry_uid]
196
- resp = resp.merge(@live_preview_response)
197
- else
198
- resp_keys = resp.keys
199
- resp_keys.each {|key|
200
- resp[key] = to_render_content(resp[key])
201
- }
202
- end
203
- elsif resp.class == Array
204
- resp.each_with_index {|value, index|
205
- resp[index] = to_render_content(value)
206
- }
207
- end
208
- resp
209
- end
210
-
211
- end
212
- 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
@@ -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