Module: Sinatra::Pebblebed::Helpers

Defined in:
lib/pebblebed/sinatra.rb

Instance Method Summary collapse

Instance Method Details

#current_identityObject



35
36
37
# File 'lib/pebblebed/sinatra.rb', line 35

def current_identity
  pebbles.checkpoint.me
end

#current_identity_is?(identity_id) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
# File 'lib/pebblebed/sinatra.rb', line 45

def current_identity_is?(identity_id)
  require_identity
  unless (current_identity.id == identity_id || current_identity.god)
    halt 403, "Private resource"
  end
end

#current_sessionObject Also known as: checkpoint_session



26
27
28
# File 'lib/pebblebed/sinatra.rb', line 26

def current_session
  params[:session] || request.cookies['checkpoint.session']
end

#limit_offset_collection(collection, options) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/pebblebed/sinatra.rb', line 62

def limit_offset_collection(collection, options)
  limit = (options[:limit] || 20).to_i
  offset = (options[:offset] || 0).to_i
  collection = collection.limit(limit+1).offset(offset)
  last_page = (collection.size <= limit)
   = {:limit => limit, :offset => offset, :last_page => last_page}
  collection = collection[0..limit-1]
  [collection, ]
end

#part(partspec, params = {}) ⇒ Object

Render the markup for a part. A partspec takes the form “<kit>.<partname>”, e.g. “base.post”



9
10
11
12
# File 'lib/pebblebed/sinatra.rb', line 9

def part(partspec, params = {})
  params[:session] ||= current_session
  pebbles.parts.markup(partspec, params)
end

#parts_script_include_tagsObject



14
15
16
17
18
# File 'lib/pebblebed/sinatra.rb', line 14

def parts_script_include_tags
  @script_include_tags ||= pebbles.parts.javascript_urls.map do |url|
    "<script src=\"#{url.to_s}\"></script>"
  end.join
end

#parts_stylesheet_include_tagsObject



20
21
22
23
24
# File 'lib/pebblebed/sinatra.rb', line 20

def parts_stylesheet_include_tags
  @stylesheet_include_tags ||= pebbles.parts.stylesheet_urls.map do |url|
    "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"#{url.to_s}\">"
  end.join
end

#pebblesObject



31
32
33
# File 'lib/pebblebed/sinatra.rb', line 31

def pebbles
  @pebbles ||= ::Pebblebed::Connector.new(checkpoint_session, :host => request.host)
end

#require_godObject



52
53
54
55
# File 'lib/pebblebed/sinatra.rb', line 52

def require_god
  require_identity
  halt 403, "Current identity #{current_identity.id} is not god" unless current_identity.god
end

#require_identityObject



39
40
41
42
43
# File 'lib/pebblebed/sinatra.rb', line 39

def require_identity
  unless current_identity.respond_to?(:id)
    halt 403, "No current identity."
  end
end

#require_parameters(parameters, *keys) ⇒ Object



57
58
59
60
# File 'lib/pebblebed/sinatra.rb', line 57

def require_parameters(parameters, *keys)
  missing = keys.map(&:to_s) - (parameters ? parameters.keys : [])
  halt 409, "missing parameters: #{missing.join(', ')}" unless missing.empty?
end