23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
|
# File 'app/controllers/api_explorer/api_controller.rb', line 23
def execute
require 'net/http'
require 'coderay'
= params[:header][:name].zip(params[:header][:value])
.select!{|| ![0].empty? && ![1].empty?}
.map!{|| {:name=> [0], :value=> [1]}}
uri = URI::HTTP.build(:host=>request.host, :port=> request.port, :path=>'/' + params[:url])
http = Net::HTTP.new(uri.host, uri.port)
if params[:method].upcase == 'GET'
request = Net::HTTP::Get.new(uri.request_uri, {'Content-Type' =>'application/json'})
elsif params[:method].upcase == 'POST'
request = Net::HTTP::Post.new(uri.request_uri, {'Content-Type' =>'application/json'})
elsif params[:method].upcase == 'PUT'
request = Net::HTTP::Put.new(uri.request_uri, {'Content-Type' =>'application/json'})
elsif params[:method].upcase == 'DELETE'
request = Net::HTTP::Delete.new(uri.request_uri, {'Content-Type' =>'application/json'})
end
form_hash = params.except([:action, :controller, :header, :authentication_type, :auth])
form_hash.keys.each do |key|
if form_hash[key].is_a?(Hash)
h_and_key = to_form_name({ key=> form_hash[key] } ).split("=")
session['api_explorer_' + h_and_key.first] = h_and_key.second
else
session['api_explorer_' + key.to_s] = form_hash[key]
end
end
request.body = form_hash.to_hash.to_json
.each do ||
request[[:name]] = [:value]
end
if params[:authentication_type] == 'basic_auth'
request.basic_auth params[:auth][:basic_auth_user], params[:auth][:basic_auth_password]
elsif params[:authentication_type] == 'hash_url_auth'
string_to_convert = uri.to_s
hash_verification = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), params[:auth][:secret_key], string_to_convert)).strip
request[params[:auth][:header_field]] = hash_verification
end
response = http.request(request)
raw_response = response.body
response_html = ''
if response.['Content-Type'].include? 'application/json'
tokens = CodeRay.scan(JSON.pretty_generate(JSON.parse(raw_response)), :json)
response_html = tokens.div
elsif response.['Content-Type'].include? 'application/xml'
tokens = CodeRay.scan(raw_response, :xml)
response_html = tokens.div
elsif response.['Content-Type'].include? 'text/html'
tokens = CodeRay.scan(raw_response, :html)
response_html = tokens.div
else
response_html = '<div>' + raw_response + '</div>'
end
request_html = CodeRay.scan(request.to_yaml, :yaml).div
curr_time = DateTime.now
timestamp = curr_time.strftime('%Y%m%d%H%M%S%L')
history_html = render_to_string 'api_explorer/api/history', :locals=>{:request_html=>request_html,
:response_html=>response_html, :timestamp=>timestamp}, :layout=>false
render :json =>{ :response_html=> response_html , :request_html => request_html,
:history_html=>history_html, :date=> curr_time.strftime('%H:%M:%S'),
:timestamp => timestamp,
:http_method=>params[:method].upcase, :request_url=>params[:url]
} , :layout=>false
end
|