Last active
December 13, 2015 18:59
-
-
Save dtan4/4959799 to your computer and use it in GitHub Desktop.
Post to Catch.com from command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
require 'net/http' | |
require 'uri' | |
require 'json' | |
CREATE_NOTE_URI = 'https://api.catch.com/v3/streams/default' | |
USER = 'username' | |
PASS = 'password' | |
if ARGV.length < 1 | |
puts 'usage: catch.rb string_to_post' | |
exit 1 | |
end | |
text = "" | |
ARGV.map {|s| | |
text += "#{s} " | |
} | |
url = URI.parse(CREATE_NOTE_URI) | |
req = Net::HTTP::Post.new(url.path) | |
req.basic_auth USER, PASS | |
req.set_form_data({'text' => text.strip}) | |
http = Net::HTTP.new(url.host, url.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
res = http.start {|h| | |
h.request(req) | |
} | |
case res | |
when Net::HTTPSuccess | |
res_json = JSON.parse(res.body) | |
result_json = res_json["result"] | |
puts "Success to post!" | |
puts "id : #{result_json['id']}" | |
puts "create : #{result_json['created_at']}" | |
puts "text : #{result_json['text']}" | |
else | |
puts res | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment