Changeset 13

Show
Ignore:
Timestamp:
07/08/06 23:55:14 (3 years ago)
Author:
imperator
Message:

Added rdoc
Removed the fetch method. Entry.new without save does this already.
Started the RAA.search method
Went to 2 space indents

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • www-raa/lib/www/raa.rb

    r12 r13  
    33 
    44module WWW 
    5   module RAA 
    6       HOME_PAGE = 'http://raa.ruby-lang.org' 
     5  module RAA 
     6    HOME_PAGE = 'http://raa.ruby-lang.org' 
    77 
    8       def update_entry(project,&block) 
    9         Entry.new(project,&block).save 
     8    # A shortcut for Entry.new + Entry#save. 
     9    # 
     10    def update_entry(project, &block) 
     11      Entry.new(project, &block).save 
     12    end 
     13 
     14    # Returns an array of Entry objects based on a search of the RAA.  These 
     15    # are the same results that would be returned if you had used the search 
     16    # feature on the RAA itself. 
     17    #-- 
     18    # TODO: finish 
     19    def search(project) 
     20      mech = WWW::Mechanize.new 
     21      mech.get(HOME_PAGE + "/search.rhtml?search=#{project}") 
     22    end 
     23 
     24    module_function :update_entry 
     25       
     26    # The Entry class represents a project entry on the RAA. 
     27    # 
     28    class Entry 
     29 
     30      # Creates and returns a new RAA::Entry object for the +project+ that 
     31      # you specify, or raise. This will grab the form data from the ruby-lang 
     32      # but it will NOT post any updates. 
     33      # 
     34      def initialize(project) 
     35        @project = project 
     36        @mech = WWW::Mechanize.new 
     37        @mech.get(HOME_PAGE + "/update.rhtml?name=#{project}") 
     38 
     39        if @mech.page.body =~ /project not found/i 
     40          raise ArgumentError, "The '#{project}' project could not be found" 
     41        end 
     42 
     43        @form = @mech.page.forms[1] 
     44        yield self if block_given? 
     45      end 
     46         
     47      # Returns an array of form field names. 
     48      #-- 
     49      # This was provided because we use method_missing instead of hard coded 
     50      # instance variables.  The field names are, in effect, the instance 
     51      # variables. 
     52      # 
     53      def fields 
     54        @form.fields.map{ |field| field.name } 
    1055      end 
    1156 
    12       module_function :update_entry 
    13        
    14       class Entry 
     57      # Submits the form data to the RAA, updating the entry.  Note that a 
     58      # password must be specified or an error is raised. 
     59      # 
     60      def save 
     61        raise "No password specified" if self.pass.empty? 
     62        @mech.submit(@form,@form.buttons[0]) 
     63      end 
    1564 
    16         def initialize(project) 
    17           @project = project 
    18           @mech = WWW::Mechanize.new 
    19           @mech.get(HOME_PAGE + "/update.rhtml?name=#{project}") 
    20           @form = @mech.page.forms[1] 
    21           yield self if block_given? 
     65      def method_missing(id,*args) 
     66        id = id.to_s.gsub(/=$/, '') 
     67        if @form.field(id) 
     68          return @form.field(id).value if args.empty? 
     69          return @form.field(id).value = args[0] 
    2270        end 
    23          
    24         # Returns an array of form field names. 
    25         # 
    26         def fields 
    27           @form.fields.map{ |field| field.name } 
    28         end 
    29  
    30         def method_missing(id,*args) 
    31           id = id.to_s.gsub(/=$/,"") 
    32           if @form.field(id) 
    33             return @form.field(id).value if args.empty? 
    34             return @form.field(id).value = args[0] 
    35           end; super 
    36         end 
    37  
    38          # TODO: finish 
    39          def fetch 
    40             agent = WWW::Mechanize.new 
    41             page  = agent.get(HOME_PAGE + "/project/#{@project}") 
    42          end 
    43  
    44          def save 
    45            raise "No password specified" if self.pass.empty? 
    46            @mech.submit(@form,@form.buttons[0]) 
    47          end 
     71        super 
    4872      end 
    49    end 
     73    end 
     74  end 
    5075end