From 5a9d6846fb18859075fc841a5a9a96e3aca8ca80 Mon Sep 17 00:00:00 2001 From: Damon Davison Date: Sat, 2 Jun 2012 09:17:20 +0100 Subject: [PATCH] renamed project to "nacre". added tests and authentication feature --- .gitignore | 4 +- Gemfile | 2 +- Brightpearl.gemspec => Nacre.gemspec | 8 ++-- README.md | 26 ++++++++--- config/sample_config.yml | 12 +++++ lib/Brightpearl.rb | 5 --- lib/nacre.rb | 6 +++ lib/nacre/api.rb | 65 +++++++++++++++++++++++++++ lib/{Brightpearl => nacre}/version.rb | 2 +- sandbox/brightpearl-test.rb | 40 ----------------- spec/api.rb | 33 ++++++++++++++ spec/spec_helper.rb | 2 + 12 files changed, 146 insertions(+), 59 deletions(-) rename Brightpearl.gemspec => Nacre.gemspec (68%) create mode 100755 config/sample_config.yml delete mode 100644 lib/Brightpearl.rb create mode 100644 lib/nacre.rb create mode 100644 lib/nacre/api.rb rename lib/{Brightpearl => nacre}/version.rb (55%) delete mode 100755 sandbox/brightpearl-test.rb create mode 100644 spec/api.rb create mode 100644 spec/spec_helper.rb diff --git a/.gitignore b/.gitignore index 57d6aea..5957a28 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,6 @@ spec/reports test/tmp test/version_tmp tmp -credentials +config/test_config.yml +sandbox +sandbox/* diff --git a/Gemfile b/Gemfile index 71a97a0..5d04b0a 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Brightpearl.gemspec b/Nacre.gemspec similarity index 68% rename from Brightpearl.gemspec rename to Nacre.gemspec index e4fe425..2c94624 100644 --- a/Brightpearl.gemspec +++ b/Nacre.gemspec @@ -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 = ["damon@allolex.net"] 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' diff --git a/README.md b/README.md index ad4eafd..14aaef4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/config/sample_config.yml b/config/sample_config.yml new file mode 100755 index 0000000..21de6d3 --- /dev/null +++ b/config/sample_config.yml @@ -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 diff --git a/lib/Brightpearl.rb b/lib/Brightpearl.rb deleted file mode 100644 index 3f179c3..0000000 --- a/lib/Brightpearl.rb +++ /dev/null @@ -1,5 +0,0 @@ -require "Brightpearl/version" - -module Brightpearl - # Your code goes here... -end diff --git a/lib/nacre.rb b/lib/nacre.rb new file mode 100644 index 0000000..a94683b --- /dev/null +++ b/lib/nacre.rb @@ -0,0 +1,6 @@ +require 'nacre/version' +require 'nacre/api' + +module Nacre + +end diff --git a/lib/nacre/api.rb b/lib/nacre/api.rb new file mode 100644 index 0000000..b2e6481 --- /dev/null +++ b/lib/nacre/api.rb @@ -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 + diff --git a/lib/Brightpearl/version.rb b/lib/nacre/version.rb similarity index 55% rename from lib/Brightpearl/version.rb rename to lib/nacre/version.rb index 4eb0c40..2dc42ab 100644 --- a/lib/Brightpearl/version.rb +++ b/lib/nacre/version.rb @@ -1,3 +1,3 @@ -module Brightpearl +module Nacre VERSION = "0.0.1" end diff --git a/sandbox/brightpearl-test.rb b/sandbox/brightpearl-test.rb deleted file mode 100755 index 132adc8..0000000 --- a/sandbox/brightpearl-test.rb +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env ruby - -require 'json' -require 'rest_client' - -cred_file = File.join(File.dirname(__FILE__),'../credentials/test') - -@credentials = [] - -def load_creds cred_file - fail "Credentials file missing at #{cred_file}" unless File.exists?(cred_file) - @credentials = File.open(cred_file,'r').readlines.each.map(&:chomp) - if @credentials.length == 3 - return true - else - return false - end -end - -if load_creds cred_file - # The Brightperl data centre code. See - # http://www.brightpearl.com/developer/latest/concept/uri-syntax.html - dc_code = 'eu1' - account_id = @credentials[0] - - uri = URI.parse( "https://ws-%s.brightpearl.com/%s/authorise" % [dc_code, account_id] ) - - message = { - apiAccountCredentials: { - emailAddress: @credentials[1], - password: @credentials[2] - } - }.to_json - - response = RestClient.post uri.to_s, message, :content_type => :json, :accept => :json - - p response.to_s -else - fail "Invalid credentials file #{cred_file}" -end diff --git a/spec/api.rb b/spec/api.rb new file mode 100644 index 0000000..def2eb0 --- /dev/null +++ b/spec/api.rb @@ -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 == 'damon@allolex.net' + @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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..d65b1b4 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,2 @@ +require 'nacre' +require 'nacre/api'