0

assign task to Asana

  require "rubygems"
  require "JSON"
  require "net/https"
  api_key = "1wPBetR9.6bhINO7xO9ypG6iP2aYU8hx"
  workspace_id = "5386494137624"
  assignee = "craig@theaerialistpress.com"

  # set up HTTPS connection
  uri = URI.parse("https://app.asana.com/api/1.0/tasks")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER

  # set up the request
  header = {
    "Content-Type" => "application/json"
  }

  req = Net::HTTP::Post.new(uri.path, header)
  req.basic_auth(api_key, '')
  req.body = {
    "data" => {
      "workspace" => workspace_id,
      "name" => "House Order: " + @order.name,
      "assignee" => assignee,
      "projects" => "35978729781365",
      "notes" => "Name: " + @order.name + 
                 "\nEmail: " + @order.email + 
                 "\nEvent Date: " + @date.to_s +
                 "\nDesign: " + @order.design +
                 "\n\nPaper Color: " + @order.paper_color +
                 "\nFont #1: " + @order.font1 +
                 "\nFont #2: " + @order.font2 +
                 if @order.card_type1 != "none" 
                   "\n\nCard #1: " + @order.card_type1 + "\nPaper Weight: " + @order.paper_weight1 + "\nQuantity: " + @order.quantity1 + "\nInk Color #1: " + @order.ink_color11 + "\nInk Color #2: " + @order.ink_color12 + "\nWording: " + @order.wording1 + "\nReturn Address Printing: " + @order.return_address1 + "\nGuest Address Printing: " + @order.guest_address1.to_s + "\nEnvelope Liners: " + @order.envelope_liners1
                 end
                 if @order.card_type2 != "none" 
                   "\n\nCard #2: " + @order.card_type2 + "\nPaper Weight: " + @order.paper_weight2 + "\nQuantity: " + @order.quantity2 + "\nInk Color #2: " + @order.ink_color21 + "\nInk Color #2: " + @order.ink_color22 + "\nWording: " + @order.wording2 + "\nReturn Address Printing: " + @order.return_address2 + "\nGuest Address Printing: " + @order.guest_address2.to_s + "\nEnvelope Liners: " + @order.envelope_liners2
                 end
                                    
    }       
  }.to_json()

    # issue the request
  res = http.start { |http| http.request(req) }

  # output
  body = JSON.parse(res.body)
  if body['errors'] then
    puts "Server returned an error: #{body['errors'][0]['message']}"
  else
    puts "Created task with id: #{body['data']['id']}"
end

I am getting a syntax error when I am using if statements inside the data I am sending to asana. I am very new at rails and not sure what I am doing wrong.

This is the error:

syntax error, unexpected keyword_if, expecting '}' if @order.card_type2 != "none" ^ /Users/craigrinde/Dropbox/top_secret/aerialist/app/controllers/orders_controller.rb:212: syntax error, unexpected '}', expecting keyword_end } ^

Community
  • 1
  • 1
Craig Rinde
  • 95
  • 1
  • 2
  • 8
  • You are trying to concatenate a string with the `if` condition in "notes". That is causing the problem – Wes Foster Oct 20 '15 at 19:07
  • Thanks for your reply! What is the better way to do this or the fix? – Craig Rinde Oct 20 '15 at 19:47
  • 1
    Try extracting it to a method and setting it outside of your hash. Then referencing the local variable inside the hash – ruby_newbie Oct 20 '15 at 20:13
  • You should never post your API key anywhere public. You should revoke that API key and create a new one immediately. For these exact reasons if you are building a rails application then you should consider implementing OAuth and not using the API key. – Andrew Noonan Oct 20 '15 at 21:28

0 Answers0