Last active
August 29, 2015 13:55
-
-
Save Willianvdv/8691373 to your computer and use it in GitHub Desktop.
Exact api spul
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
require 'base64' | |
require 'rest_client' | |
class ExactOnlineClient | |
def xml_call topic, data=nil | |
if not @cookies | |
# Brutal login technique (as documented (-; ) | |
url = "https://start.exactonline.nl/docs/XMLDivisions.aspx" | |
response = RestClient.post url, {"_UserName_" => username, "_Password_" => password} | |
@cookies = response.cookies | |
end | |
# Do the XML request | |
headers = {cookies: @cookies} | |
url = "https://start.exactonline.nl/docs/XMLUpload.aspx?Topic=#{topic}&ApplicationKey=#{application_key}" | |
response = RestClient.post url, data.strip, headers | |
#ap response | |
doc = Nokogiri::XML(response) | |
key = doc.xpath('//Topic[@node="SalesOrder"]/Data').attribute('key').to_str # Replace SalesOrder with topic! | |
key | |
end | |
def json_call url, data=nil | |
auth = 'Basic ' + Base64.encode64( "#{username}:#{password}" ).chomp | |
header = {'accept' => :json, | |
'Authorization' => auth, | |
'Content-Type' => 'application/json', | |
'X-ExactOnline-ApplicationKey' => application_key} | |
begin | |
if data | |
if url['guid'].nil? | |
response = RestClient.post "#{base_url}#{url}", data.to_json, header | |
else | |
response = RestClient.put "#{base_url}#{url}", data.to_json, header | |
end | |
else | |
response = RestClient.get "#{base_url}#{url}", header | |
end | |
rescue RestClient::InternalServerError => e | |
ap JSON.parse e.response | |
raise e | |
end | |
response.to_str.blank? ? nil : JSON.parse(response.to_str) | |
end | |
private | |
def username | |
raise "EXACT_ONLINE_USERNAME must be set" unless ENV["EXACT_ONLINE_USERNAME"] | |
ENV["EXACT_ONLINE_USERNAME"] | |
end | |
def password | |
raise "EXACT_ONLINE_PASSWORD must be set" unless ENV["EXACT_ONLINE_PASSWORD"] | |
ENV["EXACT_ONLINE_PASSWORD"] | |
end | |
def application_key | |
raise "EXACT_ONLINE_APPLICATION_KEY must be set" unless ENV["EXACT_ONLINE_APPLICATION_KEY"] | |
ENV["EXACT_ONLINE_APPLICATION_KEY"] | |
end | |
def division | |
raise "EXACT_ONLINE_DIVISION must be set" unless ENV["EXACT_ONLINE_DIVISION"] | |
ENV["EXACT_ONLINE_DIVISION"] | |
end | |
def base_url | |
"https://start.exactonline.nl/api/v1/#{division}/" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment