Module: MSFL::Sinatra

Defined in:
lib/msfl/sinatra.rb

Defined Under Namespace

Modules: Helpers

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.valid_filterObject

Returns the value of attribute valid_filter.



6
7
8
# File 'lib/msfl/sinatra.rb', line 6

def valid_filter
  @valid_filter
end

Class Method Details

.dataset_from(params) ⇒ MSFL::Datasets::Base, Nil

Extracts the dataset name from the Sinatra params. It then returns a new instance of the specified dataset.

Parameters:

  • params (Hash)

    the Sinatra request params

Returns:

  • (MSFL::Datasets::Base, Nil)

    a new instance of the specified dataset, if it can be found, otherwise nil



27
28
29
30
31
# File 'lib/msfl/sinatra.rb', line 27

def dataset_from(params)
  dataset_name = params[:dataset].to_sym unless params[:dataset].nil?
  dataset_name ||= nil
  Datasets::Base.dataset_from dataset_name
end

.parse_filter_from(params) ⇒ Object

Extracts the filter clause from a Sinatra request params hash and parsers the filter

Parameters:

  • params (Hash)

    the Sinatra request params

Returns:

  • (Object)

    the Ruby-ified MSFL filter



16
17
18
19
20
# File 'lib/msfl/sinatra.rb', line 16

def parse_filter_from(params)
  filter = params[:filter] || params["filter"]
  filter = filter.to_json if filter.is_a?(Hash) #normally it's a string
  MSFL::Parsers::JSON.parse filter
end

.registered(app) ⇒ Object

Sinatra specific registration hook



88
89
90
# File 'lib/msfl/sinatra.rb', line 88

def self.registered(app)
  app.helpers Helpers if app.respond_to?(:helpers)
end

.validate(params) ⇒ Bool

Validate the MSFL filter in the Sinatra request’s params hash

Parameters:

  • params (Hash)

    the Sinatra request params

Returns:

  • (Bool)


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

def validate(params)
  validator = validator_from params
  parsed_filter = parse_filter_from params
  result = validator.validate parsed_filter
  @valid_filter = parsed_filter if result
  result
end

.validator_from(params) ⇒ MSFL::Validators::Semantic

Creates a semantic validator instance that is ready to validate the dataset

Parameters:

  • params (Hash)

    the Sinatra request params

Returns:



37
38
39
# File 'lib/msfl/sinatra.rb', line 37

def validator_from(params)
  MSFL::Validators::Semantic.new dataset_from(params)
end