five_mobile_push 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 Kevin Faustino, James F. Herdman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -1,5 +1,7 @@
1
1
  require 'faraday'
2
2
 
3
+ # Provides us protection against the FiveMobilePush API's error handling which
4
+ # is currently incomplete.
3
5
  module Faraday
4
6
  class Response::Errors < Response::Middleware
5
7
 
@@ -0,0 +1,28 @@
1
+ module FiveMobilePush
2
+ class Notifier
3
+ # Simple proxy class for building messages. Do not use this class directly.
4
+ class Message
5
+ # @param [String] body The text to send
6
+ def body(body)
7
+ @body = body
8
+ end
9
+
10
+ # @param [Hash] meta_data (optional) The optional meta data to send.
11
+ def meta_data(meta_data)
12
+ @meta_data = meta_data
13
+ end
14
+
15
+ # @private
16
+ def self.dsl(&block)
17
+ instance = new
18
+ block.call(instance)
19
+ instance.to_payload
20
+ end
21
+
22
+ # @private
23
+ def to_payload
24
+ FiveMobilePush::Payload.new(@message, @meta_data)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,15 +1,6 @@
1
1
  module FiveMobilePush
2
2
  # @todo Validate provided platforms
3
- # @todo Simplify platform selection
4
3
  class Notifier
5
-
6
- # The following are platforms supported by the Five Mobile Push platform
7
-
8
- ALL = "all"
9
- IPHONE = "iphone"
10
- BLACKBERRY = "blackberry"
11
- ANDROID = "android"
12
-
13
4
  # @param [FiveMobilePush::Client] client The Client object to use when
14
5
  # sending notices
15
6
  def initialize(client)
@@ -18,13 +9,6 @@ module FiveMobilePush
18
9
 
19
10
  # Broadcast a notification to one or more platforms of an application.
20
11
  #
21
- # == Supported Platforms
22
- #
23
- # * all
24
- # * iphone
25
- # * blackberry
26
- # * android
27
- #
28
12
  # @param [Array<String>, String] platforms Any of the supported platforms.
29
13
  #
30
14
  # @yield [message] Provides a mini-DSL for constructing the message to
@@ -34,13 +18,15 @@ module FiveMobilePush
34
18
  #
35
19
  # @example Simple usage
36
20
  # n = FiveMobilePush::Notifier.new(client)
37
- # n.broadcast(FiveMobilePush::Notifier::ALL) do |message|
21
+ # n.broadcast(FiveMobilePush::Platform::ALL) do |message|
38
22
  # message.body "Downtime this weekend"
39
23
  # end
24
+ #
25
+ # @see FiveMobilePush::Platform.supported_platforms
40
26
  def broadcast(platforms, &block)
41
27
  @client.post 'notify/broadcast',
42
- :platforms => build_platforms_string(platforms),
43
- :payload => capture_message(&block).to_json
28
+ :platforms => Platform.new(platforms).build_list,
29
+ :payload => Message.dsl(&block).to_json
44
30
  end
45
31
 
46
32
  # Send a notification to any number of specified devices
@@ -61,18 +47,11 @@ module FiveMobilePush
61
47
  @client.post 'notify/toDevices',
62
48
  :id_type => FiveMobilePush::DEFAULT_ID_TYPE,
63
49
  :id_values => devices.join(','),
64
- :payload => capture_message(&block).to_json
50
+ :payload => Message.dsl(&block).to_json
65
51
  end
66
52
 
67
53
  # Notifies any device registered with the provided tags.
68
54
  #
69
- # == Supported Platforms
70
- #
71
- # * all
72
- # * iphone
73
- # * blackberry
74
- # * android
75
- #
76
55
  # @param [Array<String>, String] platforms Any of the supported platforms.
77
56
  # @param [Array<String>] tags Any tag that is registered
78
57
  #
@@ -83,49 +62,16 @@ module FiveMobilePush
83
62
  #
84
63
  # @example Simple usage
85
64
  # n = FiveMobilePush::Notifier.new(client)
86
- # n.notify_by_tags(FiveMobilePush::Notifier::ALL, ['muffin', 'bacon']) do |message|
65
+ # n.notify_by_tags(FiveMobilePush::Platform::ALL, ['muffin', 'bacon']) do |message|
87
66
  # message.body "Downtime this weekend"
88
67
  # end
68
+ #
69
+ # @see FiveMobilePush::Platform.supported_platforms
89
70
  def notify_by_tags(platforms, tags, &block)
90
71
  @client.post 'notify/toTags',
91
- :platforms => build_platforms_string(platforms),
72
+ :platforms => Platform.new(platforms).build_list,
92
73
  :tags => tags.join(','),
93
- :payload => capture_message(&block).to_json
74
+ :payload => Message.dsl(&block).to_json
94
75
  end
95
-
96
- # Simple proxy class for building messages. Do not use this class directly.
97
- class Message
98
- # @param [String] body The text to send
99
- def body(body)
100
- @body = body
101
- end
102
-
103
- # @param [Hash] meta_data (optional) The optional meta data to send.
104
- def meta_data(meta_data)
105
- @meta_data = meta_data
106
- end
107
-
108
- # @private
109
- def to_payload
110
- FiveMobilePush::Payload.new(@message, @meta_data)
111
- end
112
- end
113
-
114
- private
115
-
116
- def capture_message(&block)
117
- payload_proxy = Message.new
118
- block.call(payload_proxy)
119
- payload_proxy.to_payload
120
- end
121
-
122
- def build_platforms_string(platforms)
123
- if platforms.kind_of?(Enumerable)
124
- platforms.join(',')
125
- else
126
- platforms.to_s
127
- end
128
- end
129
-
130
76
  end
131
77
  end
@@ -0,0 +1,67 @@
1
+ module FiveMobilePush
2
+ ##
3
+ # Stores supported platform names as defined constants.
4
+ #--
5
+ # Internal to this gem, this class also aids in building and verifying
6
+ # selected platforms.
7
+ class Platform
8
+ ALL = "all"
9
+ IPHONE = "iphone"
10
+ BLACKBERRY = "blackberry"
11
+ ANDROID = "android"
12
+
13
+ SUPPORTED_PLATFORMS = [
14
+ ALL,
15
+ IPHONE,
16
+ BLACKBERRY,
17
+ ANDROID
18
+ ]
19
+
20
+ # @private
21
+ attr_reader :target_platforms
22
+
23
+ # @param [Array<String>, String] target_platforms The platforms being
24
+ # targeted
25
+ #
26
+ # @private
27
+ def initialize(*target_platforms)
28
+ self.target_platforms = target_platforms
29
+ validate!
30
+ end
31
+
32
+ # @return [String] a formatted String with a list of the target platforms
33
+ #
34
+ # @private
35
+ def build_list
36
+ target_platforms.join(',')
37
+ end
38
+
39
+ # @param [Array<String>, String] target_platforms The platforms being
40
+ # targeted
41
+ #
42
+ # @private
43
+ def target_platforms=(*target_platforms)
44
+ @target_platforms = target_platforms.flatten.map(&:to_s)
45
+ end
46
+
47
+ # @raise [InvalidPlatformError] raised when an invalid target platform has been
48
+ # selected
49
+ #
50
+ # @private
51
+ def validate!
52
+ validates_target_platforms
53
+ end
54
+
55
+ private
56
+
57
+ def invalid_target_platforms
58
+ target_platforms - SUPPORTED_PLATFORMS
59
+ end
60
+
61
+ def validates_target_platforms
62
+ unless invalid_target_platforms.empty?
63
+ raise FiveMobilePush::InvalidPlatformError, "The following platforms are invalid: #{invalid_target_platforms}"
64
+ end
65
+ end
66
+ end
67
+ end
@@ -1,3 +1,3 @@
1
1
  module FiveMobilePush
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -8,21 +8,28 @@ module FiveMobilePush
8
8
  autoload :Notifier, 'five_mobile_push/notifier'
9
9
  autoload :Tag, 'five_mobile_push/tag'
10
10
  autoload :Payload, 'five_mobile_push/payload'
11
+ autoload :Message, 'five_mobile_push/message'
12
+ autoload :Platform, 'five_mobile_push/platform'
11
13
 
12
- class UnauthorizedError < StandardError; end
13
- class GeneralError < StandardError; end
14
- class ServerError < StandardError; end
15
-
16
-
17
- VALID_OPTION_KEYS = [:api_token, :application_uid]
18
- SUPPORTED_PLATFORMS = %w(iphone blackberry android)
14
+ class UnauthorizedError < StandardError; end
15
+ class GeneralError < StandardError; end
16
+ class ServerError < StandardError; end
17
+ class InvalidPlatformError < StandardError; end
19
18
 
20
19
  DEFAULT_ID_TYPE = 'native'
21
20
 
22
- attr_accessor *VALID_OPTION_KEYS
21
+ attr_accessor :api_token, :application_uid
23
22
 
24
23
  attr_writer :adapter
25
24
 
25
+ # @yield [config] Provides a block to conveniently configure the library
26
+ #
27
+ # @example Simple usage
28
+ #
29
+ # FiveMobilePush.configure do |config|
30
+ # config.api_token = '12345'
31
+ # config.application_uid = 'myfancyapp'
32
+ # end
26
33
  def configure
27
34
  yield self
28
35
  end
@@ -0,0 +1,5 @@
1
+ Fabricator(:platform, :class_name => FiveMobilePush::Platform) do
2
+ on_init {
3
+ init_with(FiveMobilePush::Platform::ALL, FiveMobilePush::Platform::IPHONE)
4
+ }
5
+ end
@@ -13,8 +13,8 @@ describe FiveMobilePush::Notifier do
13
13
  it "broadcasts a notification to one or more platforms of the application" do
14
14
  stub_request(:post, broadcast_endpoint)
15
15
 
16
- subject.broadcast(:iphone) do |payload|
17
- payload.message "Minor downtime tonight from 7PM-9PM EST"
16
+ subject.broadcast(:iphone) do |message|
17
+ message.body "Minor downtime tonight from 7PM-9PM EST"
18
18
  end
19
19
 
20
20
  a_request(:post, broadcast_endpoint).should have_been_made
@@ -29,8 +29,8 @@ describe FiveMobilePush::Notifier do
29
29
  it "notifies a list of devices" do
30
30
  stub_request(:post, notify_devices_endpoint)
31
31
 
32
- subject.notify_devices(['abc', 'def']) do |payload|
33
- payload.message 'You win a prize!'
32
+ subject.notify_devices(['abc', 'def']) do |message|
33
+ message.body 'You win a prize!'
34
34
  end
35
35
 
36
36
  a_request(:post, notify_devices_endpoint).should have_been_made
@@ -45,8 +45,8 @@ describe FiveMobilePush::Notifier do
45
45
  it "notifies devices by tags" do
46
46
  stub_request(:post, notify_by_tags_endpoint)
47
47
 
48
- subject.notify_by_tags([:iphone, :android], ['tag1', 'tag2']) do |payload|
49
- payload.message 'tag1 and tag2'
48
+ subject.notify_by_tags([:iphone, :android], ['tag1', 'tag2']) do |message|
49
+ message.body 'tag1 and tag2'
50
50
  end
51
51
 
52
52
  a_request(:post, notify_by_tags_endpoint).should have_been_made
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe FiveMobilePush::Platform do
4
+ describe '.new' do
5
+ it 'sets a list of target platforms' do
6
+ described_class.new(FiveMobilePush::Platform::ALL).
7
+ target_platforms.should include(FiveMobilePush::Platform::ALL)
8
+ end
9
+
10
+ it 'raises an InvalidPlatformError exception if an invalid platform was selected' do
11
+ expect {
12
+ described_class.new('fakeplatform')
13
+ }.to raise_error(FiveMobilePush::InvalidPlatformError)
14
+ end
15
+ end
16
+
17
+ describe '#build_list' do
18
+ subject { Fabricate.build(:platform) }
19
+
20
+ it 'includes each targetted platform' do
21
+ subject.target_platforms.all? { |p| subject.build_list.include?(p) }.should be_true
22
+ end
23
+
24
+ it 'separates values by a comma' do
25
+ subject.build_list.should =~ /.+,.+/
26
+ end
27
+ end
28
+
29
+ describe '#target_platforms=' do
30
+ it 'flattens the provided argument' do
31
+ subject.target_platforms = [[FiveMobilePush::Platform::ALL]]
32
+ subject.target_platforms.should == [FiveMobilePush::Platform::ALL]
33
+ end
34
+ end
35
+ end
@@ -20,29 +20,26 @@ describe FiveMobilePush do
20
20
 
21
21
  end
22
22
 
23
- describe 'configure' do
23
+ describe '.configure' do
24
+ it 'can set the API token' do
25
+ api_token = '12345'
24
26
 
25
- FiveMobilePush::VALID_OPTION_KEYS.each do |key|
26
-
27
- it "sets the key #{key}" do
28
- FiveMobilePush.configure do |config|
29
- config.send("#{key}=", key)
30
- end
31
- FiveMobilePush.send(key).should == key
27
+ FiveMobilePush.configure do |config|
28
+ config.api_token = api_token
32
29
  end
33
30
 
31
+ FiveMobilePush.api_token.should == api_token
34
32
  end
35
33
 
36
- end
37
-
38
- describe 'platforms' do
34
+ it 'can set the application UID' do
35
+ application_uid = 'cheesebacon'
39
36
 
40
- %w(iphone blackberry android).each do |platform|
41
-
42
- specify { FiveMobilePush::SUPPORTED_PLATFORMS.should include(platform) }
37
+ FiveMobilePush.configure do |config|
38
+ config.application_uid = application_uid
39
+ end
43
40
 
41
+ FiveMobilePush.application_uid.should == application_uid
44
42
  end
45
-
46
43
  end
47
44
 
48
45
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: five_mobile_push
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.1
5
+ version: 0.4.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kevin Faustino
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-04-06 00:00:00 -04:00
14
+ date: 2011-04-07 00:00:00 -04:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
@@ -106,23 +106,28 @@ files:
106
106
  - .rspec
107
107
  - .yardopts
108
108
  - Gemfile
109
+ - MIT-LICENSE
109
110
  - Rakefile
110
111
  - five_mobile_push.gemspec
111
112
  - lib/faraday/errors.rb
112
113
  - lib/five_mobile_push.rb
113
114
  - lib/five_mobile_push/client.rb
114
115
  - lib/five_mobile_push/device.rb
116
+ - lib/five_mobile_push/message.rb
115
117
  - lib/five_mobile_push/notifier.rb
116
118
  - lib/five_mobile_push/payload.rb
119
+ - lib/five_mobile_push/platform.rb
117
120
  - lib/five_mobile_push/tag.rb
118
121
  - lib/five_mobile_push/version.rb
119
122
  - spec/fabricators/client.rb
120
123
  - spec/fabricators/device.rb
121
124
  - spec/fabricators/payload.rb
125
+ - spec/fabricators/platform.rb
122
126
  - spec/five_mobile_push/client_spec.rb
123
127
  - spec/five_mobile_push/device_spec.rb
124
128
  - spec/five_mobile_push/notifier_spec.rb
125
129
  - spec/five_mobile_push/payload_spec.rb
130
+ - spec/five_mobile_push/platform_spec.rb
126
131
  - spec/five_mobile_push/tags_spec.rb
127
132
  - spec/five_mobile_push_spec.rb
128
133
  - spec/fixtures/register.json
@@ -159,10 +164,12 @@ test_files:
159
164
  - spec/fabricators/client.rb
160
165
  - spec/fabricators/device.rb
161
166
  - spec/fabricators/payload.rb
167
+ - spec/fabricators/platform.rb
162
168
  - spec/five_mobile_push/client_spec.rb
163
169
  - spec/five_mobile_push/device_spec.rb
164
170
  - spec/five_mobile_push/notifier_spec.rb
165
171
  - spec/five_mobile_push/payload_spec.rb
172
+ - spec/five_mobile_push/platform_spec.rb
166
173
  - spec/five_mobile_push/tags_spec.rb
167
174
  - spec/five_mobile_push_spec.rb
168
175
  - spec/fixtures/register.json