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

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

Instance Method Details

#to_sObject



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

def to_s
  body = ""

  url = "http://#{@base_uri}#{@endpoint}"
  uri = URI.parse(url)

  result = if @method =~ /post/
    if @multipart == true
      request = Net::HTTP::Post::Multipart.new uri.path, transform_multipart_example_params

      Net::HTTP.start(uri.host, uri.port) do |http|
        http.request(request)
      end
    else
      Net::HTTP.post_form(uri, @example_params)
    end
  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

#transform_multipart_example_paramsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/web_service_documenter.rb', line 19

def transform_multipart_example_params
  new_params = {}

  @example_params.map do |key, value|
    if value =~ /file\((.*)\,(.*)\,(.*)\)/
      new_params[key] = UploadIO.new($1.strip, $2.strip, $3.strip)
    else
      new_params[key] = value
    end
  end

  new_params
end