Module: Hippo::API::HelperMethods

Defined in:
lib/hippo/api/helper_methods.rb

Instance Method Summary collapse

Instance Method Details

#csrf_tokenObject



15
16
17
# File 'lib/hippo/api/helper_methods.rb', line 15

def csrf_token
    Rack::Csrf.csrf_token(env)
end

#dataObject



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_jsonObject



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.message
    Hippo.logger.warn error.backtrace.join("\n    ")
    API.to_json(
        success: false,
        errors:  { exception: error.message },
        message: error.message
    )
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_urlObject



19
20
21
# File 'lib/hippo/api/helper_methods.rb', line 19

def hippo_api_url
    Hippo.config.api_path
end

#hippo_application_titleObject



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 javascript_tags(*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_originObject



45
46
47
# File 'lib/hippo/api/helper_methods.rb', line 45

def request_origin
    @request_origin ||= env['HTTP_ORIGIN']
end