Skip to content

Instantly share code, notes, and snippets.

@dokipen
Created March 27, 2011 22:35
Show Gist options
  • Select an option

  • Save dokipen/889716 to your computer and use it in GitHub Desktop.

Select an option

Save dokipen/889716 to your computer and use it in GitHub Desktop.
Not secure function that gets convore topic id by url
#!/usr/bin/env ruby
require 'json'
require 'net/https'
# Run as `ruby convore-topic-id.rb <URL>`
def request(http, path, user, pass)
req = Net::HTTP::Get.new(path)
req.basic_auth user, pass
http.request(req) do |response|
yield response
end
end
def topic_id url, username, password
url =~ %r{^((https?://)?convore.com)?/([^/]+)/([^/]+)/.*$}
group, topic = $3, $4
raise 'Invalid URL' unless group && topic
http = Net::HTTP.new('convore.com', 443)
http.use_ssl = true
http.start do |http|
r = lambda do |p,&cb|
request http, p, username, password do |response|
cb.call response
end
end
r.call '/api/groups.json' do |groups_response|
groups_json = JSON.parse(groups_response.body)
groups_json["groups"].each do |g|
if g["slug"] == group
r.call "/api/groups/#{g["id"]}/topics.json" do |topics_response|
topics_json = JSON.parse(topics_response.body)
topics_json["topics"].each do |t|
return t["id"] if t["slug"] == topic
end
end
end
end
return nil
end
end
end
if __FILE__ == $0
unless ARGV[0]
puts "Gets topic id from convore URL"
puts "#{__FILE__} <URL>"
exit 1
end
username = ENV['CONVORE_USERNAME']
unless username
print 'username: '
username = $stdin.gets.strip
end
password = ENV['CONVORE_PASSWORD']
unless password
print 'password: '
password = $stdin.gets.strip
end
id = topic_id ARGV[0], username, password
if id
puts "topic_id: #{id}"
else
puts "not found"
exit 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment