Skip to content

Commit

Permalink
adding lib skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
maetl committed Sep 12, 2011
0 parents commit 8712373
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
Copyright (C) BigCommerce, 2011.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
88 changes: 88 additions & 0 deletions README.md
@@ -0,0 +1,88 @@
BigCommerce API V2 - Ruby Client
================================

This library provides a wrapper around the BigCommerce REST API for use within
Ruby apps or via the console.

Requirements
------------

- Ruby 1.8.7+
- Rubygems
- JSON

To connect to the API, you need the following credentials:

- Secure URL pointing to a BigCommerce store
- Username of an authorized admin user of the store
- API key for the user

A valid API key is required to authenticate requests. To grant API access for
user, go to Control Panel > Users > Edit User and make sure that the
'Enable the XML API?' checkbox is ticked.

Installation
------------

Download the lib folder and copy it to a path accessible within your app, or
install the package directly from Rubygems:

```
gem install bigcommerce
```

Configuration
-------------

To use the API client in your Ruby code, provide the required credentials as
follows:

```
require 'bigcommerce'
api = BigCommerce::Api.new({
:store_url => "https://store.mybigcommerce.com",
:username => "admin",
:api_key => "d81aada4c19c34d913e18f07fd7f36ca"
})
```

Connecting to the store
-----------------------

Ping the get_time method to check that your configuration is working and you
can connect successfully to the store:

```
ping = api.get_time
```

Usage
-----

The API object acts as a gateway to all top level resources in the V2 API.

```
$ irb
>
> api = BigCommerce::Api.new(...)
>
> api.get_products.each { |product| puts product.name }
>
> api.get_customers.each { |customer| puts customer.email }
>
> puts api.get_orders_count
>
> category = api.get_category(11)
> category.name = "Laptops"
> category.update
>
> brand = BigCommerce::Api::Brand.new
> brand.name = "Samsung"
> brand.create
>
> option = api.get_option(22)
> option.delete
>
```

1 change: 1 addition & 0 deletions Rakefile
@@ -0,0 +1 @@
#require "bundler/gem_tasks"
10 changes: 10 additions & 0 deletions lib/bigcommerce.rb
@@ -0,0 +1,10 @@
require "cgi"
require "uri"
require "net/https"

require "rubygems"
require "json"

require File.join(File.dirname(__FILE__), 'bigcommerce', 'version')
require File.join(File.dirname(__FILE__), 'bigcommerce', 'api')
require File.join(File.dirname(__FILE__), 'bigcommerce', 'connection')
63 changes: 63 additions & 0 deletions lib/bigcommerce/api.rb
@@ -0,0 +1,63 @@
module BigCommerce
class Api

def initialize(configuration)
@connection = Connection.new(configuration)
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

end
end
57 changes: 57 additions & 0 deletions lib/bigcommerce/connection.rb
@@ -0,0 +1,57 @@
module BigCommerce
class Connection

def initialize(configuration)
@configuration = configuration
end

def get(path)
request(:get, path)
end

def post(path)
request(:post, path)
end

def put(path)
request(:put, path)
end

def delete(path)
request(:delete, path)
end

def request(method, path, body = nil, params = {})

url = @configuration[:store_url] + '/api/v2' + path

uri = URI.parse(url)

http = Net::HTTP.new(uri.host, uri.port)

request = case method
when :get then Net::HTTP::Get.new(uri.request_uri)
when :post then Net::HTTP::Post.new(uri.request_uri)
when :put then Net::HTTP::Put.new(uri.request_uri)
when :delete then Net::HTTP::Delete.new(uri.request_uri)
end

request.basic_auth(@configuration[:username], @configuration[:api_key])
request.add_field 'Accept', 'application/json'
request.add_field 'Content-Type', 'application/json'

response = http.request(request)

return case response
when Net::HTTPSuccess, Net::HTTPRedirection
JSON.parse(response.body || "{}")
else false
end
end

end

class HttpError < Exception

end
end
3 changes: 3 additions & 0 deletions lib/bigcommerce/version.rb
@@ -0,0 +1,3 @@
module Bigcommerce
VERSION = "0.0.1"
end

0 comments on commit 8712373

Please sign in to comment.