beaconpush 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,14 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ beaconpush (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ beaconpush!
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler"
2
+
3
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "beaconpush/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "beaconpush"
7
+ s.version = Beaconpush::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jakub Kuźma"]
10
+ s.email = ["qoobaa@gmail.com"]
11
+ s.homepage = "http://rubygems.org/gems/beaconpush"
12
+ s.summary = %q{Gem for adding Beacon support into your application}
13
+ s.description = %q{Gem for adding Beacon support into your application}
14
+
15
+ s.rubyforge_project = "beaconpush"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
data/lib/beaconpush.rb ADDED
@@ -0,0 +1,31 @@
1
+ require "json"
2
+ require "uri"
3
+ require "forwardable"
4
+
5
+ require "beaconpush/client"
6
+ require "beaconpush/response_error"
7
+
8
+ module Beaconpush
9
+ class << self
10
+ extend Forwardable
11
+ attr_accessor :api_key, :secret_key
12
+ attr_writer :version, :host, :port
13
+ def_delegators :client, :users_count, :user_online?, :user_logout, :user_message, :channel_message, :channel_users
14
+
15
+ def host
16
+ @host ||= "beaconpush.com"
17
+ end
18
+
19
+ def port
20
+ @port ||= 80
21
+ end
22
+
23
+ def version
24
+ @version ||= "1.0.0"
25
+ end
26
+
27
+ def client
28
+ @client ||= Client.new(:api_key => api_key, :secret_key => secret_key, :version => version, :host => host, :port => port)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,67 @@
1
+ module Beaconpush
2
+ class Client
3
+ attr_accessor :api_key, :secret_key, :version, :host, :port
4
+
5
+ def initialize(options)
6
+ self.api_key = options.fetch(:api_key, Beaconpush.api_key) || raise(ArgumentError, "No API key given")
7
+ self.secret_key = options.fetch(:secret_key, Beaconpush.secret_key) || raise(ArgumentError, "No secret key given")
8
+ self.version = options.fetch(:version, Beaconpush.version) || raise(ArgumentError, "No API version given")
9
+ self.host = options.fetch(:host, Beaconpush.host) || raise(ArgumentError, "No Beacon host given")
10
+ self.port = options.fetch(:port, Beaconpush.port) || raise(ArgumentError, "No Beacon port given")
11
+ end
12
+
13
+ def users_count
14
+ request("GET", "/users")["online"]
15
+ end
16
+
17
+ def user_online?(user)
18
+ request("GET", "/users/#{user}")
19
+ true
20
+ rescue ResponseError => e
21
+ (e.response.code.to_i == 404) ? false : raise
22
+ end
23
+
24
+ def user_logout(user)
25
+ request("DELETE", "/users/#{user}")
26
+ end
27
+
28
+ def user_message(user, message)
29
+ request("POST", "/users/#{user}", message.to_json)["messages_sent"]
30
+ end
31
+
32
+ def channel_message(channel, message)
33
+ request("POST", "/channels/#{channel}", message.to_json)["messages_sent"]
34
+ end
35
+
36
+ def channel_users(channel)
37
+ request("GET", "/channels/#{channel}")["users"]
38
+ end
39
+
40
+ private
41
+
42
+ def http
43
+ Net::HTTP.new(host, port)
44
+ end
45
+
46
+ def request(method, command, body = nil)
47
+ path = "/api/#{version}/#{api_key}#{URI.encode(command)}"
48
+
49
+ request = Net::HTTPGenericRequest.new(method.to_s.upcase, !!body, method.to_s.upcase != "HEAD", path)
50
+
51
+ if body
52
+ request.body = body
53
+ request.content_length = body.size
54
+ end
55
+
56
+ request["X-Beacon-Secret-Key"] = secret_key
57
+
58
+ response = http.request(request)
59
+
60
+ if (200...300).include?(response.code.to_i)
61
+ JSON.parse(response.body) if response.body and response.body != ""
62
+ else
63
+ raise ResponseError.new(response)
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,14 @@
1
+ module Beaconpush
2
+ class ResponseError < StandardError
3
+ attr_reader :response
4
+
5
+ def initialize(response)
6
+ @response = response
7
+ super(message)
8
+ end
9
+
10
+ def message
11
+ JSON.parse(response.body.to_s)["message"] rescue "Server responded with an error"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Beaconpush
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beaconpush
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - "Jakub Ku\xC5\xBAma"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-03 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Gem for adding Beacon support into your application
22
+ email:
23
+ - qoobaa@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - Gemfile.lock
34
+ - Rakefile
35
+ - beaconpush.gemspec
36
+ - lib/beaconpush.rb
37
+ - lib/beaconpush/client.rb
38
+ - lib/beaconpush/response_error.rb
39
+ - lib/beaconpush/version.rb
40
+ has_rdoc: true
41
+ homepage: http://rubygems.org/gems/beaconpush
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project: beaconpush
68
+ rubygems_version: 1.3.7
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Gem for adding Beacon support into your application
72
+ test_files: []
73
+