shove 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  gem install shove
8
8
 
9
9
  ##Install Step 2
10
- Grab your network id and API key from shove at http://shove.io/customer/network/api_access
10
+ Grab your network id and API key from shove at [http://shove.io/customer/network/api_access]
11
11
 
12
12
  ##Install Step 3
13
13
  Configure shover with your credentials
@@ -16,7 +16,8 @@
16
16
 
17
17
  Shove.configure(
18
18
  :network => "network",
19
- :key => "apikey"
19
+ :key => "apikey",
20
+ :cluster => "cluster"
20
21
  )
21
22
 
22
23
  ##Broadcast messages
@@ -47,3 +48,8 @@
47
48
  will leverage em-http-request. Otherwise, the requests fallback to net/http requests. We recommend
48
49
  using EM if possible.
49
50
 
51
+ ##CLI (Command line interface)
52
+ The shove gem comes with a command line tool for controlling the network.
53
+ View documentation @ [http://shove.io/documentation/cli]
54
+
55
+
data/bin/shove ADDED
@@ -0,0 +1,126 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "commander/import"
5
+ require "shove"
6
+
7
+ ymlpath = File.expand_path("~/.shove.yml")
8
+
9
+ # get network, key, cluster
10
+ unless FileTest.exist?(ymlpath)
11
+
12
+ access = {}
13
+
14
+ loop do
15
+
16
+ say "Please enter your network information."
17
+ say "Visit http://shove.io/customer/network/api_access for more info."
18
+
19
+ access[:cluster] = ask "Enter Cluster Group: "
20
+ access[:network] = ask "Enter Network Id: "
21
+ access[:key] = ask "Enter Network Key: "
22
+
23
+ Shove.configure access
24
+
25
+ check = Shove.validate
26
+ if check.status >= 400
27
+ say "Unable to validate network settings. Error: #{check.message}"
28
+ else
29
+ break
30
+ end
31
+
32
+ end
33
+
34
+ File.open(ymlpath, "w") do |f|
35
+ f << access.to_yaml
36
+ end
37
+
38
+ end
39
+
40
+ Shove.configure(ymlpath)
41
+
42
+ # Commander specification
43
+
44
+ program :name, "shove"
45
+ program :version, Shove::Version
46
+ program :description, "CLI for the shove.io push platform"
47
+ program :help, "Author", "Dan Simpson <dan@shove.io>"
48
+ program :formatter, :compact
49
+
50
+ command :stream do |c|
51
+ c.syntax = "shove stream channel"
52
+ c.example "Streams the default channel", "shove stream default"
53
+ c.description = "Stream all data on a given channel to the console"
54
+ c.action do |args, opts|
55
+ if args.empty?
56
+ say_invalid c
57
+ else
58
+ EM.run do
59
+ Shove.stream(args.first) { |m| say(m) }
60
+ end
61
+ end
62
+ end
63
+ end
64
+ alias_command :subscribe, :stream
65
+
66
+ command :broadcast do |c|
67
+ c.syntax = "shove broadcast channel event data"
68
+ c.example "Broadcast x,y coordinates on the grid channel", "shove broadcast grid coords '1522,234'"
69
+ c.description = "Broadcast event and data to all subscribers of a given channel"
70
+ c.action do |args, opts|
71
+ if args.length != 3
72
+ say_invalid c
73
+ else
74
+ say "Broadcasting on channel #{args.first}"
75
+ Shove.broadcast(*args) do |response|
76
+ say response.message
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ command :direct do |c|
83
+ c.syntax = "shove direct user event data"
84
+ c.example "Send a message to user with id 9812737890", "shove direct 9812737890 message 'Your account expires in 2 minutes'"
85
+ c.description = "Send a message directly to a specific shove subscriber"
86
+ c.action do |args, opts|
87
+ if args.length != 3
88
+ say_invalid c
89
+ else
90
+ say "Sending data directly to user #{args.first}"
91
+ Shove.direct(*args) do |response|
92
+ say response.message
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ command :authorize do |c|
99
+ c.syntax = "shove authorize user channel"
100
+ c.example "Authorize user 9812737890 access to channel internal-bus", "shove authorize 9812737890 internal-bus"
101
+ c.description = "Authorize a pending channel subscription. Only required for private channels."
102
+ c.action do |args, opts|
103
+ if args.length != 2
104
+ say_invalid c
105
+ else
106
+ say "Authorizing user #{args.first} on channel #{args.last}"
107
+ Shove.authorize(*args) do |response|
108
+ say response.message
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+
115
+ def say_invalid cmd
116
+ puts "Invalid #{cmd.name} command. Syntax: #{cmd.syntax}"
117
+ unless cmd.examples.empty?
118
+ puts "Examples"
119
+ cmd.examples.each do |ex|
120
+ puts "---------"
121
+ puts ex.first
122
+ puts ex.last
123
+ end
124
+ end
125
+ end
126
+
data/lib/shove.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  $:.unshift File.dirname(__FILE__)
2
2
 
3
3
  require "rubygems"
4
+ require "net/http"
4
5
  require "em-http-request"
5
6
  require "yaml"
6
7
 
@@ -10,7 +11,7 @@ require "yaml"
10
11
  # See http://shove.io for an account
11
12
  module Shove
12
13
 
13
- Version = 0.3
14
+ Version = 0.4
14
15
 
15
16
  class << self
16
17
 
@@ -20,6 +21,7 @@ module Shove
20
21
  # +settings+ the settings for the created client as
21
22
  # a string for a yaml file, or as a hash
22
23
  def configure settings
24
+
23
25
  if settings.kind_of? String
24
26
  self.config = YAML.load_file(settings)
25
27
  elsif settings.kind_of? Hash
@@ -28,6 +30,12 @@ module Shove
28
30
  raise "Unsupported configuration type"
29
31
  end
30
32
 
33
+ # symbolize keys
34
+ # self.config = {}
35
+ # tmp.each_pair do |k,v|
36
+ # self.config[k.to_sym] = v
37
+ # end
38
+
31
39
  self.client = Client.new config[:network], config[:key], config
32
40
  end
33
41
 
@@ -53,16 +61,49 @@ module Shove
53
61
  def authorize uid, channel="*", &block
54
62
  client.authorize uid, channel, &block
55
63
  end
64
+
65
+ # validate network settings
66
+ # used for the CLI
67
+ def validate
68
+ client.validate
69
+ end
56
70
 
57
71
  # act as a stream client. requires EM
58
72
  # +channel+ the channel to stream
59
73
  def stream channel, &block
74
+
60
75
  unless EM.reactor_running?
61
76
  raise "You can stream when running in an Eventmachine event loop. EM.run { #code here }"
62
77
  end
63
- raise "Websocket client not implemented yet... soon"
78
+
79
+ uid = ""
80
+
81
+ http = EventMachine::HttpRequest.new("ws://ws.shove.io/#{config[:network]}").get :timeout => 0
82
+
83
+ http.errback {
84
+ block.call("Connection Error")
85
+ }
86
+
87
+ http.callback {
88
+ block.call("Connected")
89
+ http.send("#{channel}!$subscribe!!!")
90
+ }
91
+
92
+ http.stream { |msg|
93
+
94
+ parts = msg.split "!"
95
+
96
+ case parts[1]
97
+ when "$identity"
98
+ uid = parts.last
99
+ when "$unauthorized"
100
+ Shove.authorize uid, channel
101
+ end
102
+
103
+ block.call(msg)
104
+ }
105
+
64
106
  end
65
-
66
107
  end
67
108
  end
68
109
 
data/lib/shove/client.rb CHANGED
@@ -9,10 +9,11 @@ module Shove
9
9
  # :host leave as default unless you are given a private cluster
10
10
  def initialize network, key, opts={}
11
11
  @network = network
12
+ @cluster = opts[:cluster] || "a04"
12
13
  @key = key
13
14
  @auth_header = { "api-key" => key }
14
15
  @secure = opts[:secure] || false
15
- @host = opts[:host] || "api.shove.io"
16
+ @host = opts[:host] || "api.#{@cluster}.shove.io"
16
17
  end
17
18
 
18
19
  # broadcast a message
@@ -38,6 +39,12 @@ module Shove
38
39
  Request.new("#{uri}/#{@network}/authorize/#{channel}/#{uid}", @auth_header).post(&block)
39
40
  end
40
41
 
42
+ def validate
43
+ Request.new("#{uri}/#{@network}/validate", @auth_header).post do |response|
44
+ return response
45
+ end
46
+ end
47
+
41
48
  protected
42
49
 
43
50
  def uri
data/lib/shove/request.rb CHANGED
@@ -8,7 +8,7 @@ module Shove
8
8
  self.headers = headers
9
9
  end
10
10
 
11
- def post params={}, &block
11
+ def post params="", &block
12
12
  if EM.reactor_running?
13
13
  http = EventMachine::HttpRequest.new(url).post(:body => params, :head => headers)
14
14
  if block
@@ -20,7 +20,11 @@ module Shove
20
20
  }
21
21
  end
22
22
  else
23
- raise "EM requests only for now."
23
+ uri = URI.parse(url)
24
+ res = Net::HTTP.new(uri.host, uri.port).post(uri.path, params, headers)
25
+ if block
26
+ block.call(Response.new(res.code, res.body, res.code.to_i < 400))
27
+ end
24
28
  end
25
29
  end
26
30
 
@@ -4,7 +4,7 @@ module Shove
4
4
  attr_accessor :status, :message, :error
5
5
 
6
6
  def initialize status, message, error=false
7
- self.status = status
7
+ self.status = status.to_i
8
8
  self.message = message
9
9
  self.error = error
10
10
  end
data/shove.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  spec = Gem::Specification.new do |s|
2
2
  s.name = "shove"
3
- s.version = "0.3"
3
+ s.version = "0.4"
4
4
  s.date = "2010-12-16"
5
5
  s.summary = "Ruby gem for leveraging shove.io, the web push platform"
6
6
  s.email = "dan@shove.io"
@@ -9,7 +9,11 @@ spec = Gem::Specification.new do |s|
9
9
  s.has_rdoc = true
10
10
 
11
11
  s.add_dependency("em-http-request", ">= 0.3.0")
12
+ s.add_dependency("commander", ">= 4.0.3")
12
13
 
14
+ s.bindir = "bin"
15
+ s.executables = ["shove"]
16
+
13
17
  s.authors = ["Dan Simpson"]
14
18
 
15
19
  s.files = [
@@ -21,6 +25,7 @@ spec = Gem::Specification.new do |s|
21
25
  "lib/shove.rb",
22
26
  "lib/shove/request.rb",
23
27
  "lib/shove/response.rb",
24
- "lib/shove/client.rb"
28
+ "lib/shove/client.rb",
29
+ "bin/shove"
25
30
  ]
26
31
  end
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- version: "0.3"
7
+ - 4
8
+ version: "0.4"
9
9
  platform: ruby
10
10
  authors:
11
11
  - Dan Simpson
@@ -31,10 +31,25 @@ dependencies:
31
31
  version: 0.3.0
32
32
  type: :runtime
33
33
  version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: commander
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 4
44
+ - 0
45
+ - 3
46
+ version: 4.0.3
47
+ type: :runtime
48
+ version_requirements: *id002
34
49
  description: Client side implementation for the shove.io API. See http://shove.io/documentation
35
50
  email: dan@shove.io
36
- executables: []
37
-
51
+ executables:
52
+ - shove
38
53
  extensions: []
39
54
 
40
55
  extra_rdoc_files: []
@@ -49,6 +64,7 @@ files:
49
64
  - lib/shove/request.rb
50
65
  - lib/shove/response.rb
51
66
  - lib/shove/client.rb
67
+ - bin/shove
52
68
  has_rdoc: true
53
69
  homepage: https://github.com/shove/shover
54
70
  licenses: []