five_mobile_push 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,2 +1,13 @@
1
- require 'bundler'
2
- Bundler::GemHelper.install_tasks
1
+ require 'rake'
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc "Run all examples"
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
8
+
9
+ desc "Build the gemfile"
10
+ task :build do
11
+ sh "gem build five_mobile_push.gemspec"
12
+ end
13
+ task :gem => :build
@@ -21,7 +21,7 @@ module FiveMobilePush
21
21
 
22
22
  # @private
23
23
  def to_payload
24
- FiveMobilePush::Payload.new(@body, @meta_data)
24
+ FiveMobilePush::Payload.new(@body, @meta_data || {})
25
25
  end
26
26
  end
27
27
  end
@@ -2,17 +2,27 @@ module FiveMobilePush
2
2
  # @private Used internally. You'll never use this class directly.
3
3
  # Documented for the benefit of contributors.
4
4
  class Payload
5
- attr_reader :message
6
- attr_accessor :meta_data
5
+ MAXIMUM_MESSAGE_LENGTH = 128
6
+
7
+ # This exception is raised when a payload meta data exceed its maximum length
8
+ class MessageTooLargeError < ArgumentError
9
+ end
10
+
11
+ attr_reader :message, :meta_data
7
12
 
8
13
  # @param [#to_s] message The message you wish to send with a notice
9
14
  # @param [Hash] meta_data (nil) Any meta data to send along with the
10
15
  # notice. Leave as +nil+ if none is to be sent.
11
- def initialize(message, meta_data=nil)
16
+ def initialize(message, meta_data={})
12
17
  self.message = message
13
18
  self.meta_data = meta_data
14
19
  end
15
20
 
21
+ def meta_data=(new_meta_data)
22
+ validate_meta_data!(new_meta_data)
23
+ @meta_data = new_meta_data
24
+ end
25
+
16
26
  # @param [#to_s] message The message you wish to send with a notice
17
27
  def message=(message)
18
28
  @message = message.to_s
@@ -25,17 +35,33 @@ module FiveMobilePush
25
35
 
26
36
  private
27
37
 
38
+ def validate_meta_data!(meta_data)
39
+ meta_data_is_correct_type!(meta_data)
40
+ meta_data_messages_is_valid_length!(meta_data)
41
+ end
42
+
43
+ def meta_data_is_correct_type!(meta_data)
44
+ unless meta_data.is_a?(Hash)
45
+ raise ArgumentError, 'meta_data must be a Hash'
46
+ end
47
+ end
48
+
49
+ # @todo Better error message for lengthy meta data
50
+ def meta_data_messages_is_valid_length!(meta_data)
51
+ unless meta_data.all? { |k, v| v.length <= MAXIMUM_MESSAGE_LENGTH }
52
+ raise MessageTooLargeError
53
+ end
54
+ end
55
+
28
56
  def as_json
29
- payload = {
57
+ {
30
58
  'msg' => {
31
59
  'type' => 'string',
32
60
  'value' => message
33
61
  },
34
62
  'sound' => 'default',
35
63
  'launch' => true
36
- }
37
- payload['meta'] = meta_data if meta_data
38
- payload
64
+ }.merge(meta_data)
39
65
  end
40
66
  end
41
67
  end
@@ -1,3 +1,3 @@
1
1
  module FiveMobilePush
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.2"
3
3
  end
@@ -2,7 +2,6 @@ require 'spec_helper'
2
2
  require 'uri'
3
3
 
4
4
  describe FiveMobilePush::Notifier do
5
-
6
5
  let(:client) { Fabricate.build(:client) }
7
6
 
8
7
  subject { FiveMobilePush::Notifier.new(client) }
@@ -15,15 +14,14 @@ describe FiveMobilePush::Notifier do
15
14
 
16
15
  subject.broadcast(:iphone) do |message|
17
16
  message.body "Minor downtime tonight from 7PM-9PM EST"
17
+ message.meta_data :a => '/a/b/c'
18
18
  end
19
19
 
20
20
  a_request(:post, broadcast_endpoint).should have_been_made
21
21
  end
22
-
23
22
  end
24
23
 
25
24
  describe '#notify_devices' do
26
-
27
25
  let(:notify_devices_endpoint) { notifier_endpoint('toDevices') }
28
26
 
29
27
  it "notifies a list of devices" do
@@ -31,15 +29,14 @@ describe FiveMobilePush::Notifier do
31
29
 
32
30
  subject.notify_devices(['abc', 'def']) do |message|
33
31
  message.body 'You win a prize!'
32
+ message.meta_data :a => '/a/b/c'
34
33
  end
35
34
 
36
35
  a_request(:post, notify_devices_endpoint).should have_been_made
37
36
  end
38
-
39
37
  end
40
38
 
41
39
  describe "#notify_by_tags" do
42
-
43
40
  let(:notify_by_tags_endpoint) { notifier_endpoint('toTags') }
44
41
 
45
42
  it "notifies devices by tags" do
@@ -47,15 +44,14 @@ describe FiveMobilePush::Notifier do
47
44
 
48
45
  subject.notify_by_tags([:iphone, :android], ['tag1', 'tag2']) do |message|
49
46
  message.body 'tag1 and tag2'
47
+ message.meta_data :a => '/a/b/c'
50
48
  end
51
49
 
52
50
  a_request(:post, notify_by_tags_endpoint).should have_been_made
53
51
  end
54
-
55
52
  end
56
53
 
57
54
  def notifier_endpoint(name)
58
55
  "https://push.fivemobile.com/rest/notify/#{name}"
59
56
  end
60
-
61
57
  end
@@ -1,6 +1,30 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe FiveMobilePush::Payload do
4
+ describe '#meta_data=' do
5
+ subject { Fabricate.build(:payload) }
6
+
7
+ it 'sets meta data' do
8
+ subject.meta_data = meta_data = { a: 'b' }
9
+ subject.meta_data.should == meta_data
10
+ end
11
+
12
+ it 'raises an ArgumentError if a Hash is not provided' do
13
+ expect {
14
+ subject.meta_data = nil
15
+ }.to raise_error(ArgumentError, 'meta_data must be a Hash')
16
+ end
17
+
18
+ it 'raises a MessageTooLargeError if a value is too large in the meta data' do
19
+ expect {
20
+ subject.meta_data = {
21
+ just_right: 'c' * FiveMobilePush::Payload::MAXIMUM_MESSAGE_LENGTH,
22
+ too_long: 'f' * FiveMobilePush::Payload::MAXIMUM_MESSAGE_LENGTH.next
23
+ }
24
+ }.to raise_error(FiveMobilePush::Payload::MessageTooLargeError)
25
+ end
26
+ end
27
+
4
28
  describe '#to_json' do
5
29
  subject { Fabricate.build(:payload) }
6
30
 
@@ -8,13 +32,9 @@ describe FiveMobilePush::Payload do
8
32
  subject.to_json.should include(subject.message)
9
33
  end
10
34
 
11
- it 'includes meta data' do
12
- subject.to_json.should include(MultiJson.encode(subject.meta_data))
13
- end
14
-
15
- it 'excludes meta data if there is none' do
16
- subject.meta_data = nil
17
- subject.to_json.should_not include('meta')
35
+ it 'includes any meta data' do
36
+ subject.meta_data = { a: 'b' }
37
+ subject.to_json.should include('"a":"b"')
18
38
  end
19
39
  end
20
40
  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.4.1
5
+ version: 0.4.2
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-05-02 00:00:00 -04:00
14
+ date: 2011-05-03 00:00:00 -04:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency