Class: RubyForge::Client

Inherits:
Object show all
Defined in:
lib/gems/rubyforge-1.0.1/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
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge/client.rb', line 32

def initialize(proxy = nil)
  @debug_dev       = nil
  @ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE
  @cookie_manager  = CookieManager.new
  @agent_class     = Net::HTTP
end

Instance Attribute Details

#agent_classObject

Returns the value of attribute agent_class.



30
31
32
# File 'lib/gems/rubyforge-1.0.1/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/gems/rubyforge-1.0.1/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/gems/rubyforge-1.0.1/lib/rubyforge/client.rb', line 30

def ssl_verify_mode
  @ssl_verify_mode
end

Instance Method Details

#boundary_data_for(boundary, parameters) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge/client.rb', line 101

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


39
40
41
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge/client.rb', line 39

def cookie_store=(path)
  @cookie_manager = CookieManager.load(path)
end

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



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
95
96
97
98
99
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge/client.rb', line 55

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

  @cookie_manager[uri].each { |k,v|
    request['Cookie'] = v.to_s
  }

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

  if uri.scheme == 'https'
    http.use_ssl      = true
    http.verify_mode  = OpenSSL::SSL::VERIFY_NONE
  end

  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)
  (response.get_fields('Set-Cookie') || []).each do |raw_cookie|
    WEBrick::Cookie.parse_set_cookies(raw_cookie).each { |baked_cookie|
      baked_cookie.domain ||= url.host
      baked_cookie.path   ||= url.path
      @cookie_manager.add(uri, baked_cookie)
    }
  end

  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 = {}) ⇒ Object



49
50
51
52
53
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge/client.rb', line 49

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

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



43
44
45
46
47
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge/client.rb', line 43

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

#query_string_for(parameters) ⇒ Object



123
124
125
126
127
128
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge/client.rb', line 123

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


130
131
132
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge/client.rb', line 130

def save_cookie_store
  @cookie_manager.save!
end