22
33module Contentstack
44 class Entry
5- attr_reader :fields , :content_type , :uid , :owner
5+ attr_reader :fields , :content_type , :uid , :owner , :query , :schema , :content_type
66 def initialize ( attrs , content_type_uid = nil )
77 setup ( attrs , content_type_uid )
88 end
99
10+ # Get entries from the specified locale.
11+ #
12+ # @param [String] code The locale code of the entry
13+ #
14+ # Example
15+ # @entry = @stack.content_type('category').entry(entry_uid)
16+ # @entry.locale('en-us')
17+ #
18+ # @return [Contentstack::Entry]
19+ def locale ( code )
20+ @query [ :locale ] = code
21+ self
22+ end
23+
24+ # Specifies an array of 'only' keys in BASE object that would be 'included' in the response.
25+ #
26+ # @param [Array] fields Array of the 'only' reference keys to be included in response.
27+ # @param [Array] fields_with_base Can be used to denote 'only' fields of the reference class
28+ #
29+ # Example
30+ # # Include only title and description field in response
31+ # @entry = @stack.content_type('category').entry(entry_uid)
32+ # @entry.only(['title', 'description'])
33+ #
34+ # # Query product and include only the title and description from category reference
35+ # @entry = @stack.content_type('product').entry(entry_uid)
36+ # @entry.include_reference('category')
37+ # .only('category', ['title', 'description'])
38+ #
39+ # @return [Contentstack::Entry]
40+ def only ( fields , fields_with_base = nil )
41+ q = { }
42+ if [ Array , String ] . include? ( fields_with_base . class )
43+ fields_with_base = [ fields_with_base ] if fields_with_base . class == String
44+ q [ fields . to_sym ] = fields_with_base
45+ else
46+ fields = [ fields ] if fields . class == String
47+ q = { BASE : fields }
48+ end
49+
50+ @query [ :only ] = q
51+ self
52+ end
53+
54+ # Specifies list of field uids that would be 'excluded' from the response.
55+ #
56+ # @param [Array] fields Array of field uid which get 'excluded' from the response.
57+ # @param [Array] fields_with_base Can be used to denote 'except' fields of the reference class
58+ #
59+ # Example
60+ # # Exclude 'description' field in response
61+ # @entry = @stack.content_type('category').entry(entry_uid)
62+ # @entry.except(['description'])
63+ #
64+ # # Query product and exclude the 'description' from category reference
65+ # @entry = @stack.content_type('product').entry(entry_uid)
66+ # @entry.include_reference('category')
67+ # .except('category', ['description'])
68+ #
69+ # @return [Contentstack::Entry]
70+ def except ( fields , fields_with_base = nil )
71+ q = { }
72+ if [ Array , String ] . include? ( fields_with_base . class )
73+ fields_with_base = [ fields_with_base ] if fields_with_base . class == String
74+ q [ fields . to_sym ] = fields_with_base
75+ else
76+ fields = [ fields ] if fields . class == String
77+ q = { BASE : fields }
78+ end
79+
80+ @query [ :except ] = q
81+ self
82+ end
83+
84+ # Add a constraint that requires a particular reference key details.
85+ #
86+ # @param [String/Array] reference_field_uids Pass string or array of reference fields that must be included in the response
87+ #
88+ # Example
89+ #
90+ # # Include reference of 'category'
91+ # @entry = @stack.content_type('product').entry(entry_uid)
92+ # @entry.include_reference('category')
93+ #
94+ # # Include reference of 'category' and 'reviews'
95+ # @entry = @stack.content_type('product').entry(entry_uid)
96+ # @entry.include_reference(['category', 'reviews'])
97+ #
98+ # @return [Contentstack::Entry]
99+ def include_reference ( reference_field_uids )
100+ self . include ( reference_field_uids )
101+ end
102+
103+ # Include schemas of all returned objects along with objects themselves.
104+ #
105+ # Example
106+ #
107+ # @entry = @stack.content_type('product').entry(entry_uid)
108+ # @entry.include_schema
109+ #
110+ # @return [Contentstack::Entry]
111+ def include_schema ( flag = true )
112+ @query [ :include_schema ] = flag
113+ self
114+ end
115+
116+ # Include object owner's profile in the objects data.
117+ #
118+ # Example
119+ #
120+ # @entry = @stack.content_type('product').entry(entry_uid)
121+ # @entry.include_owner
122+ #
123+ # @return [Contentstack::Entry]
124+ def include_owner ( flag = true )
125+ @query [ :include_owner ] = flag
126+ self
127+ end
128+
129+ # Include object's content_type in response
130+ #
131+ # Example
132+ #
133+ # @entry = @stack.content_type('product').entry(entry_uid)
134+ # @entry.include_content_type
135+ #
136+ # @return [Contentstack::Entry]
137+ def include_content_type ( flag = true )
138+ @query [ :include_content_type ] = flag
139+ self
140+ end
141+
142+ # Include the fallback locale publish content, if specified locale content is not publish.
143+ #
144+ # Example
145+ #
146+ # @entry = @stack.content_type('product').entry(entry_uid)
147+ # @entry.include_fallback
148+ #
149+ # @return [Contentstack::Entry]
150+ def include_fallback ( flag = true )
151+ @query [ :include_fallback ] = flag
152+ self
153+ end
154+
155+ #
156+ # @return [Contentstack::Query]
157+ def include ( field_uids )
158+ field_uids = [ field_uids ] if field_uids . class == String
159+ @query [ :include ] ||= [ ]
160+ @query [ :include ] = @query [ :include ] | field_uids
161+ self
162+ end
163+
164+ # Execute entry
165+ #
166+ # Example
167+ #
168+ # @entry = @stack.content_type('product').entry(entry_uid)
169+ # @entry.fetch
170+ #
171+ # @return [Contentstack::EntryCollection]
10172 def fetch
11- entry = API . fetch_entry ( @content_type , self . fields [ :uid ] )
173+ entry = API . fetch_entry ( @content_type , self . fields [ :uid ] , @query )
12174 setup ( entry [ "entry" ] )
175+ @schema = entry [ "schema" ] . symbolize_keys if entry [ "schema" ]
176+ @content_type = entry [ "content_type" ] . symbolize_keys if entry [ "content_type" ]
13177 self
14178 end
15179
@@ -24,6 +188,7 @@ def setup(attrs, content_type_uid=nil)
24188 @content_type = content_type_uid if !content_type_uid . blank?
25189 @owner = attrs [ :_owner ] if attrs [ :_owner ]
26190 @uid = attrs [ :uid ]
191+ @query = { }
27192 end
28193 end
29194end
0 commit comments