Skip to content

Instantly share code, notes, and snippets.

@alexbartlow
Last active June 14, 2016 16:09
Show Gist options
  • Save alexbartlow/6117c8eed4e27f070bbc11793eaf2723 to your computer and use it in GitHub Desktop.
Save alexbartlow/6117c8eed4e27f070bbc11793eaf2723 to your computer and use it in GitHub Desktop.
Using a Submit-Only portal as an API Endpoint in Aha!
require 'net/http'
require 'uri'
require "base64"
require "json"
uri = URI("http://new-submit-only-portal.ideas.aha.io/ideas/new")
post_uri = URI("http://new-submit-only-portal.ideas.aha.io/ideas")
req = Net::HTTP::Get.new(uri)
res = Net::HTTP.start(uri.hostname, uri.port) {|http| http.request(req) }
page = res.body
cookies = res["Set-Cookie"].split(/[,;] /).grep(/_aha/)
authenticity_token = page.each_line.grep(/csrf-token/).first.match(/content="([^"]+)"/)[1]
portal_token = page.each_line.grep(/portal-token/).first.match(/content="([^"]+)"/)[1]
project_id = page.each_line.grep(/ideas_idea\[project_id\]/).first.match(/value="([^"]+)"/)[1]
puts cookies, authenticity_token
http = Net::HTTP.new(post_uri.hostname, post_uri.port)
headers = {
"Cookie" => cookies.join("; ")
}
# Create an attachment
attachment_params = {
"name_prefix" => "ideas_idea[description_attributes]",
"qqfile" => File.read("my_uploaded_file.jpg")
# This needs to be a multipart upload, but it will suffice for demonstration purposes
}
_, attachment_data = http.post("/idea_portals/#{portal_token}/attachments", URI.encode_www_form(attachment_params), headers)
attachment_data = JSON.parse(attachment_data)
attachment_ids ||= []
attachment_ids << attachment_data["attachmentId"]
# Repeat this process for multiple uploads.
data = {
"ideas_idea[project_id]" => project_id,
"ideas_idea[name]" => "Idea Name",
"ideas_idea[description_attributes][body]" => "Description of the Idea",
"ideas_idea[description_attributes][attachment_ids][]" => attachment_ids,
"ideas_portal_user[email]" => "test@example.com", # The user creating the idea
"authenticity_token" => authenticity_token
}
resp, data = http.post(post_uri.path, URI.encode_www_form(data), headers)
puts resp, data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment