In the application, the user can fill in 3 fields (and 1 hidden field) and click 'load response', and this will make an ajax API call to an external API. The call to the external API is in a wrapper in lib. This wrapper stores additional required fields that the user doesn't need to enter, but are required by the API.
I've seen a few other posts about testing API calls, but I'm stumped on comparing them to this one and learning something from it, because of that extra wrapper.
In the view:
data = ->
string_ssid = '#{@bar.ssID}'
{
approval_code: $('#approval_code').val()
rejection_code: $('#rejection_code').val()
rejection_message: $('#rejection_message').val()
ssid: string_ssid
}
ajax_call = ->
$('#load_response').on 'click', ->
$.ajax
url: '/fooview/api_response'
data: data()
success: (d) ->
$('#api_response').html '<p>' + JSON.stringify(d) + '</p>'
return
error: (d, status, e) ->
$('#api_response').html '<p>Error: ' + e + ', ' + status + '</p>'
return
return
return
Routes:
get '/baz/api_response', to: 'baz#api_response'
Controller:
def api_response
render json: BazWrapper.get_api_response(params)
end
Wrapper:
module BazWrapper
@base_url = Enviro.get(:foo) + '/message/json'
def self.get_api_response(params)
query_values = {
api_key: Enviro.get(:foo, :api_key),
approval_code: params[:approval_code],
rejection_code: params[:rejection_code],
rejection_message: params[:rejection_message],
ssid: params[:ssid],
(..other required params hard-wired..)
}
HTTParty.post(
URI.encode(@base_url),
query: query_values
)
end
end
Aucun commentaire:
Enregistrer un commentaire