Skip to content

Commit

Permalink
Fixed get_orders_by_date to accept and properly format a Date or Date…
Browse files Browse the repository at this point in the history
…Time. Added bundler and a few unit tests.
  • Loading branch information
mikelarkin committed Mar 13, 2012
1 parent bbd7f33 commit 493274b
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 102 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.idea/
*.rbc
6 changes: 6 additions & 0 deletions Gemfile
@@ -0,0 +1,6 @@
source "http://gems.github.com"
source 'http://rubygems.org'

gem 'json'
gem 'fakeweb'
gem 'mocha'
17 changes: 17 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,17 @@
GEM
remote: http://gems.github.com/
remote: http://rubygems.org/
specs:
fakeweb (1.3.0)
json (1.6.5)
metaclass (0.0.1)
mocha (0.10.5)
metaclass (~> 0.0.1)

PLATFORMS
ruby

DEPENDENCIES
fakeweb
json
mocha
25 changes: 10 additions & 15 deletions Rakefile
@@ -1,16 +1,11 @@

require 'lib/bigcommerce' require 'lib/bigcommerce'

require 'rake/testtask'
task :build do require 'bundler'
sh "gem build bigcommerce.gemspec" Bundler::GemHelper.install_tasks
end

desc 'Test BigCommerceApi'
task :publish => :build do Rake::TestTask.new(:test) do |t|
sh "gem push bigcommerce-#{BigCommerce::VERSION}.gem" t.libs << 'test'
end t.pattern = 'test/**/*_test.rb'

t.verbose = true
task :clean do end
sh "rm *.gem"
end

task :release => [:publish, :clean]
189 changes: 102 additions & 87 deletions lib/bigcommerce/api.rb
@@ -1,91 +1,106 @@
module BigCommerce module BigCommerce
class Api class Api


def initialize(configuration={}) def initialize(configuration={})
@connection = Connection.new(configuration) @connection = Connection.new(configuration)
end end


def store_url=(store_url) # Added getter to ensure configuration is correct
@connection.store_url = store_url def connection
end @connection

end
def username=(username)
@connection.username = username def store_url=(store_url)
end @connection.store_url = store_url

end
def api_key=(api_key)
@connection.api_key = api_key def username=(username)
end @connection.username = username

end
def verify_peer=(verify)
@connection.verify_peer = verify def api_key=(api_key)
end @connection.api_key = api_key

end
def ca_file=(path)
@connection.ca_file = path def verify_peer=(verify)
end @connection.verify_peer = verify

end
def get_time
@connection.get '/time' def ca_file=(path)
end @connection.ca_file = path

end
def get_products
@connection.get '/products' # Returns the date formatted as
end # RFC 2822 string

def to_rfc2822(datetime)
def get_product(id) datetime.strftime("%a, %d %b %Y %H:%M:%S %z")
@connection.get '/products/' + id end
end

def get_time
def get_categories @connection.get '/time'
@connection.get '/categories' end
end

def get_products
def get_category(id) @connection.get '/products'
@connection.get '/categories/' + id end
end

def get_product(id)
def get_orders @connection.get '/products/' + id
@connection.get('/orders') end
end

def get_categories
def get_orders_by_date(date) @connection.get '/categories'
@connection.get '/orders?min_date_created=' + CGI::escape(date) end
end

def get_category(id)
def get_orders_count @connection.get '/categories/' + id
get_count @connection.get '/orders/count' end
end

def get_orders
def get_order(id) @connection.get('/orders')
@connection.get '/orders/' + id end
end

def get_orders_by_date(date)
def get_order_products(id) if date.is_a?(String)
@connection.get '/orders/' + id + '/products' date = DateTime.parse(date)
end

def get_customers
@connection.get '/customers'
end

def get_customer(id)
@connection.get '/customers/' + id
end

private

def get_count(result)
result["count"]
end

def get_resource(result)

end

def get_collection(result)

end end
date = to_rfc2822(date)
@connection.get '/orders?min_date_created=' + CGI::escape(date)
end


end def get_orders_count
get_count @connection.get '/orders/count'
end

def get_order(id)
@connection.get '/orders/' + id
end

def get_order_products(id)
@connection.get '/orders/' + id + '/products'
end

def get_customers
@connection.get '/customers'
end

def get_customer(id)
@connection.get '/customers/' + id
end

private

def get_count(result)
result["count"]
end

def get_resource(result)

end

def get_collection(result)

end

end
end end
67 changes: 67 additions & 0 deletions test/api_test.rb
@@ -0,0 +1,67 @@
require 'test_helper'

class ApiTest < Test::Unit::TestCase

def setup
@api = BigCommerce::Api.new({:store_url => "https://store-12345.mybigcommerce.com", :username => "test", :api_key => "12345"})
FakeWeb.register_uri(:get, %r|https://test:12345@store-12345.mybigcommerce.com/api/v2/orders|, :body => load_fixture('order'), :status => 200, :content_type => "text/json")
FakeWeb.register_uri(:get, %r|https://test:12345@store-12345.mybigcommerce.com/api/v2/time|, :body => load_fixture('time'), :status => 200, :content_type => "text/json")

@rfc2822_datetime = "Tue, 13 Mar 2012 12:45:26 +0000"
@rfc2822_date = "Mon, 12 Mar 2012 00:00:00 +0000"

end

def teardown
FakeWeb.allow_net_connect = false
end

def test_to_rfc2822
assert_equal @rfc2822_datetime, @api.to_rfc2822(DateTime.parse('2012-03-13 12:45:26 +0000'))
assert_equal @rfc2822_date, @api.to_rfc2822(DateTime.parse('2012-03-12 00:00:00 +0000'))
end

def test_get_time
@api.connection.expects(:get).with("/time")
@api.get_time
end


def test_get_orders
@api.connection.expects(:get).with("/orders")
@api.get_orders
end


def test_get_orders_by_date_with_date_time
# RFC 2822 format is required
@api.connection.expects(:get).with("/orders?min_date_created=#{CGI::escape(@rfc2822_datetime)}")

# Test DateTime
@api.get_orders_by_date(DateTime.parse('2012-03-13 12:45:26 GMT'))

end

def test_get_orders_by_date_with_date
# RFC 2822 format is required
@api.connection.expects(:get).with("/orders?min_date_created=#{CGI::escape(@rfc2822_date)}")

# Test Date
@api.get_orders_by_date(Date.parse("2012-03-12"))

end

def test_get_orders_by_date_with_string
# RFC 2822 format is required
@api.connection.expects(:get).with("/orders?min_date_created=#{CGI::escape(@rfc2822_datetime)}")
@api.connection.expects(:get).with("/orders?min_date_created=#{CGI::escape(@rfc2822_date)}")

# Test String
@api.get_orders_by_date('2012-03-13 12:45:26 GMT')
@api.get_orders_by_date('2012-03-12')


end


end
1 change: 1 addition & 0 deletions test/fixtures/order.json
@@ -0,0 +1 @@
[{"id":100,"customer_id":1,"date_created":"Fri, 02 Mar 2012 21:36:24 +0000","date_modified":"Mon, 12 Mar 2012 21:40:16 +0000","date_shipped":"Fri, 02 Mar 2012 21:36:56 +0000","status_id":11,"status":"Awaiting Fulfillment","subtotal_ex_tax":"0.0100","subtotal_inc_tax":"0.0100","subtotal_tax":"0.0000","base_shipping_cost":"0.0000","shipping_cost_ex_tax":"0.0000","shipping_cost_inc_tax":"0.0000","shipping_cost_tax":"0.0000","shipping_cost_tax_class_id":2,"base_handling_cost":"0.0000","handling_cost_ex_tax":"0.0000","handling_cost_inc_tax":"0.0000","handling_cost_tax":"0.0000","handling_cost_tax_class_id":2,"base_wrapping_cost":"0.0000","wrapping_cost_ex_tax":"0.0000","wrapping_cost_inc_tax":"0.0000","wrapping_cost_tax":"0.0000","wrapping_cost_tax_class_id":3,"total_ex_tax":"0.0100","total_inc_tax":"0.0100","total_tax":"0.0000","items_total":1,"items_shipped":0,"payment_method":"Pay in Store","payment_provider_id":null,"payment_status":"","refunded_amount":"0.0000","order_is_digital":true,"store_credit_amount":"0.0000","gift_certificate_amount":"0.0000","ip_address":"127.0.0.1","geoip_country":"United States","geoip_country_iso2":"US","currency_id":1,"currency_code":"USD","currency_exchange_rate":"1.0000000000","default_currency_id":1,"default_currency_code":"USD","staff_notes":"","customer_message":"","discount_amount":"0.0000","coupon_discount":"0.0000","shipping_address_count":0,"is_deleted":false,"billing_address":{"first_name":"Bugs","last_name":"Bunny","company":"","street_1":"1 Main Street","street_2":"","city":"Walla Walla","state":"WA","zip":"99362","country":"United States","country_iso2":"US","phone":"5555556789","email":"bug@bunny.com"},"products":{"url":"https:\/\/store-12345.mybigcommerce.com\/api\/v2\/orders\/100\/products.json","resource":"\/orders\/100\/products"},"shipping_addresses":{"url":"https:\/\/store-12345.mybigcommerce.com\/api\/v2\/orders\/100\/shippingaddresses.json","resource":"\/orders\/100\/shippingaddresses"},"coupons":{"url":"https:\/\/store-12345.mybigcommerce.com\/api\/v2\/orders\/100\/coupons.json","resource":"\/orders\/100\/coupons"}}]
1 change: 1 addition & 0 deletions test/fixtures/time.json
@@ -0,0 +1 @@
{"time":1331667176}
15 changes: 15 additions & 0 deletions test/test_helper.rb
@@ -0,0 +1,15 @@
require 'rubygems'
require 'test/unit'
require 'fakeweb'
require 'mocha'
require 'lib/bigcommerce'

FakeWeb.allow_net_connect = false

class Test::Unit::TestCase

def load_fixture(name)
File.read(File.dirname(__FILE__) + "/fixtures/#{name}.json")
end

end

0 comments on commit 493274b

Please sign in to comment.