Class: Api::V5::HomesController

Inherits:
BaseController
  • Object
show all
Includes:
Concerns::Authorization, Concerns::PaginationParam, ImageHelper
Defined in:
app/controllers/api/v5/homes_controller.rb

Constant Summary

Constants inherited from BaseController

BaseController::CACHE_NAMESPACE, BaseController::INTERNAL_SERVER_ERROR_MESSAGE, BaseController::ResponseSchema

Instance Method Summary collapse

Methods included from ImageHelper

#fix_image_url

Methods inherited from BaseController

#identity_cache_memoization

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ResponseCacheConcern

#my_response_cache

Instance Method Details

#dealsObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/api/v5/homes_controller.rb', line 126

def deals
  city_id_param = params[:city_id].to_i.positive? ? params[:city_id] : nil

  filter = Api::V5::SearchIconsFilter.new
  filter.by_city_id(city_id_param)
  filter.build_collections

  set_status_header(true)
  cache_key = "#{CACHE_NAMESPACE}:#{self.class}:#{I18n.locale}:#{filter.collections.cache_key}:index:#{params}"

  my_response_cache cache_key, :json, public: true do
    filter.as_json(serialization_context, minor_version_param, {}).
      merge(success: true, message: nil)
  end
end

#flash_salesObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/api/v5/homes_controller.rb', line 104

def flash_sales
  cron = '59 8 * * *'
  next_reset_at = Fugit::Cron.parse("#{cron} #{Time::THAILAND_ZONE}").next_time.to_utc_time
  cache_key = "#{self.class}:index:flash_sales:#{FlashSale.maximum(:updated_at)}:#{next_reset_at}:#{I18n.locale}"

  my_response_cache(cache_key, :json) do
    flash_sale_ids = FlashSale.flash_sale_outlet_ids

    collection = if flash_sale_ids.blank?
                   Restaurant.none
                 else
                   Restaurant.where(id: flash_sale_ids).order("FIELD(id, #{flash_sale_ids.join(', ')})")
                 end

    filter = Api::V5::RestaurantsFilter.new(collection)

    filter.as_json(serialization_context, minor_version_param, { compact_mode: false }).
      merge(success: true, message: nil).
      merge(next_reset_at: next_reset_at)
  end
end

#section_1Object

2nd, CompactRestaurant



41
42
43
# File 'app/controllers/api/v5/homes_controller.rb', line 41

def section_1
  restaurant_section(__method__)
end

#section_10Object

13th section



100
101
102
# File 'app/controllers/api/v5/homes_controller.rb', line 100

def section_10
  restaurant_section(__method__)
end

#section_11Object

10th CompactRestaurant



84
85
86
# File 'app/controllers/api/v5/homes_controller.rb', line 84

def section_11
  restaurant_section(__method__)
end

#section_12Object

12th NOT USED by app



95
96
97
# File 'app/controllers/api/v5/homes_controller.rb', line 95

def section_12
  restaurant_section(__method__)
end

#section_13Object

11th (Restaurant card, not compact), special section, don't change this!



89
90
91
92
# File 'app/controllers/api/v5/homes_controller.rb', line 89

def section_13
  # restaurant_section(__method__)
  restaurant_section(__method__)
end

#section_14Object

3rd, special section, don't change this! restaurant card, not compact



47
48
49
# File 'app/controllers/api/v5/homes_controller.rb', line 47

def section_14
  restaurant_section(__method__)
end

#section_3Object

1st, CompactRestaurant



36
37
38
# File 'app/controllers/api/v5/homes_controller.rb', line 36

def section_3
  restaurant_section(__method__)
end

#section_4Object

top categories location 5th, Restaurant Tag



58
59
60
# File 'app/controllers/api/v5/homes_controller.rb', line 58

def section_4
  restaurant_section(__method__)
end

#section_5Object

4rd popular brand, Branch



52
53
54
# File 'app/controllers/api/v5/homes_controller.rb', line 52

def section_5
  restaurant_section(__method__)
end

#section_6Object

8th, CompactRestaurant



74
75
76
# File 'app/controllers/api/v5/homes_controller.rb', line 74

def section_6
  restaurant_section(__method__)
end

#section_7Object

top categories cuisine 5th, Restaurant Tag



64
65
66
# File 'app/controllers/api/v5/homes_controller.rb', line 64

def section_7
  restaurant_section(__method__)
end

#section_8Object

7th, CompactRestaurant



69
70
71
# File 'app/controllers/api/v5/homes_controller.rb', line 69

def section_8
  restaurant_section(__method__)
end

#section_9Object

9th, CompactRestaurant



79
80
81
# File 'app/controllers/api/v5/homes_controller.rb', line 79

def section_9
  restaurant_section(__method__)
end

#section_bannerObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/controllers/api/v5/homes_controller.rb', line 159

def section_banner
  home_section = HomeSection.find_by(section_type: 'section_banner')
  if home_section.present?
    banner = home_section.banner
    banner_data = banner.attributes.merge('mobile_versions' => fix_url(banner.mobile_versions),
                                          'desktop_versions' => fix_url(banner.desktop_versions),
                                          'mobile_retina_versions' => fix_url(banner.mobile_retina_versions),
                                          'desktop_retina_versions' => fix_url(banner.desktop_retina_versions))

    render json: {
      data: banner_data,
      success: true,
      message: nil,
      title: home_section.title,
      description: home_section.description,
      footer: home_section.footer_description,
      tag_line: home_section.tag_line,
      slug: home_section.slug,
      icon: home_section.icon,
      clevertap_event_name: 'Hero Banner',
      punch_line: home_section.punch_line,
      section_template: home_section.section_template,
      section_image: nil,
      active: home_section.active?,
    }
  end
end

#sectionsObject

Provide all section info



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/controllers/api/v5/homes_controller.rb', line 188

def sections
  cache_key = "#{self.class}:index:#{HomeSection.maximum(:updated_at)}:#{I18n.locale}"
  my_response_cache(cache_key, :json, public: true) do
    data = HomeSection.includes(:translations).reject do |section|
      HomeRestaurantSection::SECTION_ROUTE.invert[section.section_type.to_s.to_sym].nil?
    end

    options = {
      serialization_context: serialization_context,
      serializer: ActiveModel::Serializer::CollectionSerializer,
      adapter: :json_api,
      each_serializer: Api::V5::HomeRestaurantSectionSerializer,
    }

    result = render_to_string(json: ActiveModelSerializers::SerializableResource.new(data, options).as_json.
                                                                                                   merge(success: true, message: nil))
    JSON.parse result
  end
end

#settingsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/api/v5/homes_controller.rb', line 11

def settings
  device_position = if params[:setting_for] == 'mobile'
                      'position_mobile'
                    else
                      'position_web'
                    end

  cache_key = "#{self.class}:index:#{HomeSection.maximum(:updated_at)}:#{params}:#{I18n.locale}"
  my_response_cache(cache_key, :json, public: true) do
    data = HomeSection.includes(:translations).order("CASE WHEN section_type = 'section_banner' THEN 0 ELSE 1 END, #{device_position} ASC")
    options = {
      serialization_context: serialization_context,
      serializer: ActiveModel::Serializer::CollectionSerializer,
      adapter: :json_api,
      city_id: params[:city_id],
      each_serializer: Api::V5::HomeSectionSerializer,
    }

    result = render_to_string(json: ActiveModelSerializers::SerializableResource.new(data, options).as_json.
                              merge(success: true, message: nil))
    JSON.parse result
  end
end

#tagsObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/controllers/api/v5/homes_controller.rb', line 142

def tags
  home_section = HomeSection.find_by(slug: params[:slug])
  return render json: { data: [], success: false, message: 'Not found' } if home_section.blank?

  cache_key = "#{self.class}:index:#{home_section.cache_key}:#{I18n.locale}"
  my_response_cache(cache_key, :json, public: true) do
    options = {}
    restaurant_tags = home_section.restaurant_tags
    options[:serialization_context] = serialization_context
    options[:adapter] = :json_api
    options[:each_serializer] = Api::V5::RestaurantTagSerializer

    ActiveModelSerializers::SerializableResource.new(restaurant_tags, options).as_json.merge(success: true,
                                                                                             message: nil)
  end
end