five_mobile_push 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,4 +24,5 @@ Gem::Specification.new do |s|
24
24
  s.add_development_dependency "rspec", ["~> 2.5.0"]
25
25
  s.add_development_dependency "yajl-ruby"
26
26
  s.add_development_dependency "webmock"
27
+ s.add_development_dependency "fabrication", ["~> 0.9.5"]
27
28
  end
@@ -3,6 +3,8 @@ module FiveMobilePush
3
3
 
4
4
  VALID_OPTION_KEYS = [:alias, :email]
5
5
 
6
+ attr_reader :device_uid
7
+
6
8
  # @param [FiveMobilePush::Client] client The Client to use to send this request
7
9
  #
8
10
  # @param [String] device_uid The ID of the device being registered.
@@ -5,31 +5,54 @@ module FiveMobilePush
5
5
  @client = client
6
6
  end
7
7
 
8
- def broadcast(platforms, payload)
9
- @client.post 'notify/broadcast', :platforms => self.class.build_platforms_string(platforms), :payload => MultiJson.encode(payload)
8
+ def broadcast(platforms, &block)
9
+ @client.post 'notify/broadcast',
10
+ :platforms => build_platforms_string(platforms),
11
+ :payload => capture_payload(&block).to_json
10
12
  end
11
-
12
- def notify_devices(devices, payload)
13
- @client.post 'notify/toDevices', :id_type => FiveMobilePush::DEFAULT_ID_TYPE, :id_values => devices.join(','), :payload => MultiJson.encode(payload)
13
+
14
+ def notify_devices(devices, &block)
15
+ @client.post 'notify/toDevices',
16
+ :id_type => FiveMobilePush::DEFAULT_ID_TYPE,
17
+ :id_values => devices.join(','),
18
+ :payload => capture_payload(&block).to_json
14
19
  end
15
20
 
16
- def notify_by_tags(platforms, tags, payload)
17
- @client.post 'notify/toTags',
18
- :platforms => self.class.build_platforms_string(platforms),
19
- :tags => tags.join(','),
20
- :payload => MultiJson.encode(payload)
21
+ def notify_by_tags(platforms, tags, &block)
22
+ @client.post 'notify/toTags',
23
+ :platforms => build_platforms_string(platforms),
24
+ :tags => tags.join(','),
25
+ :payload => capture_payload(&block).to_json
21
26
  end
22
27
 
23
- class << self
28
+ private
29
+
30
+ def capture_payload(&block)
31
+ payload_proxy = PayloadProxy.new
32
+ block.call(payload_proxy)
33
+ payload_proxy.to_payload
34
+ end
24
35
 
25
- def build_platforms_string(platforms)
26
- if platforms.kind_of?(Enumerable)
27
- platforms.join(',')
28
- else
29
- platforms.to_s
30
- end
36
+ def build_platforms_string(platforms)
37
+ if platforms.kind_of?(Enumerable)
38
+ platforms.join(',')
39
+ else
40
+ platforms.to_s
31
41
  end
42
+ end
32
43
 
44
+ class PayloadProxy
45
+ def message(message)
46
+ @message = message
47
+ end
48
+
49
+ def meta_data(meta_data)
50
+ @meta_data = meta_data
51
+ end
52
+
53
+ def to_payload
54
+ FiveMobilePush::Payload.new(@message, @meta_data)
55
+ end
33
56
  end
34
57
 
35
58
  end
@@ -0,0 +1,39 @@
1
+ module FiveMobilePush
2
+ class Payload
3
+ attr_reader :message
4
+ attr_accessor :meta_data
5
+
6
+ # @param [#to_s] message The message you wish to send with a notice
7
+ # @param [Hash] meta_data (nil) Any meta data to send along with the
8
+ # notice. Leave as +nil+ if none is to be sent.
9
+ def initialize(message, meta_data=nil)
10
+ self.message = message
11
+ self.meta_data = meta_data
12
+ end
13
+
14
+ # @param [#to_s] message The message you wish to send with a notice
15
+ def message=(message)
16
+ @message = message.to_s
17
+ end
18
+
19
+ # @return [String] JSON representation of the Payload
20
+ def to_json
21
+ MultiJson.encode(as_json)
22
+ end
23
+
24
+ private
25
+
26
+ def as_json
27
+ payload = {
28
+ 'msg' => {
29
+ 'type' => 'string',
30
+ 'value' => message
31
+ },
32
+ 'sound' => 'default',
33
+ 'launch' => true
34
+ }
35
+ payload['meta'] = meta_data if meta_data
36
+ payload
37
+ end
38
+ end
39
+ end
@@ -1,3 +1,3 @@
1
1
  module FiveMobilePush
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -4,13 +4,14 @@ require 'five_mobile_push/client'
4
4
  module FiveMobilePush
5
5
  extend self
6
6
 
7
- autoload :Device, 'five_mobile_push/device'
7
+ autoload :Device, 'five_mobile_push/device'
8
8
  autoload :Notifier, 'five_mobile_push/notifier'
9
- autoload :Tag, 'five_mobile_push/tag'
9
+ autoload :Tag, 'five_mobile_push/tag'
10
+ autoload :Payload, 'five_mobile_push/payload'
10
11
 
11
12
  class UnauthorizedError < StandardError; end
12
- class GeneralError < StandardError; end
13
- class ServerError < StandardError; end
13
+ class GeneralError < StandardError; end
14
+ class ServerError < StandardError; end
14
15
 
15
16
 
16
17
  VALID_OPTION_KEYS = [:api_token, :application_uid]
@@ -0,0 +1,4 @@
1
+ Fabricator(:client, :class_name => FiveMobilePush::Client) do
2
+ api_token 'token'
3
+ application_uid 'nulayer'
4
+ end
@@ -0,0 +1,3 @@
1
+ Fabricator(:device, :class_name => FiveMobilePush::Device) do
2
+ on_init { init_with(Fabricate.build(:client), '2b6f0cc904d137be2e1730235f5664094b831186') }
3
+ end
@@ -0,0 +1,3 @@
1
+ Fabricator(:payload, :class_name => FiveMobilePush::Payload) do
2
+ on_init { init_with('bacon', 'foo' => 'bar') }
3
+ end
@@ -2,17 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  describe FiveMobilePush::Device do
4
4
 
5
- let(:api_token) { 'token' }
6
-
7
- let(:application_uid) { 'nulayer' }
8
-
9
- let(:client) { FiveMobilePush::Client.new(:api_token => api_token, :application_uid => application_uid) }
10
-
11
- let(:device_uid) { '2b6f0cc904d137be2e1730235f5664094b831186' }
5
+ let(:client) { Fabricate.build(:client) }
12
6
 
13
7
  let(:device_token) { 'ABCDEFG' }
14
8
 
15
- subject { FiveMobilePush::Device.new(client, device_uid) }
9
+ subject { Fabricate.build(:device) }
16
10
 
17
11
  describe '#register' do
18
12
 
@@ -29,7 +23,7 @@ describe FiveMobilePush::Device do
29
23
 
30
24
  context "registration data is provided" do
31
25
 
32
- let(:body) { build_request_body(:device_id => device_uid, :reg_data => device_token, :device_info => MultiJson.encode(device_info)) }
26
+ let(:body) { build_request_body(client, :device_id => subject.device_uid, :reg_data => device_token, :device_info => MultiJson.encode(device_info)) }
33
27
 
34
28
  before(:each) do
35
29
  stub_request(:post, register_endpoint).to_return(:body => load_fixture('register.json'))
@@ -50,7 +44,7 @@ describe FiveMobilePush::Device do
50
44
  context "registration data is not provided" do
51
45
 
52
46
  it "registers a device" do
53
- body = build_request_body(:device_id => device_uid, :device_info => device_info)
47
+ body = build_request_body(client, :device_id => subject.device_uid, :device_info => device_info)
54
48
  stub_request(:post, register_endpoint)
55
49
  subject.register(device_info)
56
50
 
@@ -63,7 +57,7 @@ describe FiveMobilePush::Device do
63
57
 
64
58
  context "id_value and id_type passed to service" do
65
59
 
66
- let(:body) { build_request_body(:id_type => FiveMobilePush::DEFAULT_ID_TYPE, :id_value => device_uid) }
60
+ let(:body) { build_request_body(client, :id_type => FiveMobilePush::DEFAULT_ID_TYPE, :id_value => subject.device_uid) }
67
61
 
68
62
  describe '#resume' do
69
63
 
@@ -3,28 +3,20 @@ require 'uri'
3
3
 
4
4
  describe FiveMobilePush::Notifier do
5
5
 
6
- let(:api_token) { 'token' }
7
- let(:application_uid) { 'nulayer' }
8
- let(:client) { FiveMobilePush::Client.new :api_token => api_token, :application_uid => application_uid }
9
-
10
- subject { FiveMobilePush::Notifier.new client }
11
-
12
- let(:payload) do
13
- {
14
- :msg => {
15
- :type => "string",
16
- :value => "You sir are awesome"
17
- }
18
- }
19
- end
6
+ let(:client) { Fabricate.build(:client) }
20
7
 
21
- describe "#broadcast" do
8
+ subject { FiveMobilePush::Notifier.new(client) }
22
9
 
10
+ describe "#broadcast" do
23
11
  let(:broadcast_endpoint) { notifier_endpoint('broadcast') }
24
12
 
25
13
  it "broadcasts a notification to one or more platforms of the application" do
26
14
  stub_request(:post, broadcast_endpoint)
27
- subject.broadcast :iphone, payload
15
+
16
+ subject.broadcast(:iphone) do |payload|
17
+ payload.message "Minor downtime tonight from 7PM-9PM EST"
18
+ end
19
+
28
20
  a_request(:post, broadcast_endpoint).should have_been_made
29
21
  end
30
22
 
@@ -36,7 +28,11 @@ describe FiveMobilePush::Notifier do
36
28
 
37
29
  it "notifies a list of devices" do
38
30
  stub_request(:post, notify_devices_endpoint)
39
- subject.notify_devices ['abc', 'def'], payload
31
+
32
+ subject.notify_devices(['abc', 'def']) do |payload|
33
+ payload.message 'You win a prize!'
34
+ end
35
+
40
36
  a_request(:post, notify_devices_endpoint).should have_been_made
41
37
  end
42
38
 
@@ -48,7 +44,11 @@ describe FiveMobilePush::Notifier do
48
44
 
49
45
  it "notifies devices by tags" do
50
46
  stub_request(:post, notify_by_tags_endpoint)
51
- subject.notify_by_tags [:iphone, :android], ['tag1', 'tag2'], payload
47
+
48
+ subject.notify_by_tags([:iphone, :android], ['tag1', 'tag2']) do |payload|
49
+ payload.message 'tag1 and tag2'
50
+ end
51
+
52
52
  a_request(:post, notify_by_tags_endpoint).should have_been_made
53
53
  end
54
54
 
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe FiveMobilePush::Payload do
4
+ describe '#to_json' do
5
+ subject { Fabricate.build(:payload) }
6
+
7
+ it 'includes the message' do
8
+ subject.to_json.should include(subject.message)
9
+ end
10
+
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')
18
+ end
19
+ end
20
+ end
@@ -2,12 +2,10 @@ require 'spec_helper'
2
2
 
3
3
  describe FiveMobilePush::Tag do
4
4
 
5
- let(:api_token) { 'token' }
6
- let(:application_uid) { 'nulayer' }
7
- let(:client) { FiveMobilePush::Client.new :api_token => api_token, :application_uid => application_uid }
5
+ let(:client) { Fabricate.build(:client) }
8
6
  let(:device_uid) { 'ABCD123' }
9
7
 
10
- subject { FiveMobilePush::Tag.new client, device_uid }
8
+ subject { FiveMobilePush::Tag.new(client, device_uid) }
11
9
 
12
10
  describe "#create" do
13
11
 
@@ -15,14 +13,14 @@ describe FiveMobilePush::Tag do
15
13
  let(:tags) { %w(tag1 tag2) }
16
14
 
17
15
  it "adds new tags to Five Mobile" do
18
- body = build_request_body(:id_type => FiveMobilePush::DEFAULT_ID_TYPE, :id_value => device_uid, :tags => escape(tags.join(',')) )
16
+ body = build_request_body(client, :id_type => FiveMobilePush::DEFAULT_ID_TYPE, :id_value => device_uid, :tags => escape(tags.join(',')) )
19
17
  stub_request(:post, add_tag_endpoint).with(:body => body)
20
18
  subject.create tags
21
19
  a_request(:post, add_tag_endpoint).with(:body => body).should have_been_made
22
20
  end
23
21
 
24
22
  it "adds a new tag to Five Mobile" do
25
- body = build_request_body(:id_type => FiveMobilePush::DEFAULT_ID_TYPE, :id_value => device_uid, :tags => "cheese")
23
+ body = build_request_body(client, :id_type => FiveMobilePush::DEFAULT_ID_TYPE, :id_value => device_uid, :tags => "cheese")
26
24
  stub_request(:post, add_tag_endpoint).with(:body => body)
27
25
  subject.create("cheese")
28
26
  a_request(:post, add_tag_endpoint).with(:body => body).should have_been_made
@@ -36,14 +34,14 @@ describe FiveMobilePush::Tag do
36
34
  let(:delete_tag_endpoint) { tag_endpoint("delete") }
37
35
 
38
36
  it "unsubscribes from further notifications for tags" do
39
- body = build_request_body(:id_type => FiveMobilePush::DEFAULT_ID_TYPE, :id_value => device_uid, :tags => escape(tags.join(',')))
37
+ body = build_request_body(client, :id_type => FiveMobilePush::DEFAULT_ID_TYPE, :id_value => device_uid, :tags => escape(tags.join(',')))
40
38
  stub_request(:post, delete_tag_endpoint).with(:body => body)
41
39
  subject.delete(tags)
42
40
  a_request(:post, delete_tag_endpoint).with(:body => body).should have_been_made
43
41
  end
44
42
 
45
43
  it "unsubscribes from further notifications for tag" do
46
- body = build_request_body(:id_type => FiveMobilePush::DEFAULT_ID_TYPE, :id_value => device_uid, :tags => "bacon")
44
+ body = build_request_body(client, :id_type => FiveMobilePush::DEFAULT_ID_TYPE, :id_value => device_uid, :tags => "bacon")
47
45
  stub_request(:post, delete_tag_endpoint).with(:body => body)
48
46
  subject.delete("bacon")
49
47
  a_request(:post, delete_tag_endpoint).with(:body => body).should have_been_made
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,7 @@ require 'rspec'
3
3
  require 'webmock/rspec'
4
4
  require 'yajl'
5
5
  require 'uri'
6
+ require 'fabrication'
6
7
 
7
8
  RSpec.configure do |config|
8
9
  config.mock_with :rspec
@@ -12,8 +13,8 @@ def load_fixture(name)
12
13
  File.read File.expand_path("../fixtures/#{name}", __FILE__)
13
14
  end
14
15
 
15
- def build_request_body(data)
16
- data = data.merge(:api_token => api_token, :application_id => application_uid)
16
+ def build_request_body(client, data)
17
+ data = data.merge(:api_token => client.api_token, :application_id => client.application_uid)
17
18
  String.new.tap do |body|
18
19
  data.each do |key,value|
19
20
  body << "#{key}=#{value}"
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.2.0
5
+ version: 0.3.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-01 00:00:00 -04:00
14
+ date: 2011-04-06 00:00:00 -04:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
@@ -69,6 +69,17 @@ dependencies:
69
69
  version: "0"
70
70
  type: :development
71
71
  version_requirements: *id005
72
+ - !ruby/object:Gem::Dependency
73
+ name: fabrication
74
+ prerelease: false
75
+ requirement: &id006 !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ~>
79
+ - !ruby/object:Gem::Version
80
+ version: 0.9.5
81
+ type: :development
82
+ version_requirements: *id006
72
83
  description: API wrapper for Five Mobile Push notification service
73
84
  email:
74
85
  - kevin.faustino@gmail.com
@@ -90,11 +101,16 @@ files:
90
101
  - lib/five_mobile_push/client.rb
91
102
  - lib/five_mobile_push/device.rb
92
103
  - lib/five_mobile_push/notifier.rb
104
+ - lib/five_mobile_push/payload.rb
93
105
  - lib/five_mobile_push/tag.rb
94
106
  - lib/five_mobile_push/version.rb
107
+ - spec/fabricators/client.rb
108
+ - spec/fabricators/device.rb
109
+ - spec/fabricators/payload.rb
95
110
  - spec/five_mobile_push/client_spec.rb
96
111
  - spec/five_mobile_push/device_spec.rb
97
112
  - spec/five_mobile_push/notifier_spec.rb
113
+ - spec/five_mobile_push/payload_spec.rb
98
114
  - spec/five_mobile_push/tags_spec.rb
99
115
  - spec/five_mobile_push_spec.rb
100
116
  - spec/fixtures/register.json
@@ -128,9 +144,13 @@ signing_key:
128
144
  specification_version: 3
129
145
  summary: API wrapper for Five Mobile Push notification service
130
146
  test_files:
147
+ - spec/fabricators/client.rb
148
+ - spec/fabricators/device.rb
149
+ - spec/fabricators/payload.rb
131
150
  - spec/five_mobile_push/client_spec.rb
132
151
  - spec/five_mobile_push/device_spec.rb
133
152
  - spec/five_mobile_push/notifier_spec.rb
153
+ - spec/five_mobile_push/payload_spec.rb
134
154
  - spec/five_mobile_push/tags_spec.rb
135
155
  - spec/five_mobile_push_spec.rb
136
156
  - spec/fixtures/register.json