Last active
December 16, 2015 19:50
-
-
Save henrik/5488207 to your computer and use it in GitHub Desktop.
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
# Low-level e-conomic API for Ruby. A very thin wrapper over the Savon SOAP lib. | |
# Lets you request and get responses in a format very close to the docs: | |
# https://www.e-conomic.com/secure/api1/EconomicWebService.asmx | |
# Build higher abstractions on this. | |
require "rubygems" | |
require "bundler" | |
Bundler.require :default, :development | |
#require_relative "wasabi_marshaller" | |
class EconomicApi | |
class Operation | |
def self.normalize(name) | |
# Savon's Wasabi only accepts operations as snake_case. | |
# So we normalize to that format. We think you should still | |
# pass in the documented format, like :AccountingYear_GetAll, | |
# even if Wasabi will see it as :accounting_year_get_all. | |
name.to_s.snakecase.to_sym | |
end | |
end | |
attr_initialize :agreement_number, :account_name, :password | |
attr_private :agreement_number, :account_name, :password, :auth_cookies | |
attr_writer :auth_cookies; private :auth_cookies= | |
def call(operation, message = {}) | |
authenticate | |
operation = Operation.normalize(operation) | |
response = client.call(operation, message: message, cookies: auth_cookies) | |
response.body[:"#{operation}_response"][:"#{operation}_result"] | |
end | |
private | |
def authenticate | |
return if auth_cookies | |
response = client.call(:connect, message: credentials) | |
self.auth_cookies = response.http.cookies | |
end | |
def credentials | |
{ | |
agreementNumber: agreement_number, | |
userName: account_name, | |
password: password | |
} | |
end | |
def client | |
@client ||= Savon.client(wsdl: wsdl, convert_request_keys_to: :none, log: false) | |
end | |
def wsdl | |
# 3 MBs of goodness! | |
# We're fine retrieving from URL because we'll marshal and zip it. | |
"https://www.e-conomic.com/secure/api1/EconomicWebService.asmx?WSDL" | |
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
require "~/.economic_auth" | |
require_relative "economic_api" | |
require "pp" | |
client = EconomicApi.new(NUMBER, NAME, PW) | |
number = client.call :Entry_GetLastUsedSerialNumber | |
pp number | |
pp client.call :Entry_GetData, entityHandle: { SerialNumber: number } |
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
source :rubygems | |
gem "attr_extras" | |
gem "savon", ">2", github: "savonrb/savon" | |
group :development do | |
gem "pry" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment