Module: Snitcher

Extended by:
Snitcher
Included in:
Snitcher
Defined in:
lib/snitcher.rb,
lib/snitcher/api.rb,
lib/snitcher/version.rb

Defined Under Namespace

Modules: API

Constant Summary collapse

VERSION =
"0.4.2".freeze

Instance Method Summary collapse

Instance Method Details

#snitch(*args) ⇒ Boolean

Check-in to Dead Man’s Snitch.

Examples:

Snitch.snitch("c2354d53d2")
# => true

Parameters:

  • 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 nosnch.in/c2354d53d2.

  • opts (Hash)

Returns:

  • (Boolean)

    if the check-in succeeded.



78
79
80
81
82
# File 'lib/snitcher.rb', line 78

def snitch(*args)
  snitch!(*args)
rescue StandardError
  false
end

#snitch!(token, opts = {}) ⇒ Boolean

Check-in to Dead Man’s Snitch, exceptions are raised on failures.

Examples:

Snitch.snitch("c2354d53d2")
# => true

Parameters:

  • 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 nosnch.in/c2354d53d2.

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :message (String)

    Text message to include with the check-in. The message is limited to 256 characters.

  • :status (String, Fixnum, nil)

    Exit code for the check-in. A status of “”, nil, or 0 are all treated as the job finishing successfully.

  • :timeout (Float, Fixnum)

    Number of seconds to wait for a response from the server. Default is 5 seconds.

Returns:

  • (Boolean)

    if the check-in succeeded.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/snitcher.rb', line 31

def snitch!(token, opts = {})
  params = {}
  params[:m] = opts[:message] if opts[:message]

  # It's unnecessary to send an empty status
  if opts[:status] && opts[:status] != ""
    params[:s] = opts[:status]
  end

  uri = URI.parse(checkin_url(opts, token))
  if params.any?
    uri.query = URI.encode_www_form(params)
  end

  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
end