Skip to content

Instantly share code, notes, and snippets.

@johnthepink
Last active August 29, 2015 14:08
Show Gist options
  • Save johnthepink/aff6d5da14bb771b72f7 to your computer and use it in GitHub Desktop.
Save johnthepink/aff6d5da14bb771b72f7 to your computer and use it in GitHub Desktop.
Basecamp Alfred Workflow
require 'net/http'
require 'json'
# contants
BASECAMP_USERNAME = ARGV[0]
BASECAMP_PASSWORD = ARGV[1]
BASECAMP_EMAIL = ARGV[2]
BASECAMP_COMPANY_IDS = ARGV[3].split(",")
QUERY = ARGV[4]
def get_project_json(company_id)
# setup URI
uri = URI("https://basecamp.com/#{company_id}/api/v1/projects.json")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
# setup request
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth(BASECAMP_USERNAME, BASECAMP_PASSWORD)
request["User-Agent"] = "Alfred App (#{BASECAMP_EMAIL})"
# make request
response = http.request(request)
JSON.parse(response.body)
end
def project_xml(options = {})
<<-XML
<item arg="#{options[:arg]}" uid="#{options[:uid]}" autocomplete="#{options[:autocomplete]}">
<title>#{options[:title]}</title>
<subtitle>#{options[:subtitle]}</subtitle>
<subtitle mod="cmd">Copy to clipboard</subtitle>
<icon>icon.png</icon>
</item>
XML
end
def match?(project, query)
project["name"] =~ /#{query}/i
end
projects_collection = BASECAMP_COMPANY_IDS.map{ |company| get_project_json(company) }
# find matches
projects_collection.each do |projects|
projects.select!{ |project| match?(project, QUERY) }
end
# create xml response
alfred = "<?xml version='1.0'?><items>"
projects_collection.each do |projects|
projects.each do |project|
alfred += project_xml(
{
arg: project["app_url"],
uid: project["id"],
autocomplete: project["name"],
title: project["name"],
subtitle: project["app_url"]
}
end
end
alfred += "</items>"
puts alfred
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment