shove 0.2

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.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ #shover
2
+
3
+ * The official ruby gem for shove.io's REST API
4
+
5
+ ##Install Step 1
6
+
7
+ gem install shove
8
+
9
+ ##Install Step 2
10
+ Grab your network id and API key from shove at http://shove.io/customer/network/api_access
11
+
12
+ ##Install Step 3
13
+ Configure shover with your credentials
14
+
15
+ require "shove"
16
+
17
+ Shove.configure(
18
+ :network => "network",
19
+ :key => "apikey"
20
+ )
21
+
22
+ ##Broadcast messages
23
+
24
+ # broadcast the "hello" event on the "default" channel with
25
+ # Hello World! as the data
26
+ Shove.broadcast("default", "hello", "Hello World!") do |response|
27
+ puts response.error?
28
+ puts response.status
29
+ puts response.message
30
+ end
31
+
32
+ ##Direct messages
33
+
34
+ Shove.direct(userid, "hello", "Hello World!") do |response|
35
+ # handle response
36
+ end
37
+
38
+ ##Authorize a user
39
+
40
+ # authorize user with id userid on channel "default"
41
+ Shove.authorize(userid, "default") do |response|
42
+ # handle response
43
+ end
44
+
45
+ ##Block and Non-blocking
46
+ shover does both. If the shover code happens to run inside of an EM.run block, the HTTP calls
47
+ will leverage em-http-request. Otherwise, the requests fallback to net/http requests. We recommend
48
+ using EM if possible.
49
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "rspec/core/rake_task"
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task :default => :spec
6
+
@@ -0,0 +1,48 @@
1
+ module Shove
2
+ class Client
3
+
4
+ # create an API client
5
+ # +network+ the network id
6
+ # +key+ the api access key
7
+ # +opts+ hash with a few options, such as:
8
+ # :secure true or false
9
+ # :host leave as default unless you are given a private cluster
10
+ def initialize network, key, opts={}
11
+ @network = network
12
+ @key = key
13
+ @auth_header = { "api-key" => key }
14
+ @secure = opts[:secure] || false
15
+ @host = opts[:host] || "api.shove.io"
16
+ end
17
+
18
+ # broadcast a message
19
+ # +channel+ the channel to broadcast on
20
+ # +event+ the event to trigger
21
+ # +message+ the message to send, UTF-8 encoded kthx
22
+ def broadcast channel, event, message, &block
23
+ Request.new("#{uri}/#{@network}/broadcast/#{channel}/#{event}", @auth_header).post(message, &block)
24
+ end
25
+
26
+ # direct a message to a specific user
27
+ # +uid+ the users id
28
+ # +event+ the event to trigger
29
+ # +message+ the message to send, UTF-8 encoded kthx
30
+ def direct uid, event, message, &block
31
+ Request.new("#{uri}/#{@network}/direct/#{event}/#{uid}", @auth_header).post(message, &block)
32
+ end
33
+
34
+ # authorize a user on a private channel
35
+ # +uid+ the users id
36
+ # +channel+ the channel to authorize them on
37
+ def authorize uid, channel="*", &block
38
+ Request.new("#{uri}/#{@network}/authorize/#{channel}/#{uid}", @auth_header).post(&block)
39
+ end
40
+
41
+ protected
42
+
43
+ def uri
44
+ (@secure ? "https" : "http") + "://#{@host}"
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,31 @@
1
+ module Shove
2
+ class Request
3
+
4
+ attr_accessor :url, :headers
5
+
6
+ def initialize url, headers={}
7
+ self.url = url
8
+ self.headers = headers
9
+ end
10
+
11
+ def post params={}, &block
12
+ if EM.reactor_running?
13
+ http = EventMachine::HttpRequest.new(url).post(:body => params, :head => headers)
14
+ if block
15
+ http.errback {
16
+ block.call(Response.new(http.response_header.status, http.response, true))
17
+ }
18
+ http.callback {
19
+ block.call(Response.new(http.response_header.status, http.response, false))
20
+ }
21
+ end
22
+ else
23
+ raise "EM requests only for now."
24
+ end
25
+ end
26
+
27
+ end
28
+ end
29
+
30
+
31
+
@@ -0,0 +1,17 @@
1
+ module Shove
2
+ class Response
3
+
4
+ attr_accessor :status, :message, :error
5
+
6
+ def initialize status, message, error=false
7
+ self.status = status
8
+ self.message = message
9
+ self.error = error
10
+ end
11
+
12
+ def error?
13
+ error
14
+ end
15
+
16
+ end
17
+ end
data/lib/shove.rb ADDED
@@ -0,0 +1,73 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require "rubygems"
4
+ require "eventmachine"
5
+ require "em-http"
6
+ require "yaml"
7
+ require "json"
8
+
9
+ ##
10
+ # Shove
11
+ #
12
+ # See http://shove.io for an account
13
+ module Shove
14
+
15
+ Version = 0.2
16
+
17
+ class << self
18
+
19
+ attr_accessor :config, :client
20
+
21
+ # configure shover
22
+ # +settings+ the settings for the created client as
23
+ # a string for a yaml file, or as a hash
24
+ def configure settings
25
+ if settings.kind_of? String
26
+ self.config = YAML.load_file(settings)
27
+ elsif settings.kind_of? Hash
28
+ self.config = settings
29
+ else
30
+ raise "Unsupported configuration type"
31
+ end
32
+
33
+ self.client = Client.new config[:network], config[:key], config
34
+ end
35
+
36
+ # broadcast a message
37
+ # +channel+ the channel to broadcast on
38
+ # +event+ the event to trigger
39
+ # +message+ the message to send, UTF-8 encoded kthx
40
+ def broadcast channel, event, message, &block
41
+ client.broadcast channel, event, message, &block
42
+ end
43
+
44
+ # direct a message to a specific user
45
+ # +uid+ the users id
46
+ # +event+ the event to trigger
47
+ # +message+ the message to send, UTF-8 encoded kthx
48
+ def direct uid, event, message, &block
49
+ client.direct uid, event, message, &block
50
+ end
51
+
52
+ # authorize a user on a private channel
53
+ # +uid+ the users id
54
+ # +channel+ the channel to authorize them on
55
+ def authorize uid, channel="*", &block
56
+ client.authorize uid, channel, &block
57
+ end
58
+
59
+ # act as a stream client. requires EM
60
+ # +channel+ the channel to stream
61
+ def stream channel, &block
62
+ unless EM.reactor_running?
63
+ raise "You can stream when running in an Eventmachine event loop. EM.run { #code here }"
64
+ end
65
+ raise "Websocket client not implemented yet... soon"
66
+ end
67
+
68
+ end
69
+ end
70
+
71
+ require "shove/client"
72
+ require "shove/request"
73
+ require "shove/response"
data/shove.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ spec = Gem::Specification.new do |s|
2
+ s.name = "shove"
3
+ s.version = "0.2"
4
+ s.date = "2010-12-16"
5
+ s.summary = "Ruby gem for leveraging shove.io, the web push platform"
6
+ s.email = "dan@shove.io"
7
+ s.homepage = "https://github.com/shove/shover"
8
+ s.description = "Client side implementation for the shove.io API. See http://shove.io/documentation"
9
+ s.has_rdoc = true
10
+
11
+ s.authors = ["Dan Simpson"]
12
+
13
+ s.files = [
14
+ "README.md",
15
+ "shove.gemspec",
16
+ "Rakefile",
17
+ "spec/helper.rb",
18
+ "spec/shove_spec.rb",
19
+ "lib/shove.rb",
20
+ "lib/shove/request.rb",
21
+ "lib/shove/response.rb",
22
+ "lib/shove/client.rb"
23
+ ]
24
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,3 @@
1
+ $:.unshift(File.dirname(__FILE__) + "/../lib/")
2
+
3
+ require "shove"
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + "/helper"
2
+
3
+ describe Shove do
4
+
5
+ before do
6
+ Shove.configure "./shove.yml"
7
+ end
8
+
9
+ it "should have a version" do
10
+ Shove.const_defined?("Version").should == true
11
+ end
12
+
13
+ it "should have config" do
14
+ Shove.config["network"].should == "shove"
15
+ end
16
+
17
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shove
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ version: "0.2"
9
+ platform: ruby
10
+ authors:
11
+ - Dan Simpson
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-12-16 00:00:00 -08:00
17
+ default_executable:
18
+ dependencies: []
19
+
20
+ description: Client side implementation for the shove.io API. See http://shove.io/documentation
21
+ email: dan@shove.io
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files: []
27
+
28
+ files:
29
+ - README.md
30
+ - shove.gemspec
31
+ - Rakefile
32
+ - spec/helper.rb
33
+ - spec/shove_spec.rb
34
+ - lib/shove.rb
35
+ - lib/shove/request.rb
36
+ - lib/shove/response.rb
37
+ - lib/shove/client.rb
38
+ has_rdoc: true
39
+ homepage: https://github.com/shove/shover
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.7
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Ruby gem for leveraging shove.io, the web push platform
70
+ test_files: []
71
+