Module: CubaApi::Utils

Defined in:
lib/cuba_api/utils.rb

Instance Method Summary collapse

Instance Method Details

#browser_only_cacheObject



46
47
48
49
50
# File 'lib/cuba_api/utils.rb', line 46

def browser_only_cache
  res[ 'Date' ] = DateTime.now.rfc2822
  res[ 'Expires' ] = "Fri, 01 Jan 1990 00:00:00 GMT"
  res[ 'Cache-Control' ] = "private, max-age=0, must-revalidate"
end

#browser_only_cache_no_storeObject



52
53
54
55
# File 'lib/cuba_api/utils.rb', line 52

def browser_only_cache_no_store
  browser_only_cache
  res[ 'Cache-Control' ] += ", no-store"
end

#content_type(mime) ⇒ Object



69
70
71
# File 'lib/cuba_api/utils.rb', line 69

def content_type( mime )
  res[ 'Content-Type' ] = mime if mime
end

#expires_in(minutes) ⇒ Object



40
41
42
43
44
# File 'lib/cuba_api/utils.rb', line 40

def expires_in( minutes )
  now = DateTime.now
  res[ 'Date' ] = now.rfc2822
  res[ 'Expires' ] = ( now + minutes / 1440.0 ).rfc2822
end

#last_modified(last) ⇒ Object



29
30
31
# File 'lib/cuba_api/utils.rb', line 29

def last_modified( last )
  res[ 'Last-Modified' ] = last.rfc2822
end

#modified_sinceObject



33
34
35
36
37
38
# File 'lib/cuba_api/utils.rb', line 33

def modified_since
  @modified_since ||=
    if date = env[ 'HTTP_IF_MODIFIED_SINCE' ]
      DateTime.parse( date )
    end
end

#no_cacheObject



62
63
64
65
66
67
# File 'lib/cuba_api/utils.rb', line 62

def no_cache
  res["Date"] = Time.now.rfc2822
  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_storeObject



57
58
59
60
# File 'lib/cuba_api/utils.rb', line 57

def no_cache_no_store
  no_cache
  res["Cache-Control"] += ", no-store"
end

#offset_n_limit(method, set) ⇒ Object



22
23
24
25
26
27
# File 'lib/cuba_api/utils.rb', line 22

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

#to_float(name, default = nil) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/cuba_api/utils.rb', line 4

def to_float( name, default = nil )
 v = req[ name ]
 if v
   v.to_f
 else
   default
 end
end

#to_int(name, default = nil) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/cuba_api/utils.rb', line 13

def to_int( name, default = nil )
  v = req[ name ]
  if v
    v.to_i
  else
    default
  end
end