Module: Viaduct::WebPush

Defined in:
lib/viaduct/web_push.rb,
lib/viaduct/web_push/channel.rb,
lib/viaduct/web_push/web_socket.rb,
lib/viaduct/web_push/web_socket/channel.rb,
lib/viaduct/web_push/web_socket/connection.rb,
lib/viaduct/web_push/web_socket/raw_socket.rb

Defined Under Namespace

Modules: WebSocket Classes: Channel, Error

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.endpointObject

Return the endpoint for API request



23
24
25
# File 'lib/viaduct/web_push.rb', line 23

def endpoint
  @endpoint ||= "https://#{self.webpush_host}/vwp/api"
end

.secretObject

Return the application secret



37
38
39
# File 'lib/viaduct/web_push.rb', line 37

def secret
  @secret || ENV['WEBPUSH_SECRET'] || raise(Error, "Must set `Viaduct::WebPush.secret` to an application secret")
end

.tokenObject

Return the application token



30
31
32
# File 'lib/viaduct/web_push.rb', line 30

def token
  @token || ENV['WEBPUSH_TOKEN'] || raise(Error, "Must set `Viaduct::WebPush.token` to an application token")
end

.webpush_hostObject

Return the host for the VWP service



16
17
18
# File 'lib/viaduct/web_push.rb', line 16

def webpush_host
  @webpush_host ||= 'webpush.viaduct.io'
end

Class Method Details

.[](name) ⇒ Object

Initialize a new channel with the given name (caching it for future use)



52
53
54
55
# File 'lib/viaduct/web_push.rb', line 52

def [](name)
  @channels ||= {}
  @channels[name] ||= Channel.new(name)
end

.request(action, params = {}) ⇒ Object

Make an HTTP request to the WebPush API



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/viaduct/web_push.rb', line 61

def request(action, params = {})
  uri = URI.parse(self.endpoint + "/#{action}")
  request = Net::HTTP::Post.new(uri.path)
  request.set_form_data(params.merge(:token => self.token, :secret => self.secret))
  http = Net::HTTP.new(uri.host, uri.port)
  if uri.scheme == 'https'
    http.use_ssl = true
  end
  Timeout.timeout(5) do
    result = http.request(request)
    result.is_a?(Net::HTTPSuccess)
  end
rescue Exception, Timeout::Error => e
  raise Error, "An error occurred while sending data to the WebPush HTTP API. #{e.to_s}"
end