-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1323 from prathyush/master
Agent for Boxcar.io
- Loading branch information
Showing
2 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
module Agents | ||
class BoxcarAgent < Agent | ||
|
||
cannot_be_scheduled! | ||
cannot_create_events! | ||
|
||
API_URL = 'https://new.boxcar.io/api/notifications' | ||
|
||
description <<-MD | ||
The Boxcar agent sends push notifications to iPhone. | ||
To be able to use the Boxcar end-user API, you need your `Access Token`. | ||
The access token is available on general "Settings" screen of Boxcar iOS | ||
app or from Boxcar Web Inbox settings page. | ||
Please provide your access token in the `user_credentials` option. If | ||
you'd like to use a credential, set the `user_credentials` option to `{% | ||
credential CREDENTIAL_NAME %}`. | ||
Options: | ||
* `user_credentials` - Boxcar access token. | ||
* `title` - Title of the message. | ||
* `body` - Body of the message. | ||
* `source_name` - Name of the source of the message. Set to `Huginn` by default. | ||
* `icon_url` - URL to the icon. | ||
* `sound` - Sound to be played for the notification. Set to 'bird-1' by default. | ||
MD | ||
|
||
def default_options | ||
{ | ||
'user_credentials' => '', | ||
'title' => "{{title}}", | ||
'body' => "{{body}}", | ||
'source_name' => "Huginn", | ||
'icon_url' => "", | ||
'sound' => "bird-1" | ||
} | ||
end | ||
|
||
def working? | ||
received_event_without_error? | ||
end | ||
|
||
def strip(string) | ||
(string || '').strip | ||
end | ||
|
||
def validate_options | ||
errors.add(:base, "you need to specify a boxcar api key") if options['user_credentials'].blank? | ||
end | ||
|
||
def receive(incoming_events) | ||
incoming_events.each do |event| | ||
payload_interpolated = interpolated(event) | ||
user_credentials = payload_interpolated['user_credentials'] | ||
post_params = { | ||
'user_credentials' => user_credentials, | ||
'notification' => { | ||
'title' => strip(payload_interpolated['title']), | ||
'long_message' => strip(payload_interpolated['body']), | ||
'source_name' => payload_interpolated['source_name'], | ||
'sound' => payload_interpolated['sound'], | ||
'icon_url' => payload_interpolated['icon_url'] | ||
} | ||
} | ||
send_notification(post_params) | ||
end | ||
end | ||
|
||
def send_notification(post_params) | ||
response = HTTParty.post(API_URL, :query => post_params) | ||
raise StandardError, response['error']['message'] if response['error'].present? | ||
if response['Response'].present? && response['Response'] == "Not authorized" | ||
raise StandardError, response['Response'] | ||
end | ||
if !response['id'].present? | ||
raise StandardError, "Invalid response from Boxcar: #{response}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require 'rails_helper' | ||
|
||
describe Agents::BoxcarAgent do | ||
before(:each) do | ||
@valid_params = { | ||
'user_credentials' => 'access_token', | ||
'title' => 'Sample Title', | ||
'body' => 'Sample Body' | ||
} | ||
@checker = Agents::BoxcarAgent.new(:name => "boxcartest", :options => @valid_params) | ||
@checker.user = users(:bob) | ||
@checker.save! | ||
|
||
@event = Event.new | ||
@event.agent = agents(:bob_weather_agent) | ||
@event.payload = { :body => 'Sample message' } | ||
@event.save! | ||
end | ||
|
||
describe 'validating' do | ||
before do | ||
expect(@checker).to be_valid | ||
end | ||
|
||
it "should require access token" do | ||
@checker.options['user_credentials'] = nil | ||
expect(@checker).not_to be_valid | ||
end | ||
end | ||
|
||
describe '#working?' do | ||
it "should not be working until the first event was received" do | ||
expect(@checker).not_to be_working | ||
@checker.last_receive_at = Time.now | ||
expect(@checker).to be_working | ||
end | ||
end | ||
|
||
describe "#receive" do | ||
it "sends a message" do | ||
stub(HTTParty).post { {"id" => 1, "message" => "blah", "title" => "blah","source_name" => "Custom Notification"} } | ||
@checker.receive([@event]) | ||
end | ||
|
||
it "should raise error when invalid response arrives" do | ||
stub(HTTParty).post { {"blah" => "blah"} } | ||
expect{@checker.send_notification}.to raise_error | ||
end | ||
|
||
it "should raise error when response says unauthorized" do | ||
stub(HTTParty).post '{"Response":"Not authorized"}' | ||
expect{@checker.send_notification}.to raise_error | ||
end | ||
|
||
it "should raise error when response has an error" do | ||
stub(HTTParty).post '{"error": {"message": "Sample error"}}' | ||
expect{@checker.send_notification}.to raise_error | ||
end | ||
end | ||
end |