Class: MetaProject::Project::XForge::Session

Inherits:
Object
  • Object
show all
Includes:
HTTP::Multipart
Defined in:
lib/meta_project/project/xforge/session.rb

Overview

A Session object allows authenticated interaction with a Project, such as releasing files.

A Session object can be obtained via Project.login

Defined Under Namespace

Classes: Processor

Constant Summary collapse

PACKAGE_ID_PATTERN =
%r{name="package_id"
\s+
value="([^"]+)"
.*?
name="package_name"
\s+
value="([^"]+)"}mxo

Constants included from HTTP::Multipart

HTTP::Multipart::BOUNDARY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HTTP::Multipart

#post_data, #post_multipart

Constructor Details

#initialize(host, project, cookie) ⇒ Session

:nodoc:



34
35
36
37
38
# File 'lib/meta_project/project/xforge/session.rb', line 34

def initialize(host, project, cookie) # :nodoc:
  @host = host
  @project = project
  @request_headers = { "Cookie" => cookie }.merge(DEFAULT_POST_OPTIONS)
end

Instance Attribute Details

#request_headersObject (readonly)

:nodoc:



32
33
34
# File 'lib/meta_project/project/xforge/session.rb', line 32

def request_headers
  @request_headers
end

Instance Method Details

#package_id(name = nil) ⇒ Object

This will get the package_id for the project. This accepts an optional name of the package that will be searched for results. A given session will only work for one package.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/meta_project/project/xforge/session.rb', line 43

def package_id(name = nil)
  unless @package_id
    release_uri = "http://#{@host}/frs/admin/?group_id=#{@project.group_id}"
    release_data = better_open(release_uri, @request_headers).read
    packages = release_data.scan(PACKAGE_ID_PATTERN)
    first = packages[0][0]
    packages = Hash[*packages.map { |el| el.reverse }.flatten]

    if name
      @package_id = packages[name]
    else
      @package_id = first
    end
    
    unless @package_id
      File.open("package_id.html", "w") {|io| io.write(release_data)}
      raise "Couldn't get package_id from #{release_uri}. I was looking for /#{package_id_pattern.source}/. HTML saved to package_id.html for debugging." 
    end
  end
  @package_id
end

#publish_news(subject, details) ⇒ Object

Publish news relating to a project and a package.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/meta_project/project/xforge/session.rb', line 151

def publish_news(subject, details)
  puts "About to publish news"
  puts "Subject: '#{subject}'"
  puts "Details:"
  puts details
  puts ""
      
  release_response = Net::HTTP.start(@host, 80) do |http|
    query_hash = {
      "group_id" => @project.group_id,
      "post_changes" => "y",
      "summary" => subject,
      "details" => details
    }
      
    target = "/news/submit.php"
    headers = @request_headers.merge("Content-Type" => "multipart/form-data; boundary=#{BOUNDARY}")

    http.post(target, post_data(query_hash), headers)
  end
  puts "Done!"
end

#release(release_name, filenames, release_notes = "", release_changes = "", package_name = nil, preformatted = true, processor = Processor::ANY) ⇒ Object

Creates a new release containing the files specified by filenames (Array) and named release_name. Optional parameters are processor (which should be one of the Processor constants), release_notes, release_changes and preformatted which will appear on the releas page of the associated project. The package_name parameter will help choose from the possible multiple packages for a release. TODO: consider using a hash based signature with symbols. This is getting big!



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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/meta_project/project/xforge/session.rb', line 74

def release(release_name, filenames, release_notes = "",
            release_changes = "", package_name = nil,
            preformatted = true, processor = Processor::ANY)
  release_date = Time.now.strftime("%Y-%m-%d %H:%M")
  release_id = nil

  puts "About to release '#{release_name}'"
  puts "Files:"
  puts "  " + filenames.join("\n  ")
  puts "\nRelease Notes:\n"
  puts release_notes.split(/\n/)[0..10].join("\n")
  puts "\nRelease Changes:\n"
  puts release_changes.split(/\n/)[0..10].join("\n")
  puts "\nRelease Settings:\n"
  puts "Preformatted: #{preformatted}"
  puts "Processor: #{processor}"
  puts "\nStarting release..."

  xfiles = filenames.collect{|filename| XFile.new(filename)}      
  xfiles.each_with_index do |xfile, i|
    first_file = (i == 0)
    puts "Releasing #{xfile.basename}..."
    release_response = Net::HTTP.start(@host, 80) do |http|
      query_hash = if first_file then
        {
          "group_id" => @project.group_id,
          "package_id" => package_id(package_name),
          "type_id" => xfile.bin_type_id,
          "processor_id" => processor,

          "release_name" => release_name,
          "release_date" => release_date,
          "release_notes" => release_notes,
          "release_changes" => release_changes,
          "preformatted" => preformatted ? "1" : "0",
          "submit" => "1"
        }
      else
        {
          "group_id" => @project.group_id,
          "package_id" => package_id(package_name),
          "type_id" => xfile.bin_type_id,
          "processor_id" => processor,

          "step2" => "1",
          "release_id" => release_id,
          "submit" => "Add This File"
        }
      end

      form = [
        "--#{BOUNDARY}",
        %Q(Content-Disposition: form-data; name="userfile"; ) +
          %Q(filename="#{xfile.basename}"),
        "Content-Type: application/octet-stream",
        "Content-Transfer-Encoding: binary",
        "", xfile.data, ""
      ]

      data = post_data(form, query_hash)

      headers = @request_headers.merge("Content-Type" => "multipart/form-data; boundary=#{BOUNDARY}")


      target = first_file ? "/frs/admin/qrs.php" : "/frs/admin/editrelease.php"
      http.post(target, data, headers)
    end

    if first_file then
      release_id = release_response.body[/release_id=(\d+)/, 1]
      raise("Couldn't get release id") unless release_id
    end
  end
  puts "Done!"
end