Class: Prowler

Inherits:
Object
  • Object
show all
Defined in:
lib/prowler.rb

Defined Under Namespace

Modules: Priority Classes: ConfigurationError, DelayedJob

Constant Summary collapse

SERVICE_URL =
"https://prowl.weks.net/publicapi"
USER_AGENT =
"Prowler/1.1.1"
MULTIPLE_APIKEY_COMMANDS =
%w(add)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, application, provider_key = nil) ⇒ Prowler

Create an instance for sending to different accounts within a single Rails application

  • api_key: Your API key.

  • application: The name of your application.

  • provider_key: Key to override the rate limit of 1000 requests per hour. (Optional)



264
265
266
# File 'lib/prowler.rb', line 264

def initialize(api_key, application, provider_key = nil)
  @api_key, @application, @provider_key = api_key, application, provider_key
end

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



94
95
96
# File 'lib/prowler.rb', line 94

def api_key
  @api_key
end

.applicationObject

Returns the value of attribute application.



95
96
97
# File 'lib/prowler.rb', line 95

def application
  @application
end

.delayedObject

:nodoc:



108
109
110
# File 'lib/prowler.rb', line 108

def delayed
  @delayed
end

.open_timeoutObject

:nodoc:



96
97
98
# File 'lib/prowler.rb', line 96

def open_timeout
  @open_timeout
end

.provider_keyObject

Returns the value of attribute provider_key.



94
95
96
# File 'lib/prowler.rb', line 94

def provider_key
  @provider_key
end

.read_timeoutObject

:nodoc:



96
97
98
# File 'lib/prowler.rb', line 96

def read_timeout
  @read_timeout
end

.root_certificatesObject

Location of the root certificates file. Default: RAILS_ROOT/config/cacert.pem



130
131
132
# File 'lib/prowler.rb', line 130

def root_certificates
  @root_certificates
end

.send_notificationsObject

:nodoc:



104
105
106
# File 'lib/prowler.rb', line 104

def send_notifications
  @send_notifications
end

.verify_certificateObject

Returns the value of attribute verify_certificate.



97
98
99
# File 'lib/prowler.rb', line 97

def verify_certificate
  @verify_certificate
end

Instance Attribute Details

#api_keyObject

:nodoc:



257
258
259
# File 'lib/prowler.rb', line 257

def api_key
  @api_key
end

#applicationObject

:nodoc:



258
259
260
# File 'lib/prowler.rb', line 258

def application
  @application
end

#provider_keyObject

:nodoc:



257
258
259
# File 'lib/prowler.rb', line 257

def provider_key
  @provider_key
end

#send_notificationsObject

:nodoc:



258
259
260
# File 'lib/prowler.rb', line 258

def send_notifications
  @send_notifications
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Call this method to configure your account details in an initializer.

Yields:

  • (_self)

Yield Parameters:

  • _self (Prowler)

    the object that the method was called on



100
101
102
# File 'lib/prowler.rb', line 100

def configure
  yield self
end

.configured?Boolean

Whether the library has been configured

Returns:

  • (Boolean)


119
120
121
# File 'lib/prowler.rb', line 119

def configured?
  !@application.nil? && !@api_key.nil?
end

.enqueue_delayed_job(config, event, message, priority) ⇒ Object

:nodoc:



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/prowler.rb', line 188

def enqueue_delayed_job(config, event, message, priority) #:nodoc:
  record = Delayed::Job.enqueue(DelayedJob.new do |job|
    job.api_key = config.api_key
    job.provider_key = config.provider_key
    job.application = config.application
    job.event = event
    job.message = message
    job.priority = priority
  end)
  !record.new_record?
end

.loggerObject

Returns the default logger or a logger that prints to STDOUT.



135
136
137
138
139
# File 'lib/prowler.rb', line 135

def logger
  ActiveRecord::Base.logger
rescue
  @logger ||= Logger.new(STDERR)
end

.notify(event, message, priority = Priority::NORMAL, delayed = self.delayed) ⇒ Object

Send a notification to your iPhone:

  • event: The title of notification you want to send.

  • message: The text of the notification message you want to send.

  • priority: The priority of the notification - see Prowler::Priority. (Optional)

  • delayed: Whether to use Delayed::Job to send notifications. (Optional)

Raises:



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/prowler.rb', line 154

def notify(event, message, priority = Priority::NORMAL, delayed = self.delayed)
  raise ConfigurationError, "You must provide an API key to send notifications" if api_key.nil?
  raise ConfigurationError, "You must provide an application name to send notifications" if application.nil?
  if delayed
    enqueue_delayed_job(self, event, message, priority)
  else
    perform(
      :add, api_key, provider_key,
      {
        :application => application,
        :event => event,
        :description => message,
        :priority => priority
      }
    )
  end
end

.perform(command, api_key, provider_key, data = {}, method = :post) ⇒ Object

:nodoc:



178
179
180
181
182
183
184
185
186
# File 'lib/prowler.rb', line 178

def perform(command, api_key, provider_key, data = {}, method = :post) #:nodoc:
  params = { :apikey => format_api_key(command, api_key), :provider_key => provider_key }.merge(data).delete_if { |k,v| v.nil? }
  case method
  when :post
    perform_post(command, params)
  else
    perform_get(command, params)
  end
end

.reset_configurationObject

Reset configuration



113
114
115
116
# File 'lib/prowler.rb', line 113

def reset_configuration
  @application = @api_key = @provider_key = nil
  @delayed = @verify_certificate = @root_certificates = nil
end

.verifyObject

Verify the configured API key is valid

Raises:



173
174
175
176
# File 'lib/prowler.rb', line 173

def verify
  raise ConfigurationError, "You must provide an API key to verify" if api_key.nil?
  perform(:verify, api_key, provider_key, {}, :get)
end

.verify_certificate?Boolean

Whether to verify the server’s SSL certificate

Returns:

  • (Boolean)


124
125
126
# File 'lib/prowler.rb', line 124

def verify_certificate?
  @verify_certificate ||= false
end

Instance Method Details

#notify(event, message, priority = Priority::NORMAL, delayed = self.class.delayed) ⇒ Object

Send a notification to your iPhone:

  • event: The title of notification you want to send.

  • message: The text of the notification message you want to send.

  • priority: The priority of the notification - see Prowler::Priority. (Optional)

  • delayed: Whether to use Delayed::Job to send notifications. (Optional)

Raises:



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/prowler.rb', line 273

def notify(event, message, priority = Priority::NORMAL, delayed = self.class.delayed)
  raise ConfigurationError, "You must provide an API key to send notifications" if api_key.nil?
  raise ConfigurationError, "You must provide an application name to send notifications" if application.nil?
  if delayed
    self.class.enqueue_delayed_job(self, event, message, priority)
  else
    self.class.perform(
      :add, api_key, provider_key,
      {
        :application => application,
        :event => event,
        :description => message,
        :priority => priority
      }
    )
  end
end

#verifyObject

Verify the configured API key is valid

Raises:



292
293
294
295
# File 'lib/prowler.rb', line 292

def verify
  raise ConfigurationError, "You must provide an API key to verify" if api_key.nil?
  self.class.perform(:verify, api_key, provider_key, {}, :get)
end