Module: CubaApi::Utils
- Defined in:
- lib/cuba_api/utils.rb
Instance Method Summary collapse
- #browser_only_cache ⇒ Object
- #browser_only_cache_no_store ⇒ Object
- #content_type(mime) ⇒ Object
- #expires_in(minutes) ⇒ Object
-
#head ⇒ Object
matcher.
- #last_modified(last) ⇒ Object
- #modified_since ⇒ Object
- #no_cache ⇒ Object
- #no_cache_no_store ⇒ Object
-
#no_path ⇒ Object
matcher.
- #offset_n_limit(method, set) ⇒ Object
- #option ⇒ Object
- #rfc2616(time = DateTime.now) ⇒ Object
- #to_boolean(name, default = nil) ⇒ Object
-
#to_float(name, default = nil) ⇒ Object
params.
- #to_int(name, default = nil) ⇒ Object
Instance Method Details
#browser_only_cache ⇒ Object
70 71 72 73 74 |
# File 'lib/cuba_api/utils.rb', line 70 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 ⇒ Object
76 77 78 79 |
# File 'lib/cuba_api/utils.rb', line 76 def browser_only_cache_no_store browser_only_cache res[ 'Cache-Control' ] += ", no-store" end |
#content_type(mime) ⇒ Object
93 94 95 |
# File 'lib/cuba_api/utils.rb', line 93 def content_type( mime ) res[ 'Content-Type' ] = mime if mime end |
#expires_in(minutes) ⇒ Object
64 65 66 67 68 |
# File 'lib/cuba_api/utils.rb', line 64 def expires_in( minutes ) now = DateTime.now res[ 'Date' ] = rfc2616( now ) res[ 'Expires' ] = rfc2616( now + minutes / 1440.0 ) end |
#head ⇒ Object
matcher
10 11 12 |
# File 'lib/cuba_api/utils.rb', line 10 def head req.head? end |
#last_modified(last) ⇒ Object
53 54 55 |
# File 'lib/cuba_api/utils.rb', line 53 def last_modified( last ) res[ 'Last-Modified' ] = rfc2616( last ) if last end |
#modified_since ⇒ Object
57 58 59 60 61 62 |
# File 'lib/cuba_api/utils.rb', line 57 def modified_since @modified_since ||= if date = env[ 'HTTP_IF_MODIFIED_SINCE' ] DateTime.parse( date ) end end |
#no_cache ⇒ Object
86 87 88 89 90 91 |
# File 'lib/cuba_api/utils.rb', line 86 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 ⇒ Object
81 82 83 84 |
# File 'lib/cuba_api/utils.rb', line 81 def no_cache_no_store no_cache res["Cache-Control"] += ", no-store" end |
#no_path ⇒ Object
matcher
5 6 7 |
# File 'lib/cuba_api/utils.rb', line 5 def no_path Proc.new { env[ 'PATH_INFO' ].empty? } end |
#offset_n_limit(method, set) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/cuba_api/utils.rb', line 46 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 ⇒ Object
14 15 16 |
# File 'lib/cuba_api/utils.rb', line 14 def option req. end |
#rfc2616(time = DateTime.now) ⇒ Object
97 98 99 |
# File 'lib/cuba_api/utils.rb', line 97 def rfc2616( time = DateTime.now ) time.to_time.utc.rfc2822.sub( /.....$/, 'GMT') end |
#to_boolean(name, default = nil) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/cuba_api/utils.rb', line 37 def to_boolean( name, default = nil ) v = req[ name ] if v v == 'true' else default end end |
#to_float(name, default = nil) ⇒ Object
params
19 20 21 22 23 24 25 26 |
# File 'lib/cuba_api/utils.rb', line 19 def to_float( name, default = nil ) v = req[ name ] if v v.to_f else default end end |
#to_int(name, default = nil) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/cuba_api/utils.rb', line 28 def to_int( name, default = nil ) v = req[ name ] if v v.to_i else default end end |