You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@whimsical.apache.org by se...@apache.org on 2018/07/06 10:35:27 UTC

[whimsy] branch master updated: Simplify by using instance variables

This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/whimsy.git


The following commit(s) were added to refs/heads/master by this push:
     new abf8406  Simplify by using instance variables
abf8406 is described below

commit abf840629a995caf0e02d9e15dacd783bfe672ad
Author: Sebb <se...@apache.org>
AuthorDate: Fri Jul 6 11:35:25 2018 +0100

    Simplify by using instance variables
    
    Also now agrees with other action modules
---
 www/project/icla/views/actions/update.json.rb | 107 ++++++++++++--------------
 1 file changed, 48 insertions(+), 59 deletions(-)

diff --git a/www/project/icla/views/actions/update.json.rb b/www/project/icla/views/actions/update.json.rb
index 50c6d32..ff71da8 100644
--- a/www/project/icla/views/actions/update.json.rb
+++ b/www/project/icla/views/actions/update.json.rb
@@ -35,9 +35,8 @@ VALID_PHASES=%w{discuss vote cancelled tallied invite}
 VALID_VOTES=%w{+1 +0 -0 -1}
 
 # Tally the votes and send them
-def sendTally(data, contents)
-  member = data['member'] # currently logged in
-  user_email = "#{member}@apache.org"
+def sendTally(contents)
+  user_email = "#{@member}@apache.org"
   pmc = ASF::Committee.find(contents['project'])
   pmc_email = "#{pmc.mail_list}@apache.org"
   subject = "[RESULT]" + contents['subject']
@@ -77,79 +76,72 @@ def sendTally(data, contents)
   mail.deliver
 end
 
-def update(data)
+def update()
   # setup and validation
-  token = data['token']
-  raise ArgumentError.new('token must not be nil') unless token
-  action = data['action']
-  raise ArgumentError.new("Invalid action: '#{action}'") unless VALID_ACTIONS.include? action
-  member = data['member']
-  comment = data['comment'] # may be nil
-  expectedPhase = data['expectedPhase']
-  raise ArgumentError.new('expectedPhase must not be nil') unless expectedPhase
-  newPhase = data['newPhase'] # nil for no change
-  if newPhase and not VALID_PHASES.include? newPhase
-    raise ArgumentError.new("Invalid newPhase: '#{newPhase}'")
+  raise ArgumentError.new('token must not be nil') unless @token
+  raise ArgumentError.new("Invalid action: '#{@action}'") unless VALID_ACTIONS.include? @action
+  raise ArgumentError.new('expectedPhase must not be nil') unless @expectedPhase
+  if @newPhase and not VALID_PHASES.include? @newPhase
+    raise ArgumentError.new("Invalid @newPhase: '#{@newPhase}'")
   end
 
   timestamp = Time.now.utc.to_s
   addComment = nil
   voteinfo = nil
-  if action == 'submitVote'
-    vote = data['vote']
-    raise ArgumentError.new("Invalid vote: '#{vote}'") unless VALID_VOTES.include? vote
-    raise ArgumentError.new('member must not be nil') unless member
-    if vote == '-1'
-      raise ArgumentError.new('-1 vote must have comment') unless comment
+  if @action == 'submitVote'
+    raise ArgumentError.new("Invalid vote: '#{@vote}'") unless VALID_VOTES.include? @vote
+    raise ArgumentError.new('member must not be nil') unless @member
+    if @vote == '-1'
+      raise ArgumentError.new('-1 vote must have comment') unless @comment
     end
-    if comment # allow comment for other votes
+    if @comment # allow comment for other votes
       voteinfo = {
-        'vote' => vote,
-        'comment' => comment,
-        'member' => member,
+        'vote' => @vote,
+        'comment' => @comment,
+        'member' => @member,
         'timestamp' => timestamp,
       }
     else
       voteinfo = {
-        'vote' => vote,
-        'member' => member,
+        'vote' => @vote,
+        'member' => @member,
         'timestamp' => timestamp,
       }
     end
-  elsif HAS_COMMENT.include? action
-    if comment
+  elsif HAS_COMMENT.include? @action
+    if @comment
       addComment = 
       {
-        'comment' => comment,
-        'member' => member,
+        'comment' => @comment,
+        'member' => @member,
         'timestamp' => timestamp,
       } 
     else
-      raise ArgumentError.new("comment must not be nil for '#{action}'")
+      raise ArgumentError.new("comment must not be nil for '#{@action}'")
     end
   end
 
-  file = "/srv/icla/#{token}.json"
+  file = "/srv/icla/#{@token}.json"
 
   # now read/update the file if necessary
   contents = {} # define the var outside the block
   rewrite = false # should the file be updated?
-  phases = *expectedPhase # convert string to array
+  phases = *@expectedPhase # convert string to array
 
   LockFile.lockfile(file, 'r+', File::LOCK_EX) do |f|
     contents = JSON::parse(f.read)
     phase = contents['phase']
-    raise ArgumentError.new("Phase '#{phase}': expected '#{expectedPhase}'") unless expectedPhase == '*' or phases.include? phase 
-    if newPhase && newPhase != phase
-      contents['phase'] = newPhase
+    raise ArgumentError.new("Phase '#{phase}': expected '#{@expectedPhase}'") unless @expectedPhase == '*' or phases.include? phase 
+    if @newPhase && @newPhase != phase
+      contents['phase'] = @newPhase
       rewrite = true
     end
-    if action == 'startVoting' # need to add a vote to start this off
+    if @action == 'startVoting' # need to add a vote to start this off
       comment0 = contents['comments'][0]['comment'] # initial comment
       voteinfo = {
         'vote' => '+1',
         'comment' => "#{comment0}\nHere is my +1", # append to original comment
-        'member' => member,
+        'member' => @member,
         'timestamp' => timestamp,
       }
       addComment['comment'] += "\n**Starting the vote.**"
@@ -169,8 +161,8 @@ def update(data)
     end
   end
 
-  if action == 'tallyVote'
-    sendTally(data, contents)
+  if @action == 'tallyVote'
+    sendTally(contents)
   end
 
   # return the data
@@ -179,10 +171,10 @@ def update(data)
 end
 
 # error handler
-def process(data)
+def process()
   contents = {}
   begin
-    contents = update(data)
+    contents = update
   rescue => e
     _error e
     _backtrace e.backtrace[0] # can be rather large
@@ -190,14 +182,6 @@ def process(data)
   _contents contents
 end
 
-def embed # called by Sinatra which sets params
-  process(params)
-end
-
-def main(params) # called by CLI which passes params
-  process(params)
-end
-
 if __FILE__ == $0 # Allow independent testing
   require 'whimsy/asf'
   require 'mail'
@@ -208,17 +192,22 @@ if __FILE__ == $0 # Allow independent testing
       ret[n] = a
     end
   end
-  data = Hash[*ARGV] # cannot combine this with next line as hash doesn't yet exist
-  data.each{|k,v| data[k] = v.split(',') if v =~ /,/} # fix up lists
-  puts data.inspect
-  if data['action'] == 'sendTally' # special for testing stand-alone
-    contents = JSON.parse(File.read("/srv/icla/#{data['token']}.json"))
-    sendTally(data, contents)
+  params = Hash[*ARGV] # cannot combine this with next line as hash doesn't yet exist
+  params.each{|k,v| params[k] = v.split(',') if v =~ /,/} # fix up lists
+  params.each{|k,v| instance_variable_set("@#{k}", v)}
+  puts params.inspect
+  if @action == 'sendTally' # special for testing stand-alone
+    contents = JSON.parse(File.read("/srv/icla/#{@token}.json"))
+    begin
+      sendTally(contents)
+    rescue => err
+      puts err
+    end
     puts ret['body_text']
   else
-    main(data)
+    process
     puts JSON.pretty_generate(ret) # output the return data
   end
 else
-  embed # Sinatra sets params
+  process
 end
\ No newline at end of file