Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed get_orders_by_date to accept and properly format a Date or Date…

…Time. Added bundler and a few unit tests.
  • Loading branch information...
commit 493274b197efc77c97ddaecbe46834d32cd2afe5 1 parent bbd7f33
Michael Larkin mikelarkin authored
2  .gitignore
View
@@ -0,0 +1,2 @@
+.idea/
+*.rbc
6 Gemfile
View
@@ -0,0 +1,6 @@
+source "http://gems.github.com"
+source 'http://rubygems.org'
+
+gem 'json'
+gem 'fakeweb'
+gem 'mocha'
17 Gemfile.lock
View
@@ -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 Rakefile
View
@@ -1,16 +1,11 @@
-
require 'lib/bigcommerce'
-
-task :build do
- sh "gem build bigcommerce.gemspec"
-end
-
-task :publish => :build do
- sh "gem push bigcommerce-#{BigCommerce::VERSION}.gem"
-end
-
-task :clean do
- sh "rm *.gem"
-end
-
-task :release => [:publish, :clean]
+require 'rake/testtask'
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+desc 'Test BigCommerceApi'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
189 lib/bigcommerce/api.rb
View
@@ -1,91 +1,106 @@
module BigCommerce
- class Api
-
- def initialize(configuration={})
- @connection = Connection.new(configuration)
- end
-
- def store_url=(store_url)
- @connection.store_url = store_url
- end
-
- def username=(username)
- @connection.username = username
- end
-
- def api_key=(api_key)
- @connection.api_key = api_key
- end
-
- def verify_peer=(verify)
- @connection.verify_peer = verify
- end
-
- def ca_file=(path)
- @connection.ca_file = path
- end
-
- def get_time
- @connection.get '/time'
- end
-
- def get_products
- @connection.get '/products'
- end
-
- def get_product(id)
- @connection.get '/products/' + id
- end
-
- def get_categories
- @connection.get '/categories'
- end
-
- def get_category(id)
- @connection.get '/categories/' + id
- end
-
- def get_orders
- @connection.get('/orders')
- end
-
- def get_orders_by_date(date)
- @connection.get '/orders?min_date_created=' + CGI::escape(date)
- 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)
-
+ class Api
+
+ def initialize(configuration={})
+ @connection = Connection.new(configuration)
+ end
+
+ # Added getter to ensure configuration is correct
+ def connection
+ @connection
+ end
+
+ def store_url=(store_url)
+ @connection.store_url = store_url
+ end
+
+ def username=(username)
+ @connection.username = username
+ end
+
+ def api_key=(api_key)
+ @connection.api_key = api_key
+ end
+
+ def verify_peer=(verify)
+ @connection.verify_peer = verify
+ end
+
+ def ca_file=(path)
+ @connection.ca_file = path
+ end
+
+ # Returns the date formatted as
+ # RFC 2822 string
+ def to_rfc2822(datetime)
+ datetime.strftime("%a, %d %b %Y %H:%M:%S %z")
+ end
+
+ def get_time
+ @connection.get '/time'
+ end
+
+ def get_products
+ @connection.get '/products'
+ end
+
+ def get_product(id)
+ @connection.get '/products/' + id
+ end
+
+ def get_categories
+ @connection.get '/categories'
+ end
+
+ def get_category(id)
+ @connection.get '/categories/' + id
+ end
+
+ def get_orders
+ @connection.get('/orders')
+ end
+
+ def get_orders_by_date(date)
+ if date.is_a?(String)
+ date = DateTime.parse(date)
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
67 test/api_test.rb
View
@@ -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  test/fixtures/order.json
View
@@ -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  test/fixtures/time.json
View
@@ -0,0 +1 @@
+{"time":1331667176}
15 test/test_helper.rb
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.