Module: Hippo::API::HelperMethods
- Defined in:
- lib/hippo/api/helper_methods.rb
Instance Method Summary collapse
- #csrf_token ⇒ Object
- #data ⇒ Object
- #error_as_json ⇒ Object
- #find_template(views, name, engine, &block) ⇒ Object
- #hippo_api_url ⇒ Object
- #hippo_application_title ⇒ Object
- #javascript_tags(*entries) ⇒ Object
- #json_reply(response) ⇒ Object
- #request_origin ⇒ Object
Instance Method Details
#csrf_token ⇒ Object
15 16 17 |
# File 'lib/hippo/api/helper_methods.rb', line 15 def csrf_token Rack::Csrf.csrf_token(env) end |
#data ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/hippo/api/helper_methods.rb', line 34 def data if request.content_type == 'application/json' @json_data ||= ( body = request.body.read body.present? ? Oj.load(body) : {} ) else request.params end end |
#error_as_json ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/hippo/api/helper_methods.rb', line 23 def error_as_json error = request.env['sinatra.error'] Hippo.logger.warn error. Hippo.logger.warn error.backtrace.join("\n ") API.to_json( success: false, errors: { exception: error. }, message: error. ) end |
#find_template(views, name, engine, &block) ⇒ Object
54 55 56 |
# File 'lib/hippo/api/helper_methods.rb', line 54 def find_template(views, name, engine, &block) views.each{ |v| super(v, name, engine, &block) } end |
#hippo_api_url ⇒ Object
19 20 21 |
# File 'lib/hippo/api/helper_methods.rb', line 19 def hippo_api_url Hippo.config.api_path end |
#hippo_application_title ⇒ Object
4 5 6 |
# File 'lib/hippo/api/helper_methods.rb', line 4 def hippo_application_title Extensions.controlling.title end |
#javascript_tags(*entries) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/hippo/api/helper_methods.rb', line 8 def (*entries) Root.webpack.wait_until_available entries.map { |entry| "<script src=\"#{Root.webpack.host}/assets/#{Root.webpack.file(entry)}\"></script>" }.join("\n") end |
#json_reply(response) ⇒ Object
49 50 51 52 |
# File 'lib/hippo/api/helper_methods.rb', line 49 def json_reply(response) content_type 'application/json' API.to_json(response) end |
#request_origin ⇒ Object
45 46 47 |
# File 'lib/hippo/api/helper_methods.rb', line 45 def request_origin @request_origin ||= env['HTTP_ORIGIN'] end |