Skip to content

Instantly share code, notes, and snippets.

@daneb
Created December 30, 2015 12:07
Show Gist options
  • Save daneb/0986c18205c0104b9e6b to your computer and use it in GitHub Desktop.
Save daneb/0986c18205c0104b9e6b to your computer and use it in GitHub Desktop.
Exposure Fail -
# Controller
module Web::Controllers::Home
class GetMetric
include Web::Action
expose :method_to_use, :query_string
handle_exception ArgumentError => :handle_no_query_string
def call(params)
raise ArgumentError.new('Failed to provide parameters') if params.env["QUERY_STRING"].empty?
raise ArgumentError.new('Failed to provide named metric') unless valid_query_string?
@method_to_use = "get_total_managed"
@query_string = { :start_date => '2015-01-01', :end_date => '2015-02-01' }
end
private
def handle_no_query_string(exception)
status 400, exception.message
end
def valid_query_string?
params.env["QUERY_STRING"].include?('metric=')
end
end
end
# View
module Web::Views::Home
class GetMetric
include Web::View
format :json
def render
result = MetricRepository.raw_all(method_to_use, query_string)
result = { :meta_data => [ :total => result[0][:total], :period => '2015-01-01_to_2015-02-01'] }
result
end
end
Web::Views::Home::GetMetric.render(format: :json)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment