Skip to content

Commit

Permalink
adding traits to restfulie
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermesilveira committed Oct 21, 2010
1 parent 2d20c1c commit 07e68b0
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 24 deletions.
2 changes: 0 additions & 2 deletions Gemfile.lock
Expand Up @@ -72,7 +72,6 @@ GEM
thor (~> 0.14.0)
rake (0.8.7)
rcov (0.9.8)
responders_backport (0.1.2)
rspec (2.0.0.beta.19)
rspec-core (= 2.0.0.beta.19)
rspec-expectations (= 2.0.0.beta.19)
Expand Down Expand Up @@ -118,7 +117,6 @@ DEPENDENCIES
rack-conneg
rails (>= 3.0.0)
rcov
responders_backport
rspec-rails (>= 2.0.0.beta.19)
ruby-debug
sinatra
Expand Down
18 changes: 5 additions & 13 deletions Rakefile
Expand Up @@ -104,19 +104,11 @@ namespace :test do

task :all => ["spec","integration"]

# namespace :rcov do
# Spec::Rake::SpecTask.new('rcov') do |t|
# t.spec_opts = %w(-fs --color)
# t.spec_files = FileList['spec/units/**/*_spec.rb']
# t.rcov = true
# t.rcov_opts = ["-e", "/Library*", "-e", "~/.rvm", "-e", "spec", "-i", "bin"]
# end
# desc 'Run coverage test with fake server'
# task :run do
# start_server_and_invoke_test('test:rcov:rcov')
# end
# end

end

RSpec::Core::RakeTask.new(:spec) do |t|
# t.spec_files = FileList['spec_*.rb']
# t.options = '-v'
end

Rake::GemPackageTask.new(spec) do |pkg|
Expand Down
16 changes: 16 additions & 0 deletions lib/restfulie.rb
Expand Up @@ -5,6 +5,18 @@
require 'restfulie/client'
require 'restfulie/server'

class RestfulieDslBuilder
def initialize
@traits = [Restfulie::Client::Feature::Nil.new]
end
def method_missing(sym, *args)
trait = "Restfulie::Client::Feature::#{sym.to_s.classify}".constantize
@traits << trait
self.extend trait
self
end
end

# Shortcut to Restfulie::Client::EntryPoint
module Restfulie

Expand All @@ -22,6 +34,10 @@ def self.using(&block)
RestfulieUsing.new.instance_eval(&block)
end

def self.use(&block)
RestfulieDslBuilder.new.instance_eval(&block)
end

end

class RestfulieUsing
Expand Down
1 change: 1 addition & 0 deletions lib/restfulie/client.rb
Expand Up @@ -10,6 +10,7 @@ module Client
autoload :Base, 'restfulie/client/base'
autoload :Mikyung, 'restfulie/client/mikyung'
autoload :Cache, 'restfulie/client/cache'
autoload :Feature, 'restfulie/client/feature'

mattr_accessor :cache_provider, :cache_store

Expand Down
6 changes: 6 additions & 0 deletions lib/restfulie/client/feature.rb
@@ -0,0 +1,6 @@
module Restfulie::Client
module Feature
class Nil
end
end
end
19 changes: 10 additions & 9 deletions lib/restfulie/client/http/request_adapter.rb
Expand Up @@ -76,15 +76,6 @@ def headers
@headers ||= {}
end

# Executes a request against your server and return a response instance without {Error}
# * <tt>method: :get,:post,:delete,:head,:put</tt>
# * <tt>path: '/posts'</tt>
# * <tt>args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'}</tt>
def request(method = nil, path = nil, *args)
@response_handler = Restfulie::Client::Response::IgnoreError.new(@response_handler)
request!(method, path, *args)
end

# Executes a request against your server and return a response instance.
# * <tt>method: :get,:post,:delete,:head,:put</tt>
# * <tt>path: '/posts'</tt>
Expand Down Expand Up @@ -113,6 +104,16 @@ def request!(method, path, *args)

end


# Executes a request against your server and return a response instance without {Error}
# * <tt>method: :get,:post,:delete,:head,:put</tt>
# * <tt>path: '/posts'</tt>
# * <tt>args: payload: 'some text' and/or headers: {'Accept' => '*/*', 'Content-Type' => 'application/atom+xml'}</tt>
def request(method = nil, path = nil, *args)
@response_handler = Restfulie::Client::Response::IgnoreError.new(@response_handler)
request!(method, path, *args)
end

private

def get_connection_provider
Expand Down
8 changes: 8 additions & 0 deletions lib/restfulie/client/http/request_history.rb
Expand Up @@ -39,6 +39,14 @@ def request!(method=nil, path=nil, *args)#:nodoc:
end
end

def request(method = nil, path = nil, *args)
begin
request!(method, path, *args)
rescue Restfulie::Client::HTTP::Error::RESTError => se
se
end
end

def history(number)
@snapshot = snapshots[number]
raise "Undefined snapshot for #{number}" unless @snapshot
Expand Down
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,8 @@
# require File.expand_path("../../config/environment", __FILE__)
require 'rspec'

# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

require 'restfulie'
22 changes: 22 additions & 0 deletions spec/unit/restfulie_spec.rb
@@ -0,0 +1,22 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

context Restfulie do

context "when building restfulie" do

it "should create a restfulie with an at method" do
Restfulie.respond_to?(:at).should be_true
end

module Restfulie::Client::Feature::Custom
def custom_history
end
end

it "should allow adding extra methods by usage" do
Restfulie.use{custom}.respond_to?(:custom_history).should be_true
end

end

end
4 changes: 4 additions & 0 deletions tests/spec/requests/http/adapter_spec.rb
Expand Up @@ -164,6 +164,10 @@
before(:all) do
@host = "http://localhost:4567"
@builder = ::Restfulie::Client::HTTP::RequestHistoryExecutor.new(@host)
@builder = Restfulie.using {
request_history
verb_request
}.at(@host)
end

it "should remember last requests" do
Expand Down

0 comments on commit 07e68b0

Please sign in to comment.