Class: ViewCategoriesAction

Inherits:
WebAction show all
Includes:
Assert
Defined in:
lib/ribit/action.rb

Direct Known Subclasses

CancelCategoryAction

Constant Summary collapse

ID =
'categories'
PAGE_CACHE_ID =
'categories'

Instance Attribute Summary

Attributes inherited from WebAction

#id

Instance Method Summary collapse

Methods included from Assert

assert, #assert, assert_nil, #assert_nil, #assert_not_nil, assert_not_nil, raise_exception

Methods inherited from WebAction

#add_headers

Constructor Details

#initialize(ribitData, ribitConfig, pageCache) ⇒ ViewCategoriesAction

Returns a new instance of ViewCategoriesAction.



761
762
763
764
765
766
767
768
769
770
# File 'lib/ribit/action.rb', line 761

def initialize( ribitData, ribitConfig, pageCache )
  assert_not_nil( ribitData, 'RibitData is nil' )
  assert_not_nil( ribitConfig )
  
  @id = ID
  @ribitData = ribitData
  @ribitConfig = ribitConfig
  @pageCache = pageCache
  @logger = RibitLogger.new( ViewCategoriesAction )
end

Instance Method Details

#decorate_page(page) ⇒ Object



773
774
775
776
777
# File 'lib/ribit/action.rb', line 773

def decorate_page( page )
  # set baseUrl etc.
  add_headers( page )
  return false
end

#get_cached_pageObject



828
829
830
831
832
833
834
# File 'lib/ribit/action.rb', line 828

def get_cached_page
  if ( @pageCache == nil )
    return nil
  end
  
  return @pageCache.get( PAGE_CACHE_ID )
end

#run(pageRequest, pageResponse) ⇒ Object



780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# File 'lib/ribit/action.rb', line 780

def run( pageRequest, pageResponse )
  super( pageRequest, pageResponse )
  
  cachedPage = get_cached_page
  if ( cachedPage != nil )
    @logger.debug( 'Found categories page from cache' )
    
    lastModified = pageRequest.get_header_field( 'If-Modified-Since' )
    if ( lastModified != nil )
      @logger.debug( "Found If-Modified-Since field from HTTP headers" )
      time = Time.httpdate( lastModified )
      
      # NOTE: MODIFIED time contains also fraction of seconds but httpdate
      #       doesn't have them => we have to compare full seconds
      if ( cachedPage.is_time_same_or_after( lastModified ) )
        @logger.debug( "Cached page is up to date" )
        pageResponse.status = 304
        return
      end
    end
    
    pageResponse.set_body( cachedPage.data )
    pageResponse.set_content_type( cachedPage.contentType )
    
    pageResponse.set_header_field( 'Cache-Control', 'public, must-revalidate, proxy-revalidate' )
    pageResponse.set_header_field( 'Last-Modified', cachedPage.lastModfiedHttpdate )
  
  else
    page = CategoriesPage.new( @ribitData, @ribitConfig )
    decorate_page( page )
      
    page.output_page( pageResponse )
    
    if ( @pageCache != nil  )
      # we can safely insert data to cache
      @logger.debug( "Inserting categories page data to cache" )
      
      cachedPage = CachedPage.new( pageResponse.get_body, pageResponse.get_content_type )       
      @pageCache.add( PAGE_CACHE_ID, cachedPage )
      
      pageResponse.set_header_field( 'Cache-Control', 'public, must-revalidate, proxy-revalidate' )
      pageResponse.set_header_field( 'Last-Modified', cachedPage.lastModfiedHttpdate )
    end
  end
  
end