Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-stripe committed Apr 7, 2013
1 parent f6a0060 commit 51a46a7
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Expand Up @@ -2,3 +2,7 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in nestful.gemspec
gemspec

group :test do
gem 'webmock'
end
9 changes: 8 additions & 1 deletion Rakefile
@@ -1 +1,8 @@
require "bundler/gem_tasks"
require 'bundler/gem_tasks'
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs << 'test'
t.test_files = FileList['test/**/test*.rb']
t.verbose = true
end
4 changes: 4 additions & 0 deletions examples/resource.rb
Expand Up @@ -24,4 +24,8 @@ class Charge < Base
def refund
post(:refund)
end
end

class Token < Base
path '/v1/tokens'
end
4 changes: 4 additions & 0 deletions lib/nestful/helpers.rb
Expand Up @@ -2,6 +2,10 @@

module Nestful
module Helpers extend self
def to_path(*params)
params.map(&:to_s).reject(&:empty?) * '/'
end

def to_param(value, key = nil)
case value
when Hash then value.map { |k,v| to_param(v, append_key(key,k)) }.join('&')
Expand Down
2 changes: 1 addition & 1 deletion lib/nestful/request.rb
Expand Up @@ -23,7 +23,7 @@ def initialize(url, options = {})

def format=(mime_or_format)
@format = mime_or_format.is_a?(Symbol) ?
Formats[mime_or_format] : mime_or_format
Formats[mime_or_format].new : mime_or_format
end

def url=(value)
Expand Down
18 changes: 8 additions & 10 deletions lib/nestful/resource.rb
Expand Up @@ -17,17 +17,15 @@ def self.path(value = nil)
def self.options(value = nil)
@options = value if value
return @options if @options
superclass.respond_to?(:options) ? superclass.options : nil
superclass.respond_to?(:options) ? superclass.options : {}
end

def self.url
URI.join(endpoint.to_s, path.to_s).to_s
end

def self.uri(*parts)
parts.unshift(path)
parts.unshift(endpoint)
URI.join(*parts.compact.map(&:to_s))
URI.parse(Helpers.to_path(url, *parts))
end

def self.get(action = '', params = {}, options = {})
Expand Down Expand Up @@ -74,23 +72,23 @@ def initialize(attributes = {})
end

def get(action = '', *args)
self.class.get(uri(action), *args)
self.class.get(path(action), *args)
end

def put(action = '', *args)
self.class.put(uri(action), *args)
self.class.put(path(action), *args)
end

def post(action = '', *args)
self.class.post(uri(action), *args)
self.class.post(path(action), *args)
end

def delete(action = '', *args)
self.class.delete(uri(action), *args)
self.class.delete(path(action), *args)
end

def uri(*parts)
self.class.uri(self.id, *parts)
def path(*parts)
Helpers.to_path(self.id, *parts)
end

def id #:nodoc:
Expand Down
45 changes: 45 additions & 0 deletions test/nestful/test_resource.rb
@@ -0,0 +1,45 @@
require 'minitest/autorun'
require 'webmock/minitest'
require 'nestful'

WebMock.disable_net_connect!

class TestResource < MiniTest::Unit::TestCase
class Charge < Nestful::Resource
endpoint 'http://example.com'
path '/v1/charges'
end

def setup
end

def test_get
stub_request(:any, 'http://example.com/v1/charges').to_return(:body => '')
Charge.get
assert_requested(:get, 'http://example.com/v1/charges')
end

def test_post
stub_request(:any, 'http://example.com/v1/charges/unknown').to_return(:body => '')
Charge.post 'unknown'
assert_requested(:post, 'http://example.com/v1/charges/unknown')
end

def test_get_json
stub_request(:any, 'http://example.com/v1/charges/1').to_return(
:body => '{"id": 1, "amount": 2000}',
:headers => {'Content-Type' => 'application/json'}
)
charge = Charge.find(1)
assert_requested(:get, 'http://example.com/v1/charges/1')
assert_equal 1, charge.id
assert_equal 2000, charge.amount
end

def test_instance_put
stub_request(:any, 'http://example.com/v1/charges/1/capture')
charge = Charge.new({:id => 1})
charge.put(:capture)
assert_requested(:put, 'http://example.com/v1/charges/1/capture')
end
end

0 comments on commit 51a46a7

Please sign in to comment.