File: net_http.rb

package info (click to toggle)
ruby-fogbugz 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 132 kB
  • sloc: ruby: 199; makefile: 3
file content (44 lines) | stat: -rw-r--r-- 1,082 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
require 'cgi'
require 'net/https'
require 'net/http/post/multipart'

module Fogbugz
  module Adapter
    module HTTP
      class NetHttp
        attr_accessor :root_url, :requester

        def initialize(options = {})
          @root_url = options[:uri]
          @ca_file = options[:ca_file]
        end

        def build_request(uri, params)
          return Net::HTTP::Post::Multipart.new(uri.request_uri, params) if params.key? :File1

          request = Net::HTTP::Post.new(uri.request_uri)
          request.set_form_data(params)
          request
        end

        def request(action, options)
          uri = URI("#{@root_url}/api.asp")

          params = { 'cmd' => action }
          params.merge!(options[:params])

          request = build_request(uri, params)

          http = Net::HTTP.new(uri.host, uri.port)
          if @root_url.start_with? 'https'
            http.use_ssl = true
            http.ca_file = @ca_file
          end

          response = http.start { |h| h.request(request) }
          response.body
        end
      end
    end
  end
end