forked from allolex/nacre-old
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
renamed project to "nacre". added tests and authentication feature
- Loading branch information
Showing
12 changed files
with
146 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,6 @@ spec/reports | |
test/tmp | ||
test/version_tmp | ||
tmp | ||
credentials | ||
config/test_config.yml | ||
sandbox | ||
sandbox/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require 'nacre/version' | ||
require 'nacre/api' | ||
|
||
module Nacre | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Brightpearl | ||
module Nacre | ||
VERSION = "0.0.1" | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'nacre' | ||
require 'nacre/api' |