Skip to content

Commit

Permalink
renamed project to "nacre". added tests and authentication feature
Browse files Browse the repository at this point in the history
  • Loading branch information
allolex committed Jun 2, 2012
1 parent f475e3c commit 5a9d684
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 59 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ spec/reports
test/tmp
test/version_tmp
tmp
credentials
config/test_config.yml
sandbox
sandbox/*
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in Brightpearl.gemspec
# Specify your gem's dependencies in Nacre.gemspec
gemspec
8 changes: 4 additions & 4 deletions Brightpearl.gemspec → Nacre.gemspec
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/Brightpearl/version', __FILE__)
require File.expand_path('../lib/nacre/version', __FILE__)

Gem::Specification.new do |gem|
gem.authors = ["Damon Allen Davison"]
gem.email = ["[email protected]"]
gem.description = %q{A Ruby class for working with the Brightpearl API}
gem.summary = %q{The Brightpearl gem provides an easy way of talking with the Brightpearl API.}
gem.summary = %q{The Nacre gem provides an easy way of talking with the Brightpearl API.}
gem.homepage = ""

gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "Brightpearl"
gem.name = "Nacre"
gem.require_paths = ["lib"]
gem.version = Brightpearl::VERSION
gem.version = Nacre::VERSION

gem.add_dependency 'rest-client'
gem.add_dependency 'rspec'
Expand Down
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
# Brightpearl
# Nacre

TODO: Write a gem description
Nacre is an interface to the Brightpearl accounting software service. It
uses the Brightpearl API.

## Installation

Add this line to your application's Gemfile:

gem 'Brightpearl'
gem "nacre", :git => "git://github.com/allolex/nacre.git"

And then execute:

$ bundle

Or install it yourself as:
## Usage

$ gem install Brightpearl
This gem isn't really very functional as of yet, so for now you can look
at the tests to see how it's used.

## Usage
## Features

- Configuration support. See config/ for a sample YAML file.
- API authentication

## Todo

TODO: Write usage instructions here
- Services
- Product
- Accounting
- Order
- Contact
- Warehouse

## Contributing

Expand Down
12 changes: 12 additions & 0 deletions config/sample_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Brightpearl API configuration
# For more information, see the
# Brightpearl API documentation here
# http://www.brightpearl.com/developer/latest/
# and more specifically here
# http://www.brightpearl.com/developer/latest/concept/uri-syntax.html
---
id: your_brightpearl_id
email: your_brightpearl_user_email
password: your_brightpearl_password
distribution_centre: eu1
api_version: 2.0.0
5 changes: 0 additions & 5 deletions lib/Brightpearl.rb

This file was deleted.

6 changes: 6 additions & 0 deletions lib/nacre.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'nacre/version'
require 'nacre/api'

module Nacre

end
65 changes: 65 additions & 0 deletions lib/nacre/api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'Nacre'
require 'psych'
require 'rest-client'
require 'json'

require 'pp'

module Nacre

class Api

attr_reader :email, :id, :auth_token, :distribution_centre, :api_version

def initialize args
if args[:config] && File.exists?(args[:config])
load_config args[:config]
end
@email ||= args[:email]
@id ||= args[:id]
@password ||= args[:password]
if @email =~ /@/ && password.respond_to?(:to_s)
puts "Authenticating"
begin
authenticate
rescue
puts "Authentication failure"
end
end
end

private

def password
@password
end

def load_config file
config = Psych.load( File.open(file,'r').read )
@email = config['email']
@id = config['id']
@password = config['password']
@distribution_centre = config['distribution_centre']
@api_version = config['api_version']
end

def authenticate
uri = URI.parse( "https://ws-%s.brightpearl.com/%s/authorise" % [@distribution_centre, @id] )
message = {
apiAccountCredentials: {
emailAddress: @email,
password: @password
}
}.to_json
response = RestClient.post uri.to_s, message, :content_type => :json, :accept => :json
auth = JSON.parse(response.body)
if auth['response'] =~ /^[a-z0-9]{8}-(?:[a-z0-9]{4}-){3}[a-z0-9]{12}$/i
@auth_token = auth['response']
return true
else
return false
end
end
end
end

2 changes: 1 addition & 1 deletion lib/Brightpearl/version.rb → lib/nacre/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Brightpearl
module Nacre
VERSION = "0.0.1"
end
40 changes: 0 additions & 40 deletions sandbox/brightpearl-test.rb

This file was deleted.

33 changes: 33 additions & 0 deletions spec/api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'spec_helper'

describe Nacre do

before :all do
@config = 'config/test_config.yml'
@bp = Nacre::Api.new( config: @config )
end

it 'should have a configuration file' do
File.exists?(@config).should == true
end

it 'can be instantiated' do
@bp.should be_a(Nacre::Api)
end

it 'should read in the credentials from a file' do
@bp.email.should == '[email protected]'
@bp.id.should == 'allolex'
@bp.distribution_centre.should == 'eu1'
@bp.api_version.should == '2.0.0'
end

it 'should not output the password' do
expect {@bp.password}.to raise_error(NoMethodError)
end

it 'should authenticate to the Nacre web API' do
# fe54961f-8adf-4d00-8bd3-185a479e827a
@bp.auth_token.should match(/^[a-z0-9]{8}-(?:[a-z0-9]{4}-){3}[a-z0-9]{12}$/)
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'nacre'
require 'nacre/api'

0 comments on commit 5a9d684

Please sign in to comment.