Class: RazorRisk::Cassini::Applications::RouteVerbAdaptors::UserProfiles::ItemGet

Inherits:
RESTFramework::VerbHandler
  • Object
show all
Includes:
Cassini::Mixin::RazorResponseValidator, Cassini::Util::ConversionUtil, Razor::Connectivity::Razor3::EntityConnectors, RazorRisk::Core::Diagnostics::Logger
Defined in:
lib/razor_risk/cassini/applications/route_verb_adaptors/user_profiles/item_get.rb

Overview

########################################################################## classes

Constant Summary collapse

HTTP_VERB =
:get
HTTP_ACCEPTS =
[
    'application/xml',
    'application/json',
    'text/xml',
]
QUERY_PARAMETERS =
[]
ROUTE_VARIABLES =
[ 'id' ]

Instance Method Summary collapse

Constructor Details

#initialize(*args, **options) ⇒ ItemGet

########################################################## methods



63
64
65
66
67
# File 'lib/razor_risk/cassini/applications/route_verb_adaptors/user_profiles/item_get.rb', line 63

def initialize *args, **options

    @connector_class = options[:connector_class] || UserProfilesConnector
    super
end

Instance Method Details

#handle(env, params, request, response) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/razor_risk/cassini/applications/route_verb_adaptors/user_profiles/item_get.rb', line 69

def handle env, params, request, response

    trace(
        ParamNames[ :env, :params, :request, :response ],
        env, params, request, response
    )

    user_id = check_option(
        params,
        'id',
        strip_str_whitespace: true,
        reject_empty: true,
        nothrow: true,
    ) || error(HTTP_STATUS_NAMES::UNPROCESSABLE_ENTITY, "Invalid User ID")

    # FIXME: Validate that all characters in the User ID are alphanumerics,

    # spaces, or underscores.

    #

    # This check is temporary and does not grantee SQL safety. This

    # must be addressed as a larger issue around sanitization of user

    # inputs to Razor.

    settings.razor_requester.stopgap_sql_string_validator_fix_me user_id do
        error(
            HTTP_STATUS_NAMES::UNPROCESSABLE_ENTITY,
            'User ID contained invalid characters, may only be alphanumerics, spaces, or underscores.'
        )
    end

    ec = @connector_class.new(
        settings.razor_requester,
        credentials: get_required_credentials,
    )

    qr = ec.(
        user_id,
        indicate_result_by: :qualified_result,
    )
    log :debug1, "qr(#{qr.class})='#{qr}'"

    validate_qualified_razor_response qr

    unless qr.succeeded?
        if qr.result.nil?
            error HTTP_STATUS_NAMES::NOT_FOUND, 'User Profile could not be found'
        else
            reasons = qr.failure_qualifier.reasons.join("\n")
            log :warning, "Failed to retrieve User Profile for '#{user_id}'; #{reasons}"
            error(
                HTTP_STATUS_NAMES::INTERNAL_SERVER_ERROR,
                'Oops! Something went wrong'
            )
        end
    end

    status HTTP_STATUS_NAMES::OK

    if request.accept?('application/xml')
        log :debug1, 'application/xml'
        content_type 'application/xml'
        qr.result.to_s
    elsif request.accept?('text/xml')
        log :debug1, 'text/xml'
        content_type 'text/xml'
        qr.result.to_s
    elsif request.accept?('application/json')
        log :debug1, 'application/json'
        content_type 'application/json'
        convert_XML_to_JSON qr.result, { scheme: :gdata }
    else
        log :violation, "unexpected failure to match given 'Accept' header '#{request.accept}'"
        error(
            HTTP_STATUS_NAMES::INTERNAL_SERVER_ERROR,
            'Oops! Something went wrong'
        )
    end
end