dimma 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ .yardoc
23
+ doc
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Kim Burgestrand
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,29 @@
1
+ What is Dimma?
2
+ ==============
3
+ Dimma is a Ruby library aimed to create a simple interface to [Beacons REST API](http://www.beaconpush.com/), built on top of the sweet [REST Client](http://github.com/archiloque/rest-client).
4
+
5
+ How do I use it?
6
+ ----------------
7
+ Excellent question! This is how:
8
+
9
+ # Set up the session.
10
+ beacon = Dimma.new(API_KEY, SECRET_KEY)
11
+
12
+ # Send a message to all users in the default channel.
13
+ beacon.message "We now have a total of #{beacon.users} users online!"
14
+
15
+ # Send a message to all users in the “chunky” channel.
16
+ chunky = beacon.channel "chunky"
17
+ chunky.message "Chunky bacon!"
18
+ # Spit out all the channel users in the “chunky” channel.
19
+ chunky.message "Current users in “chunky” channel: #{chunky.users.map(&:name).join ', '}"
20
+
21
+ # Send a message to a specific user if she is online.
22
+ candy = beacon.user "godisnappen88"
23
+ candy.message 'A/S/L?' if candy.online?
24
+
25
+ Really? Yeah, really! For more advanced usage I suggest you [read the documentation](http://rdoc.info/projects/Burgestrand/Dimma).
26
+
27
+ Miscellaneous notes
28
+ -------------------
29
+ - Dimma uses [Semantic Versioning](http://semver.org/) as of version v1.0.0!
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ gem.name = "dimma"
7
+ gem.summary = "A Ruby library for the Beacon REST API"
8
+ gem.email = "kim@burgestrand.se"
9
+ gem.homepage = "http://github.com/Burgestrand/Dimma"
10
+ gem.authors = ["Kim Burgestrand"]
11
+ gem.license = "X11 License"
12
+ gem.add_dependency "rest-client"
13
+ gem.add_dependency "json"
14
+ gem.add_development_dependency "webmock"
15
+ gem.add_development_dependency "rspec", ">= 1.2.9"
16
+ gem.add_development_dependency "yard", ">= 0"
17
+ gem.required_ruby_version = '>= 1.8.7'
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require 'spec/rake/spectask'
25
+ Spec::Rake::SpecTask.new(:spec) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.spec_files = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
31
+ spec.libs << 'lib' << 'spec'
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :spec => :check_dependencies
37
+
38
+ task :default => :spec
39
+
40
+ begin
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
43
+ rescue LoadError
44
+ task :yardoc do
45
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
46
+ end
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/dimma.gemspec ADDED
@@ -0,0 +1,68 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{dimma}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kim Burgestrand"]
12
+ s.date = %q{2010-07-05}
13
+ s.email = %q{kim@burgestrand.se}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.markdown",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "dimma.gemspec",
26
+ "lib/Dimma.rb",
27
+ "spec/dimma_spec.rb",
28
+ "spec/spec.opts",
29
+ "spec/spec_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/Burgestrand/Dimma}
32
+ s.licenses = ["X11 License"]
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.required_ruby_version = Gem::Requirement.new(">= 1.8.7")
36
+ s.rubygems_version = %q{1.3.7}
37
+ s.summary = %q{A Ruby library for the Beacon REST API}
38
+ s.test_files = [
39
+ "spec/dimma_spec.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
+ s.add_runtime_dependency(%q<rest-client>, [">= 0"])
49
+ s.add_runtime_dependency(%q<json>, [">= 0"])
50
+ s.add_development_dependency(%q<webmock>, [">= 0"])
51
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
52
+ s.add_development_dependency(%q<yard>, [">= 0"])
53
+ else
54
+ s.add_dependency(%q<rest-client>, [">= 0"])
55
+ s.add_dependency(%q<json>, [">= 0"])
56
+ s.add_dependency(%q<webmock>, [">= 0"])
57
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
58
+ s.add_dependency(%q<yard>, [">= 0"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<rest-client>, [">= 0"])
62
+ s.add_dependency(%q<json>, [">= 0"])
63
+ s.add_dependency(%q<webmock>, [">= 0"])
64
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
65
+ s.add_dependency(%q<yard>, [">= 0"])
66
+ end
67
+ end
68
+
data/lib/Dimma.rb ADDED
@@ -0,0 +1,135 @@
1
+ require 'delegate'
2
+ require 'rest-client'
3
+ require 'json'
4
+
5
+ # See http://beaconpush.com/guide for specification details.
6
+ #
7
+ module Dimma
8
+ # The Beacon API (http://beaconpush.com/guide/rest-api) version Dimma conforms to.
9
+ API_VERSION = "1.0.0"
10
+
11
+ # @param (see Session#initialize)
12
+ # @return (see Session#initialize)
13
+ # @see Session#initialize
14
+ def Dimma.new(*args)
15
+ Session.new *args
16
+ end
17
+
18
+ # Missing method calls are delegated to the underlying RestClient::Resource
19
+ class Session < SimpleDelegator
20
+ # Initialize the Beacon API resource; you get both your API key and secret key from http://beaconpush.com/account/settings.
21
+ #
22
+ # @example
23
+ # # Specify that no requests must take longer than 10 seconds.
24
+ # dimma = Dimma::Session.new "key", "secret", :timeout => 10
25
+ #
26
+ # @param [#to_s] key API key.
27
+ # @param [nil, #to_s] secret Secret key. Set to nil if you have turned off authentication.
28
+ # @param [Hash] options (see RestClient#initialize)
29
+ def initialize(key, secret = nil, options = {})
30
+ url = "http://api.beaconpush.com/#{Dimma::API_VERSION}/#{key.to_s}/"
31
+ options[:headers] ||= {}
32
+ options[:headers].merge!({"X-Beacon-Secret-Key" => secret.to_s}) unless secret.nil?
33
+ __setobj__ RestClient::Resource.new(url, options)
34
+ end
35
+
36
+ # Retrieve number of online users.
37
+ #
38
+ # @return [Fixnum]
39
+ def users
40
+ JSON.parse(self['users'].get.body)["online"]
41
+ end
42
+
43
+ # Send a message to the 'default' channel.
44
+ #
45
+ # @param (see Channel#message)
46
+ # @return (see Channel#message)
47
+ # @see Channel#message
48
+ def message(message)
49
+ channel.message(message)
50
+ end
51
+
52
+ # Retrieve a user by name, bound to this Session.
53
+ #
54
+ # @param [#to_s] name
55
+ # @return (see Session#initialize)
56
+ def user(name)
57
+ User.new(name, __getobj__)
58
+ end
59
+
60
+ # Retrieve a channel by name, bound to this Session.
61
+ #
62
+ # @param [#to_s] name
63
+ # @return (see Channel#initialize)
64
+ def channel(name = 'default')
65
+ Channel.new(name, __getobj__)
66
+ end
67
+ end
68
+
69
+ # Missing method calls are delegated to the underlying RestClient::Resource
70
+ class User < SimpleDelegator
71
+ # @return [String]
72
+ attr_reader :name
73
+
74
+ # Create a new User resource.
75
+ #
76
+ # @param [#to_s] name
77
+ # @param [RestClient::Resource] resource An object conformant with the RestClient API.
78
+ def initialize(name, resource)
79
+ __setobj__ resource["users/#{@name = name.to_s}"]
80
+ end
81
+
82
+ # True if the user is online.
83
+ #
84
+ # @return [Boolean]
85
+ def online?
86
+ get.code == 200 rescue false
87
+ end
88
+
89
+ # Force-logout the user.
90
+ #
91
+ # @return [RestClient::Response]
92
+ def logout
93
+ delete
94
+ end
95
+
96
+ # Send a message to the user.
97
+ #
98
+ # @param [#to_json] message Data to send to the user.
99
+ # @return [RestClient::Response]
100
+ # @raise [RestClient::Exception]
101
+ def message(message)
102
+ post message.to_json
103
+ end
104
+ end
105
+
106
+ # Missing method calls are delegated to the underlying RestClient::Resource
107
+ class Channel < SimpleDelegator
108
+ # @return [String]
109
+ attr_reader :name
110
+
111
+ # Create a new Channel resource.
112
+ #
113
+ # @param [#to_s] name
114
+ # @param [RestClient::Resource] resource An object conformant with the RestClient API.
115
+ def initialize(name, resource)
116
+ __setobj__ resource["channels/#{@name = name.to_s}"]
117
+ end
118
+
119
+ # Sends a message to all users in the channel.
120
+ #
121
+ # @param [#to_json] message Data to send to the channel.
122
+ # @return [RestClient::Response]
123
+ # @raise [RestClient::Exception]
124
+ def message(message)
125
+ post message.to_json
126
+ end
127
+
128
+ # Retrieve all users in the channel.
129
+ #
130
+ # @return [Array<User>] Array of users.
131
+ def users
132
+ JSON.parse(get.body)["users"].map { |user| User.new(user, __getobj__) }
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,72 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Dimma do
4
+ before do
5
+ @url ||= "http://api.beaconpush.com/#{Dimma::API_VERSION}/resource/"
6
+ @obj = Dimma.new("resource")
7
+
8
+ # Total user count
9
+ stub_request(:get, @url + 'users').to_return(:body => {:online => 10}.to_json)
10
+
11
+ # Channel message posting
12
+ stub_request(:get, @obj.channel.url).to_return(:body => {:users => ['Kim']}.to_json)
13
+ stub_request(:post, @obj.channel.url).
14
+ with(:body => 'Message'.to_json).to_return(:body => {:messages_sent => 1}.to_json)
15
+
16
+ # User API
17
+ @user = @obj.user 'Kim'
18
+ stub_request(:get, @user.url).to_return(:headers => {:code => 200})
19
+ stub_request(:post, @user.url).
20
+ with(:body => 'Message'.to_json).to_return(:body => {:messages_sent => 1}.to_json)
21
+ stub_request(:delete, @user.url).to_return(:headers => {:code => 204})
22
+ end
23
+
24
+ it "can retrieve a total usercount" do
25
+ @obj.users
26
+ WebMock.should have_requested(:get, @obj['users'].url)
27
+ end
28
+
29
+ it "can send messages to default channel" do
30
+ @obj.message 'Message'
31
+ WebMock.should have_requested(:post, @obj.channel.url).with(:body => 'Message'.to_json)
32
+ end
33
+
34
+ describe Dimma::User do
35
+ it "should have a name" do
36
+ @user.name.should == 'Kim'
37
+ end
38
+
39
+ it "should be online" do
40
+ @user.online?.should be true
41
+ WebMock.should have_requested(:get, @user.url)
42
+ end
43
+
44
+ it "can be logged out by force" do
45
+ @user.logout
46
+ WebMock.should have_requested(:delete, @user.url)
47
+ end
48
+
49
+ it "can be sent messages" do
50
+ @user.message 'Message'
51
+ WebMock.should have_requested(:post, @user.url).with(:body => 'Message'.to_json)
52
+ end
53
+ end
54
+
55
+ describe Dimma::Channel do
56
+ it "should have a name" do
57
+ @obj.channel.name.should == "default"
58
+ end
59
+
60
+ it "can be sent messages" do
61
+ channel = @obj.channel
62
+ channel.message 'Message'
63
+ WebMock.should have_requested(:post, channel.url).with(:body => 'Message'.to_json)
64
+ end
65
+
66
+ it "should have a list of users" do
67
+ channel = @obj.channel
68
+ channel.users.map(&:name).should == ['Kim']
69
+ WebMock.should have_requested(:get, channel.url)
70
+ end
71
+ end
72
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ -fs
@@ -0,0 +1,9 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+ require 'dimma'
4
+ require 'spec'
5
+ require 'webmock/rspec'
6
+
7
+ Spec::Runner.configure do |config|
8
+ config.include WebMock
9
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dimma
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Kim Burgestrand
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-05 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rest-client
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: json
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: webmock
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: rspec
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 13
72
+ segments:
73
+ - 1
74
+ - 2
75
+ - 9
76
+ version: 1.2.9
77
+ type: :development
78
+ version_requirements: *id004
79
+ - !ruby/object:Gem::Dependency
80
+ name: yard
81
+ prerelease: false
82
+ requirement: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ type: :development
92
+ version_requirements: *id005
93
+ description:
94
+ email: kim@burgestrand.se
95
+ executables: []
96
+
97
+ extensions: []
98
+
99
+ extra_rdoc_files:
100
+ - LICENSE
101
+ - README.markdown
102
+ files:
103
+ - .document
104
+ - .gitignore
105
+ - LICENSE
106
+ - README.markdown
107
+ - Rakefile
108
+ - VERSION
109
+ - dimma.gemspec
110
+ - lib/Dimma.rb
111
+ - spec/dimma_spec.rb
112
+ - spec/spec.opts
113
+ - spec/spec_helper.rb
114
+ has_rdoc: true
115
+ homepage: http://github.com/Burgestrand/Dimma
116
+ licenses:
117
+ - X11 License
118
+ post_install_message:
119
+ rdoc_options:
120
+ - --charset=UTF-8
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ hash: 57
129
+ segments:
130
+ - 1
131
+ - 8
132
+ - 7
133
+ version: 1.8.7
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ hash: 3
140
+ segments:
141
+ - 0
142
+ version: "0"
143
+ requirements: []
144
+
145
+ rubyforge_project:
146
+ rubygems_version: 1.3.7
147
+ signing_key:
148
+ specification_version: 3
149
+ summary: A Ruby library for the Beacon REST API
150
+ test_files:
151
+ - spec/dimma_spec.rb
152
+ - spec/spec_helper.rb