module CubaApi::Utils

Public Instance Methods

browser_only_cache() click to toggle source
# File lib/cuba_api/utils.rb, line 80
def browser_only_cache
  res[ 'Date' ] = rfc2616
  res[ 'Expires' ] = "Fri, 01 Jan 1990 00:00:00 GMT"
  res[ 'Cache-Control' ] = "private, max-age=0, must-revalidate"
end
browser_only_cache_no_store() click to toggle source
# File lib/cuba_api/utils.rb, line 86
def browser_only_cache_no_store
  browser_only_cache
  res[ 'Cache-Control' ] += ", no-store"
end
content_type( mime ) click to toggle source
# File lib/cuba_api/utils.rb, line 103
def content_type( mime )
  res[ 'Content-Type' ] = mime if mime
end
expires_in( minutes ) click to toggle source
# File lib/cuba_api/utils.rb, line 74
def expires_in( minutes )
  now = DateTime.now
  res[ 'Date' ] = rfc2616( now )
  res[ 'Expires' ] = rfc2616( now + minutes / 1440.0 )
end
head() click to toggle source

matcher

# File lib/cuba_api/utils.rb, line 10
def head
  req.head?
end
last_modified( last ) click to toggle source
# File lib/cuba_api/utils.rb, line 60
def last_modified( last )
  if last
    res[ 'Last-Modified' ] = rfc2616( last )
    res[ 'Cache-Control' ] = "private, max-age=0, must-revalidate"
  end
end
modified_since() click to toggle source
# File lib/cuba_api/utils.rb, line 67
def modified_since
  @modified_since ||=
    if date = env[ 'HTTP_IF_MODIFIED_SINCE' ]
      DateTime.parse( date )
    end
end
no_body( status ) click to toggle source

convenient method for status only responses

# File lib/cuba_api/utils.rb, line 19
def no_body( status )
  res.status = ::Rack::Utils.status_code( status )
  res.write ::Rack::Utils::HTTP_STATUS_CODES[ res.status ]
  res['Content-Type' ] = 'text/plain'
end
no_cache() click to toggle source
# File lib/cuba_api/utils.rb, line 96
def no_cache
  res["Date"] = rfc2616
  res["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
  res["Pragma"] = "no-cache"
  res["Cache-Control"] = "no-cache, must-revalidate"
end
no_cache_no_store() click to toggle source
# File lib/cuba_api/utils.rb, line 91
def no_cache_no_store
  no_cache
  res["Cache-Control"] += ", no-store"
end
no_path() click to toggle source

matcher

# File lib/cuba_api/utils.rb, line 5
def no_path
  Proc.new { env[ 'PATH_INFO' ].empty? }
end
offset_n_limit( method, set ) click to toggle source
# File lib/cuba_api/utils.rb, line 53
def offset_n_limit( method, set )
  count = set.count
  offset = to_int( 'offset' ).to_i
  limit = ( to_int( 'count' ) || count ) - 1 + offset
  { method => set[ offset..limit ], :offset => offset, :total_count => count }
end
option() click to toggle source
# File lib/cuba_api/utils.rb, line 14
def option
  req.options?
end
rfc2616( time = DateTime.now ) click to toggle source
# File lib/cuba_api/utils.rb, line 107
def rfc2616( time = DateTime.now )
  time.to_time.utc.rfc2822.sub( /.....$/, 'GMT')
end
to_boolean( name, default = nil ) click to toggle source
# File lib/cuba_api/utils.rb, line 44
def to_boolean( name, default = nil )
  v = req[ name ]
  if v
    v == 'true'
  else
    default
  end
end
to_float( name, default = nil ) click to toggle source

params

# File lib/cuba_api/utils.rb, line 26
def to_float( name, default = nil )
 v = req[ name ]
 if v
   v.to_f
 else
   default
 end
end
to_int( name, default = nil ) click to toggle source
# File lib/cuba_api/utils.rb, line 35
def to_int( name, default = nil )
  v = req[ name ]
  if v
    v.to_i
  else
    default
  end
end