Skip to content

Instantly share code, notes, and snippets.

@trappist
Created June 11, 2012 19:04
Show Gist options
  • Select an option

  • Save trappist/2912003 to your computer and use it in GitHub Desktop.

Select an option

Save trappist/2912003 to your computer and use it in GitHub Desktop.
Command-line Socialyzer API client
#!/usr/bin/env ruby
require 'rest_client'
require 'json'
require 'optparse'
require 'pp'
api_key = ENV['SOCIALYZER_API_KEY'] || (
puts "Your Socialyzer API key needs to be present in your environment:"
puts " export SOCIALYZER_API_KEY=your_api_key"
puts "or"
puts " SOCIALYZER_API_KEY=your_api_key #{__FILE__} [options]"
exit 1
)
ACTIONS = {
"optimize" => "Optimize a tweet",
"add-twitter-account" => "Add a Twitter account by screen name",
"twitter-user" => "Get details of a Twitter user"
}
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: #{__FILE__} [options] [user]
one of #{ACTIONS.keys.map{|a|"--#{a}"}.join(', ')} is required."
ACTIONS.each do |action,desc|
opts.on("--#{action}", desc) do |a|
options[:action] = action.gsub(/-/,'_')
end
end
opts.on("-u", "--user SCREEN_NAME", "Twitter screen name") do |u|
options[:user] = u
end
opts.on("-o", "--options RESULTS", OptionParser::DecimalInteger, "Number of results to return") do |o|
options[:options] = o
end
opts.on("-s", "--start WINDOW_START", "Window start time as a number of hours from now or supported keyword") do |s|
options[:start] = s
end
opts.on("-e", "--end WINDOW_END", "Window end time as a number of hours from now or supported keyword") do |e|
options[:end] = e
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
@opts = opts
end.parse!
unless action = options.delete(:action)
puts @opts
exit 1
end
options.merge!(:key => api_key)
res = RestClient.post "http://api.socialyzerhq.com/api/#{action}.json", options.to_json, :content_type => :json, :accept => :json
pp JSON.parse(res)
@trappist
Copy link
Copy Markdown
Author

Be sure to gem install rest-client first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment