Class: WebServiceDocumenter::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/web_service_documenter.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_uri, options) ⇒ Service

Returns a new instance of Service.



9
10
11
12
13
14
15
# File 'lib/web_service_documenter.rb', line 9

def initialize(base_uri, options)
  @base_uri       = base_uri
  @endpoint       = options["endpoint"]
  @params         = options["params"]
  @method         = (options["method"] || "get").downcase
  @example_params = create_example_params
end

Instance Method Details

#to_sObject



17
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
# File 'lib/web_service_documenter.rb', line 17

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 << "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