Skip to content

Commit

Permalink
Change Module from Class and add Brawne.setup config
Browse files Browse the repository at this point in the history
  • Loading branch information
joel committed Sep 15, 2011
1 parent 34ae614 commit 1d9941a
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 24 deletions.
13 changes: 13 additions & 0 deletions Gemfile
@@ -0,0 +1,13 @@
source 'http://rubygems.org'

gemspec

# gem "mocha"
gem "rake"
gem "rspec"
gem 'active_support'

group :development do
gem "ruby-debug", :platforms => :mri_18
gem "ruby-debug19", :platforms => :mri_19
end
54 changes: 54 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,54 @@
PATH
remote: .
specs:
brawne (0.0.1)

GEM
remote: http://rubygems.org/
specs:
active_support (3.0.0)
activesupport (= 3.0.0)
activesupport (3.0.0)
archive-tar-minitar (0.5.2)
columnize (0.3.4)
diff-lcs (1.1.3)
linecache (0.46)
rbx-require-relative (> 0.0.4)
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
rake (0.9.2)
rbx-require-relative (0.0.5)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
rspec-mocks (~> 2.6.0)
rspec-core (2.6.4)
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
ruby-debug (0.10.4)
columnize (>= 0.1)
ruby-debug-base (~> 0.10.4.0)
ruby-debug-base (0.10.4)
linecache (>= 0.3)
ruby-debug-base19 (0.11.25)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby_core_source (>= 0.1.4)
ruby-debug19 (0.11.6)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby-debug-base19 (>= 0.11.19)
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)

PLATFORMS
ruby

DEPENDENCIES
active_support
brawne!
rake
rspec
ruby-debug
ruby-debug19
Empty file added README
Empty file.
29 changes: 29 additions & 0 deletions Rakefile
@@ -0,0 +1,29 @@
begin
require "bundler"
Bundler::GemHelper.install_tasks
rescue Exception => e
end

require "rake"
require "rspec"
require "rspec/core/rake_task"
require "brawne"

RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = "spec/**/*_spec.rb"
end

RSpec::Core::RakeTask.new('spec:progress') do |spec|
spec.rspec_opts = %w(--format progress)
spec.pattern = "spec/**/*_spec.rb"
end

require "rake/rdoctask"
Rake::RDocTask.new do |rdoc|
rdoc.rdoc_dir = "rdoc"
rdoc.title = "Brawne #{Brawne::VERSION}"
rdoc.rdoc_files.include("README*")
rdoc.rdoc_files.include("lib/**/*.rb")
end

task :default => :spec
47 changes: 23 additions & 24 deletions lib/brawne.rb
@@ -1,28 +1,27 @@
require 'net/http'
require "net/https"
require 'active_support/core_ext/module/attribute_accessors'

class Brawne

attr_accessor :host, :port, :ssl, :user, :token

def initialize(host, port, ssl, user, token)
@host, @port, @ssl, @user, @token = host, port, ssl, user, token
module Brawne
@@_ran_once = false

mattr_accessor :host, :port, :ssl, :user, :token
@@host, @@port, @@ssl, @@user, @@token = nil, nil, nil, nil, nil

def self.setup
yield self if @@_ran_once == false
@@_ran_once = true
end

def self.load_and_set_settings!
Kernel.send(:remove_const, 'BRAWNE_HOST') if Kernel.const_defined?('BRAWNE_HOST')
Kernel.const_set('BRAWNE_HOST', Brawne.host)
Kernel.send(:remove_const, 'BRAWNE_PORT') if Kernel.const_defined?('BRAWNE_PORT')
Kernel.const_set('BRAWNE_PORT', Brawne.port)
Kernel.send(:remove_const, 'BRAWNE_SSL') if Kernel.const_defined?('BRAWNE_SSL')
Kernel.const_set('BRAWNE_SSL', Brawne.ssl)
Kernel.send(:remove_const, 'BRAWNE_USER') if Kernel.const_defined?('BRAWNE_USER')
Kernel.const_set('BRAWNE_USER', Brawne.user)
Kernel.send(:remove_const, 'BRAWNE_TOKEN') if Kernel.const_defined?('BRAWNE_TOKEN')
Kernel.const_set('BRAWNE_TOKEN', Brawne.token)
end

def get(request)
http_r = Net::HTTP.new(@host, @port)
http_r.use_ssl = @ssl
response = nil
begin
http_r.start() do |http|
req = Net::HTTP::Get.new(request)
req.add_field("USERNAME", @user)
req.add_field("TOKEN", @token)
response = http.request(req)
end
return [response.code, response.body]
rescue Errno::ECONNREFUSED
return [503, "unavailable"]
end

end
15 changes: 15 additions & 0 deletions lib/brawne/rack/reloader.rb
@@ -0,0 +1,15 @@
module Brawne
module Rack
# Rack middleware the reloads RailsConfig on every request (only use in dev mode)
class Reloader
def initialize(app)
@app = app
end

def call(env)
Brawne.load_and_set_settings!
@app.call(env)
end
end
end
end
22 changes: 22 additions & 0 deletions lib/brawne/request.rb
@@ -0,0 +1,22 @@
require 'net/http'
require "net/https"

module Brawne

def get(request)
http_r = Net::HTTP.new(Brawne.host, Brawne.port)
http_r.use_ssl = @ssl
response = nil
begin
http_r.start() do |http|
req = Net::HTTP::Get.new(request)
req.add_field("USERNAME", Brawne.user)
req.add_field("TOKEN", Brawne.token)
response = http.request(req)
end
return [response.code, response.body]
rescue Errno::ECONNREFUSED
return [503, "unavailable"]
end

end
3 changes: 3 additions & 0 deletions lib/brawne/version.rb
@@ -0,0 +1,3 @@
module Brawne
VERSION = '0.0.1'
end
7 changes: 7 additions & 0 deletions lib/generators/templates/brawne.rb
@@ -0,0 +1,7 @@
Brawne.setup do |config|
config.host = ""
config.port = ""
config.ssl = ""
config.user = ""
config.token = ""
end
16 changes: 16 additions & 0 deletions lib/integration/sinatra.rb
@@ -0,0 +1,16 @@
require "brawne/rack/reloader"

module Brawne
# provide helper to register within your Sinatra app
# set :root, File.dirname(__FILE__)
# register Brawne
def self.registered(app)
app.configure do |inner_app|
env = inner_app.environment || ENV["RACK_ENV"]
root = inner_app.root
Brawne.load_and_set_settings!
inner_app.use(::Brawne::Rack::Reloader) if inner_app.development?
end
end

end
53 changes: 53 additions & 0 deletions spec/brawne_spec.rb
@@ -0,0 +1,53 @@
require 'spec_helper'

describe Brawne do

context "Test Configuration" do
before :each do
Brawne.setup do |config|
config.host = "localhost"
config.port = "8080"
config.ssl = true
config.user = "Korben"
config.token = "34524GVQSDCA2CT34CZDX2"
end
end
it { Brawne.host.should eql("localhost") }
it { Brawne.port.should eql("8080") }
it { Brawne.ssl.should be_true }
it { Brawne.user.should eql("Korben") }
it { Brawne.token.should eql("34524GVQSDCA2CT34CZDX2") }
end

context "Test Configuration again !" do
it { Brawne.host.should eql("localhost") }
it { Brawne.port.should eql("8080") }
it { Brawne.ssl.should be_true }
it { Brawne.user.should eql("Korben") }
it { Brawne.token.should eql("34524GVQSDCA2CT34CZDX2") }
end

context "Test Configuration" do
before :each do
Brawne.setup do |config|
config.host = "localhost"
config.port = "8080"
config.ssl = true
config.user = "Korben"
config.token = "34524GVQSDCA2CT34CZDX2"
end
Brawne.load_and_set_settings!
end
it "Test constantes" do
['BRAWNE_HOST','BRAWNE_PORT','BRAWNE_SSL','BRAWNE_USER','BRAWNE_TOKEN'].each do |constant|
Kernel.const_defined?(constant).should be_true
end
end
it { BRAWNE_HOST.should eql("localhost") }
it { BRAWNE_PORT.should eql("8080") }
it { BRAWNE_SSL.should be_true }
it { BRAWNE_USER.should eql("Korben") }
it { BRAWNE_TOKEN.should eql("34524GVQSDCA2CT34CZDX2") }
end

end
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,8 @@
require 'brawne'
require 'bundler/setup'

RSpec.configure do |c|
c.run_all_when_everything_filtered = true
# config.mock_with :mocha
end

0 comments on commit 1d9941a

Please sign in to comment.