Module: Popcorn

Defined in:
lib/popcorn.rb,
lib/popcorn/version.rb,
lib/popcorn/configuration.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

API_URL =
'https://popcornnotify.com'
VERSION =
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



9
10
11
# File 'lib/popcorn.rb', line 9

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



12
13
14
15
# File 'lib/popcorn.rb', line 12

def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end

.notify(recipients, message, opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/popcorn.rb', line 17

def self.notify(recipients, message, opts = {})
  self.configuration ||= Configuration.new

  # Setup Default Options
  default_options = {
    subject: nil,
    verbose: false,
    api_key: self.configuration.api_key
  }
  options = default_options.merge(opts)

  # Create Faraday Client
  conn = Faraday.new(url: API_URL) do |f|
    f.request :url_encoded
    # Filter api_key from logs if verbose
    f.response :logger do |logger|
      logger.filter(/(api_key=)(\w+)/,'\1[FILTERED]')
    end if options[:verbose]
    f.adapter Faraday.default_adapter
  end

  # Convert recipients to string if Array
  recipients = recipients.join(',') if recipients.is_a?(Array)

  # Initiate POST to /notify
  response = conn.post '/notify', {
    recipients: recipients.to_s,
    message: message.to_s,
    subject: options[:subject].to_s,
    api_key: options[:api_key].to_s
  }

  # Return true or false
  response.success?
end