Skip to content

Commit

Permalink
Uses Connection class for API link
Browse files Browse the repository at this point in the history
  • Loading branch information
allolex committed Dec 16, 2012
1 parent 1314683 commit 6067fbc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/nacre/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class Api

def initialize args
@config = Nacre::Config.new args
@connection = Faraday.new
@connection = Nacre::Connection.new
begin
authenticate
self.config.header['brightpearl-auth'] = self.token.to_s
self.config.header['brightpearl-auth'] = self.connection.token.to_s
rescue
puts "Authentication failure"
end
Expand Down Expand Up @@ -42,9 +42,9 @@ def authenticate
password: self.config.password
}
}.to_json
@current_response = @connection.post self.auth_url, message, self.config.header
@current_response = @connection.connection.post self.auth_url, message, self.config.header
@auth = JSON.parse(@current_response.body)
@token = Nacre::Token.new(@auth['response'])
@token = @connection.token = Nacre::Token.new(@auth['response'])
end
end
end
Expand Down
9 changes: 7 additions & 2 deletions lib/nacre/api/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ def search_url

def list
results = []
response = self.api.connection.get self.search_url, {}
response = nil
begin
response = @api.connection.connection.get self.search_url, {}, @api.config.header
rescue
raise "Error in response: #{response.body.inspect}\n#{@api.inspect}"
end
hash = JSON.parse response.body
hash['response']['results'].each do |result|
model = Nacre::ProductSearchResult.new result
model = Nacre::API::ProductSearchResult.new result
results << model
end
return results
Expand Down
5 changes: 5 additions & 0 deletions spec/api/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@
'https://ws-eu1.brightpearl.com/2.0.0/allolex/product-service/product-search'
end

it 'should return a list of all products' do
@product.list.should be_a(Array)
@product.list.first.should be_a(Nacre::API::ProductSearchResult)
end

end

0 comments on commit 6067fbc

Please sign in to comment.