Class: PostCommit::Hooks::URL
Overview
To send an URL post commit, you have to set up your URL and, optionally, username and password.
post_commit :url do
post "http://example.com", :message => "Post commit"
end
If you need to authorize your request with basic auth, you can set your username and password.
post_commit :url do
:username => "johndoe", :password => "mypass"
post "http://example.com", :message => "Post commit"
end
You can post data encoded as JSON or XML. Just set the :data_type option.
post_commit :url do
set :data_type, :json
set :data_type, :xml
end
To post a XML, your params hash need to be a multi-level hash. For instance, the hash {:message => { :title => "Some title", :body => "Some message" }} will be converted to
<>
<title>Some title</title>
<body>Some message</body>
</message>
Gotcha: to send a normal POST, you have to specify a flat hash. So {:a => 1} is ok, but {:a => {:b => 1}} will fail.
Instance Attribute Summary
Attributes inherited from Base
#credentials, #options, #request, #response, #uri
Instance Method Summary collapse
-
#post(url, params = {}) ⇒ Object
Post data to an arbitrary URL.
Methods inherited from Base
#authorize, #convert_to_params, #convert_to_xml, inherited, #initialize, #set
Constructor Details
This class inherits a constructor from PostCommit::Hooks::Base
Instance Method Details
#post(url, params = {}) ⇒ Object
Post data to an arbitrary URL.
post "http://example.com", :message => "Post commit", :service => "yourapp"
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/post_commit/hooks/url.rb', line 37 def post(url, params = {}) @uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) @request = Net::HTTP::Post.new(uri.path) case [:data_type] when :json then @request.content_type = "application/json" @request.body = params.to_json when :xml then @request.content_type = "application/xml" @request.body = convert_to_xml(params) else @request.form_data = convert_to_params(params) end @request.basic_auth credentials[:username], credentials[:password] if credentials[:username] @response = http.request(@request) if response.code =~ /^2\d+/ true else false end rescue Exception false end |