Class: RubyForge::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy = nil) ⇒ Client

Returns a new instance of Client.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubyforge/client.rb', line 32

def initialize(proxy = nil)
  @debug_dev       = nil
  @ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE
  if proxy
    begin
      proxy_uri = URI.parse(proxy)
      @agent_class = Net::HTTP::Proxy(proxy_uri.host,proxy_uri.port)
    rescue URI::InvalidURIError
    end
  end
  @agent_class ||= Net::HTTP
end

Instance Attribute Details

#agent_classObject

Returns the value of attribute agent_class.



30
31
32
# File 'lib/rubyforge/client.rb', line 30

def agent_class
  @agent_class
end

#debug_devObject

Returns the value of attribute debug_dev.



30
31
32
# File 'lib/rubyforge/client.rb', line 30

def debug_dev
  @debug_dev
end

#ssl_verify_modeObject

Returns the value of attribute ssl_verify_mode.



30
31
32
# File 'lib/rubyforge/client.rb', line 30

def ssl_verify_mode
  @ssl_verify_mode
end

Instance Method Details

#boundary_data_for(boundary, parameters) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rubyforge/client.rb', line 94

def boundary_data_for(boundary, parameters)
  parameters.sort_by {|k,v| k.to_s }.map { |k,v|
    parameter = "--#{boundary}\r\nContent-Disposition: form-data; name=\"" +
        WEBrick::HTTPUtils.escape_form(k.to_s) + "\""

    if v.respond_to? :path
      parameter += "; filename=\"#{File.basename(v.path)}\"\r\n"
      parameter += "Content-Transfer-Encoding: binary\r\n"
      parameter += "Content-Type: text/plain"
    end
    parameter += "\r\n\r\n"

    if v.respond_to? :path
      parameter += v.read
    else
      parameter += v.to_s
    end

    parameter
  }.join("\r\n") + "\r\n--#{boundary}--\r\n"
end

#execute(request, uri, parameters = {}, headers = {}, userconfig = nil) ⇒ Object



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
# File 'lib/rubyforge/client.rb', line 57

def execute(request, uri, parameters = {}, headers = {}, userconfig = nil)
  {
    'content-type' => 'application/x-www-form-urlencoded'
  }.merge(headers).each { |k,v| request[k] = v }

  http = agent_class.new( uri.host, uri.port )

  if uri.scheme == 'https' && uri.host !~ /localhost/
   http.use_ssl      = true
   http.verify_mode  = OpenSSL::SSL::VERIFY_NONE
  end
  
  request.basic_auth(userconfig["username"], userconfig["password"])

  request_data = case request['Content-Type']
                 when /boundary=(.*)$/
                   boundary_data_for($1, parameters)
                 else
                   query_string_for(parameters)
                 end
  request['Content-Length'] = request_data.length.to_s

  response = http.request(request, request_data)

  return response.body if response.class <= Net::HTTPSuccess

  if response.class <= Net::HTTPRedirection
    location = response['Location']
    unless location =~ /^http/
      location = "#{uri.scheme}://#{uri.host}#{location}"
    end
    uri = URI.parse(location)

    execute(agent_class::Get.new(uri.request_uri), uri)
  end
end

#get_content(uri, query = {}, headers = {}, userconfig = nil) ⇒ Object



51
52
53
54
55
# File 'lib/rubyforge/client.rb', line 51

def get_content(uri, query = {}, headers = {}, userconfig = nil)
  uri = URI.parse(uri) unless uri.is_a?(URI)
  request = agent_class::Get.new(uri.request_uri)
  execute(request, uri, query, headers, userconfig)
end

#post_content(uri, form = {}, headers = {}, userconfig = nil) ⇒ Object



45
46
47
48
49
# File 'lib/rubyforge/client.rb', line 45

def post_content(uri, form = {}, headers = {}, userconfig = nil)
  uri = URI.parse(uri) unless uri.is_a?(URI)
  request = agent_class::Post.new(uri.request_uri)
  execute(request, uri, form, headers, userconfig)
end

#query_string_for(parameters) ⇒ Object



116
117
118
119
120
121
# File 'lib/rubyforge/client.rb', line 116

def query_string_for(parameters)
  parameters.sort_by {|k,v| k.to_s }.map { |k,v|
    k && [  WEBrick::HTTPUtils.escape_form(k.to_s),
            WEBrick::HTTPUtils.escape_form(v.to_s) ].join('=')
  }.compact.join('&')
end