Class: WebServiceDocumenter::Service
- Inherits:
-
Object
- Object
- WebServiceDocumenter::Service
- Defined in:
- lib/web_service_documenter.rb
Instance Method Summary collapse
-
#initialize(base_uri, options) ⇒ Service
constructor
A new instance of Service.
- #to_s ⇒ Object
Constructor Details
#initialize(base_uri, options) ⇒ Service
Returns a new instance of Service.
9 10 11 12 13 14 15 16 |
# File 'lib/web_service_documenter.rb', line 9 def initialize(base_uri, ) @base_uri = base_uri @endpoint = ["endpoint"] @params = ["params"] @description = ["description"] @method = (["method"] || "get").downcase @example_params = create_example_params end |
Instance Method Details
#to_s ⇒ Object
18 19 20 21 22 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 |
# File 'lib/web_service_documenter.rb', line 18 def to_s body = "" url = "http://#{@base_uri}#{@endpoint}" uri = URI.parse(url) result = if @method =~ /post/ Net::HTTP.post_form(uri, @example_params) else http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri.path) request.set_form_data(@example_params) request = Net::HTTP::Get.new("#{uri.path}?#{request.body}") http.request(request) end if !result.kind_of?(Net::HTTPOK) raise "Couldn't perform request with url: #{url}" end json_response = JSON.parse(result.body) body << "\n" body << "==================================================\n" body << "URL: #{@endpoint} (#{@method.upcase})\n" body << "\n" body << "DESCRIPTION: #{@description}" << "\n \n" if @description body << "Request Params: \n" body << "\n" if @params && @params.any? @params.each do |key, value| body << " #{key} \n" @params[key].each do |k,v| body << " #{k}: #{v} \n" end end else body << " None" end body << "\n" body << "Response: \n" body << "\n" body << JSON.pretty_generate(json_response) body << "\n" body << "\n" body end |