#!/usr/bin/env ruby require 'rubygems' require 'commander/import' require 'rest_client' require 'fileutils' require 'json' require File.join(File.dirname(__FILE__), '..', 'lib', 'socialcast') include Socialcast program :version, File.read(File.join(File.dirname(__FILE__), '..', 'VERSION')).strip program :description, 'Socialcast API command line interface' command :authenticate do |c| c.syntax = 'socialcast authenticate [options]' c.summary = 'authenticate your Socialcast credentials' c.description = 'verify that your credentials are valid' c.example 'socialcast authenticate -u emily@socialcast.com', 'default usage' c.option '-u', '--user STRING', String, 'Socialcast account username/email' c.option '--domain STRING', String, 'Socialcast community domain' c.action do |args, options| options.default :domain => 'api.socialcast.com' options.user = ask('Socialcast username: ') unless options.user options.password = password say "Authenticating #{options.user}" url = ['https://', options.domain, '/api/authentication.json'].join params = {:email => options.user, :password => options.password } resource = RestClient::Resource.new url response = resource.post params puts "API response: #{response.body.to_s}" if options.trace communities = JSON.parse(response.body.to_s)['communities'] options.domain = communities.detect {|c| c['domain'] == options.domain} ? options.domain : communities.first['domain'] save_credentials :user => options.user, :password => options.password, :domain => options.domain say "Authentication successful for #{options.domain}" end end command :share do |c| c.syntax = 'socialcast share [options]' c.summary = 'share a message' c.description = 'post a message into your socialcast stream' c.example 'socialcast share "some text"', 'basic usage' c.option '--url STRING', String, 'referenced url for the message' c.action do |args, options| message = args.first url = ['https://', credentials[:domain], '/api/messages.json'].join say "Posting message from #{credentials[:user]} to: #{url}" resource = RestClient::Resource.new url, :user => credentials[:user], :password => credentials[:password] params = {:message => {:body => message, :url => options.url}} response = resource.post params puts "API response: #{response.body.to_s}" if options.trace say "Message has been shared" end end