viaduct-webpush 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 18d110157e02cd4d3a06114d0cf940ac589edf46
4
+ data.tar.gz: 863edbc6adc567e905450a95b828c2294eecef39
5
+ SHA512:
6
+ metadata.gz: bc5c4f548d9d33f4d97a2ad9381d61270fb9159a866c95780111f439323aa1b38b9904005d0aae4f3136e09fdd84af1e1f2c295d9d63af0c05d7e667cef0fb2f
7
+ data.tar.gz: 4922ed5f743ac54afb41b5a5a0aabee3721343e82212c181dd4f76ff79cf95402d117b8c4a360b92514b08c42f1396f0e42e07588d30f250c64861d7240efdab
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Viaduct WebPush Ruby Client
2
+
3
+ This is a Ruby Client for the Viaduct WebPush service. The WebPush service
4
+ allows you to easily provide real-time messaging to your users in their browsers.
5
+
6
+ ## Installation
7
+
8
+ Just add the `viaduct-webpush` gem to your `Gemfile` and run `bundle` to install
9
+ it.
10
+
11
+ ```ruby
12
+ gem 'viaduct-webpush', :require => 'viaduct/webpush'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ require 'viaduct/webpush'
19
+
20
+ Viaduct::WebPush.token = 'your-token'
21
+ Viaduct::WebPush.secret = 'your-secret'
22
+
23
+ # Sending a single message
24
+ Viaduct::WebPush['test-channel'].trigger('say-hello', {
25
+ :name => 'Adam'
26
+ })
27
+
28
+ # Sending a message to multiple channels
29
+ Viaduct::WebPush::Channel.multi_trigger(['channel1', 'channel2'], 'say-hello', {
30
+ :name => 'Adam'
31
+ })
32
+ ```
@@ -0,0 +1,70 @@
1
+ require 'json'
2
+ require 'uri'
3
+ require 'net/https'
4
+ require 'viaduct/web_push/channel'
5
+
6
+ module Viaduct
7
+ module WebPush
8
+
9
+ class Error < StandardError; end
10
+
11
+ class << self
12
+
13
+ #
14
+ # Return the endpoint for API request
15
+ #
16
+ def endpoint
17
+ @endpoint ||= 'https://webpush.viaduct.io/vwp/api'
18
+ end
19
+
20
+ #
21
+ # Return the application token
22
+ #
23
+ def token
24
+ @token || raise(Error, "Must set `Viaudct::WebPush.token` to an application token")
25
+ end
26
+
27
+ #
28
+ # Return the application secret
29
+ #
30
+ def secret
31
+ @secret || raise(Error, "Must set `Viaudct::WebPush.secret` to an application secret")
32
+ end
33
+
34
+ #
35
+ # Allow some configuration to be overridden/set
36
+ #
37
+ attr_writer :endpoint
38
+ attr_writer :token
39
+ attr_writer :secret
40
+
41
+ #
42
+ # Initialize a new channel with the given name (caching it for future use)
43
+ #
44
+ def [](name)
45
+ @channels ||= {}
46
+ @channels[name] ||= Channel.new(name)
47
+ end
48
+
49
+ #
50
+ # Make an HTTP request to the WebPush API
51
+ #
52
+ def request(action, params = {})
53
+ uri = URI.parse(self.endpoint + "/#{action}")
54
+ request = Net::HTTP::Post.new(uri.path)
55
+ request.set_form_data(params.merge(:token => self.token, :secret => self.secret))
56
+ http = Net::HTTP.new(uri.host, uri.port)
57
+ if uri.scheme == 'https'
58
+ http.use_ssl = true
59
+ end
60
+ Timeout.timeout(5) do
61
+ result = http.request(request)
62
+ result.is_a?(Net::HTTPSuccess)
63
+ end
64
+ rescue Exception, Timeout::Error => e
65
+ raise Error, "An error occurred while sending data to the WebPush HTTP API. #{e.to_s}"
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,34 @@
1
+ module Viaduct
2
+ module WebPush
3
+ class Channel
4
+
5
+ def initialize(name)
6
+ @name = name
7
+ end
8
+
9
+ #
10
+ # Trigger an event on this channel
11
+ #
12
+ def trigger(event, data = {})
13
+ self.class.trigger(@name, event, data)
14
+ end
15
+
16
+ #
17
+ # Trigger a single even on a given channel
18
+ #
19
+ def self.trigger(channel, event, data = {})
20
+ WebPush.request('trigger', {:channel => channel, :event => event, :data => data.to_json})
21
+ end
22
+
23
+
24
+ #
25
+ # Trigger an event on multiple channels simultaneously
26
+ #
27
+ def self.multi_trigger(channels, event, data = {})
28
+ raise Error, "`channels` must an arrayof strings" unless channels.all? { |c| c.is_a?(String) }
29
+ WebPush.request('trigger', {:channel => channels.join(','), :event => event, :data => data.to_json})
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1 @@
1
+ require 'viadut/web_push'
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: viaduct-webpush
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Cooke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.8'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2'
33
+ description: A client library allows messages to be sent to the WebPush API.
34
+ email:
35
+ - adam@viaduct.io
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - README.md
41
+ - lib/viaduct/web_push.rb
42
+ - lib/viaduct/web_push/channel.rb
43
+ - lib/viaduct/webpush.rb
44
+ homepage: http://viaduct.io
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.2.2
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: A client library for the Viaduct WebPush API.
68
+ test_files: []
69
+ has_rdoc: