viaduct-api 1.0.5 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6fb34a04f6e55e7114411073a865510f0cf0df4d
4
- data.tar.gz: a03996f629235d61e4b3e65404d6a2baf11759a6
3
+ metadata.gz: cb00a240bc610a40492bb2ba7c9b7e7d5f6e05c8
4
+ data.tar.gz: 0fa62e1faf2ce0d684041cb75919198e6defe41a
5
5
  SHA512:
6
- metadata.gz: 6d4cb62c810e77b18dd1dfe7bf6b57ebaf12748211f37a4670c7671770dfcd7ec326137dcf5caa8ee8fdfe3e85c49169695b7f862fb3eda5ed9ae95cf0e300dc
7
- data.tar.gz: 08bb5fdc882bb22e3c4a5208b4383031b6b0af79f6d7d18f267bb033743decb9fd2350b90c287685e99a9532542e64354dfebd2b35ec7eba183f7c9392f9ade0
6
+ metadata.gz: 1acfb380ed0e2094620cab1c372e824470375ccdb1876a139efd64c20578ec4dd6bf9b561ef0fd81bfdeb0cb4c83aa6835d7b91aba69b1c6d9dc3dda05abd3a1
7
+ data.tar.gz: df56aa672060f7c7abdd52d07a28461d93aba1fce57d4441b5b36290efb22cd0b0bce7f78ba3876bc5d19c39ff9f9da94e340a48d661d9d0740616919a827eca
@@ -1,9 +1,13 @@
1
1
  require 'moonrope_client'
2
+ require 'viaduct/api/push_client'
2
3
 
3
4
  module Viaduct
4
5
  module API
5
6
  class Client
6
7
 
8
+ attr_reader :token
9
+ attr_reader :secret
10
+
7
11
  def initialize(token = nil, secret = nil)
8
12
  @token, @secret = token, secret
9
13
  end
@@ -16,6 +20,18 @@ module Viaduct
16
20
  moonrope.controller(*args)
17
21
  end
18
22
 
23
+ def push
24
+ @push_client ||= PushClient.new(self)
25
+ end
26
+
27
+ def token
28
+ @token
29
+ end
30
+
31
+ def secret
32
+ @secret
33
+ end
34
+
19
35
  def method_missing(name, value = nil)
20
36
  value.nil? ? controller(name) : super
21
37
  end
@@ -0,0 +1,49 @@
1
+ require 'json'
2
+ require 'faye/websocket'
3
+
4
+ module Viaduct
5
+ module API
6
+ class PushClient
7
+
8
+ attr_reader :url
9
+ attr_reader :ws
10
+
11
+ def initialize(api_client)
12
+ @url = "#{Viaduct::API.ssl ? 'wss' : 'ws'}://#{Viaduct::API.host}:#{Viaduct::API.push_port}/push.ws/api:#{api_client.token}:#{api_client.secret}"
13
+ @ws = Faye::WebSocket::Client.new(@url)
14
+ end
15
+
16
+ def connected(&block)
17
+ @ws.on :open do |event|
18
+ block.call(event)
19
+ end
20
+ end
21
+
22
+ def receive(&block)
23
+ @ws.on :message do |event|
24
+ json = JSON.parse(event.data)
25
+ if json['type'] == 'push'
26
+ block.call(json['exchange'].to_sym, json['routing_key'].to_s, json['body'])
27
+ else
28
+ block.call(:global, nil, json)
29
+ end
30
+ end
31
+ end
32
+
33
+ def disconnected(&block)
34
+ @ws.on :close do |event|
35
+ block.call(event)
36
+ end
37
+ end
38
+
39
+ def send(type, data = {})
40
+ @ws.send(data.merge(:type => type).to_json)
41
+ end
42
+
43
+ def subscribe(exchange, routing_key)
44
+ send(:subscribe, :exchange => exchange, :routing_key => routing_key.to_s)
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -1,5 +1,5 @@
1
1
  module Viaduct
2
2
  module API
3
- VERSION = '1.0.5'
3
+ VERSION = '1.0.6'
4
4
  end
5
5
  end
data/lib/viaduct/api.rb CHANGED
@@ -8,6 +8,7 @@ module Viaduct
8
8
  attr_accessor :application_token
9
9
  attr_accessor :host
10
10
  attr_accessor :port
11
+ attr_accessor :push_port
11
12
  attr_accessor :ssl
12
13
 
13
14
  def host
@@ -22,6 +23,10 @@ module Viaduct
22
23
  @port || ENV['VIADUCT_API_PORT'] || (self.ssl ? 443 : 80)
23
24
  end
24
25
 
26
+ def push_port
27
+ @push_port || ENV['VIADUCT_API_PUSH_PORT'] || self.port
28
+ end
29
+
25
30
  def application_token
26
31
  @application_token || ENV['VIADUCT_API_APP']
27
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: viaduct-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-23 00:00:00.000000000 Z
11
+ date: 2014-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: moonrope-client
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: faye-websocket
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.7.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.7.5
27
41
  description: A full client library allows requests to made to the Viaduct API.
28
42
  email:
29
43
  - adam@viaduct.io
@@ -33,6 +47,7 @@ extra_rdoc_files: []
33
47
  files:
34
48
  - lib/viaduct/api.rb
35
49
  - lib/viaduct/api/client.rb
50
+ - lib/viaduct/api/push_client.rb
36
51
  - lib/viaduct/api/version.rb
37
52
  homepage: http://viaduct.io
38
53
  licenses: