Sha256: b90b6a0136ba8e7376318031df69990bea4a64718571c0a1b92b9fa468eaf162
Contents?: true
Size: 1.97 KB
Versions: 2
Compression:
Stored size: 1.97 KB
Contents
require "uri" require "timeout" require "net/http" require "snitcher/version" module Snitcher extend self # Check-in to Dead Man's Snitch. # # @param token [String] The unique Snitch token to check-in with. This can be # found on the Setup page as the last part of the HTTP check-in url. For # example, c2354d53d2 is the token in http://nosnch.in/c2354d53d2. # # @param [Hash] opts # # @option opts [String] :message Text message to include with the check-in. # The message is limited to 256 characters. # # @option opts [Float, Fixnum] :timeout Number of seconds to wait for a # response from the server. Default is 5 seconds. # no # # @example # Snitch.snitch("c2354d53d2") # # => true # # @raise [Timeout::Error] if the request took too long and timed out. # # @return [Boolean] if the check-in succeeded. def snitch(token, opts = {}) uri = URI.parse(checkin_url(opts, token)) uri.query = URI.encode_www_form(m: opts[:message]) if opts[:message] opts = initialize_opts(opts, uri) Net::HTTP.start(uri.host, uri.port, opts) do |http| request = Net::HTTP::Get.new(uri.request_uri) request["User-Agent"] = user_agent response = http.request(request) response.is_a?(Net::HTTPSuccess) end rescue ::Timeout::Error false end private def initialize_opts(options, uri) timeout = options.fetch(:timeout, 5) { open_timeout: timeout, read_timeout: timeout, ssl_timeout: timeout, use_ssl: use_ssl?(uri) } end def checkin_url(opts, token) if opts[:uri].nil? "https://nosnch.in/#{token}" else "#{opts[:uri]}/#{token}" end end def use_ssl?(uri) uri.scheme == "https" end def user_agent # RUBY_ENGINE was not added until 1.9.3 engine = defined?(::RUBY_ENGINE) ? ::RUBY_ENGINE : "Ruby" "Snitcher; #{engine}/#{RUBY_VERSION}; #{RUBY_PLATFORM}; v#{::Snitcher::VERSION}" end end require "snitcher/version"
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
snitcher-0.4.0.rc2 | lib/snitcher.rb |
snitcher-0.4.0.rc1 | lib/snitcher.rb |