Skip to content

Commit 3f52ff1

Browse files
committed
Initial Ruby SDK commit
1 parent 78429bf commit 3f52ff1

17 files changed

+441
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,6 @@ build-iPhoneSimulator/
4848

4949
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
5050
.rvmrc
51+
52+
.DS_Store
53+
tags

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--backtrace

.yardopts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--no-private
2+
--protected
3+
--markup markdown
4+
-
5+
LICENSE

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source "http://rubygems.org"
2+
3+
gemspec

avatax.gemspec

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require File.expand_path('../lib/avatax/version', __FILE__)
2+
3+
Gem::Specification.new do |s|
4+
s.add_development_dependency('rake', '~> 12.0.0')
5+
s.add_development_dependency('rspec', '~> 3.0.0')
6+
s.add_development_dependency('webmock', '>= 2.0.0')
7+
s.add_runtime_dependency('faraday', '>= 0.10')
8+
s.add_runtime_dependency('faraday_middleware', '>= 0.10')
9+
s.add_runtime_dependency('multi_json', '>= 1.0.3')
10+
s.add_runtime_dependency('hashie', '>= 3.0.0')
11+
s.authors = ["Marcus Vorwaller"]
12+
s.description = %q{A Ruby wrapper for the AvaTax REST and Search APIs}
13+
s.post_install_message =<<eos
14+
********************************************************************************
15+
16+
AvaTax REST API
17+
------------------------------
18+
Our developer site documents the AvaTax REST API.
19+
(http://developer.avatax.com).
20+
Blog
21+
----------------------------
22+
The Developer Blog is a great place to learn more about the API and AvaTax integrations
23+
Subscribe to the RSS feed be notified of new posts:
24+
(http://developer.avatax.com/blog).
25+
26+
********************************************************************************
27+
eos
28+
s.email = ['marcus.vorwaller@avalara.com']
29+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
30+
s.files = `git ls-files`.split("\n")
31+
s.homepage = 'https://github.com/avadev/AvaTax-REST-V2-Ruby-SDK'
32+
s.name = 'avatax'
33+
s.platform = Gem::Platform::RUBY
34+
s.require_paths = ['lib']
35+
s.required_rubygems_version = Gem::Requirement.new('>= 2.0.0') if s.respond_to? :required_rubygems_version=
36+
s.summary = %q{Ruby wrapper for the AvaTax API}
37+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
38+
s.version = AvaTax::VERSION.dup
39+
end

lib/avatax.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require File.expand_path('../avatax/configuration', __FILE__)
2+
require File.expand_path('../avatax/api', __FILE__)
3+
require File.expand_path('../avatax/client', __FILE__)
4+
5+
module AvaTax
6+
extend Configuration
7+
8+
# Alias for Avatax::Client.new
9+
#
10+
# @return [Avatax::Client]
11+
def self.client(options={})
12+
AvaTax::Client.new(options)
13+
end
14+
15+
# Delegate to AvaTax::Client
16+
def self.method_missing(method, *args, &block)
17+
return super unless client.respond_to?(method)
18+
client.send(method, *args, &block)
19+
end
20+
21+
# Delegate to AvaTax::Client
22+
def self.respond_to?(method, include_all=false)
23+
return client.respond_to?(method, include_all) || super
24+
end
25+
26+
end

lib/avatax/api.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require File.expand_path('../connection', __FILE__)
2+
require File.expand_path('../request', __FILE__)
3+
4+
module AvaTax
5+
class API
6+
7+
attr_accessor *Configuration::VALID_OPTIONS_KEYS
8+
9+
def initialize(options={})
10+
options = AvaTax.options.merge(options)
11+
Configuration::VALID_OPTIONS_KEYS.each do |key|
12+
send("#{key}=", options[key])
13+
end
14+
end
15+
16+
def config
17+
conf = {}
18+
Configuration::VALID_OPTIONS_KEYS.each do |key|
19+
conf[key] = send key
20+
end
21+
conf
22+
end
23+
24+
include Connection
25+
include Request
26+
end
27+
end

lib/avatax/client.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module AvaTax
2+
class Client < API
3+
Dir[File.expand_path('../client/*.rb', __FILE__)].each{|f| require f}
4+
5+
include AvaTax::Client::Accounts
6+
end
7+
end

lib/avatax/client/accounts.rb

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module AvaTax
2+
class Client
3+
module Accounts
4+
# Retrieve all accounts
5+
#
6+
# # For Registrar Use Only
7+
#
8+
# @param options [Hash] with include, filter, top, skip, and order_by params
9+
# @return [Hashie::Mash] The requested accounts.
10+
# @example Returns a list of accounts
11+
# AvaTax.accounts({ $filter: '' })
12+
#
13+
# _You must be authenticated as an Avalara Registrar to use this route_
14+
# @see http://developer.avalara.com/avatax/filtering-in-rest/ More information on filtering in REST
15+
def accounts(options={})
16+
get('/api/v2/accounts', options)
17+
end
18+
19+
# Retrieve an account
20+
#
21+
# @param include [Array] an array of child objects to return underneath the primary object.
22+
# The include array can contain
23+
#
24+
# * Subscriptions
25+
# * Users
26+
# @return [Hashie::Mash] The requested accounts.
27+
# @example Returns a list of accounts
28+
# AvaTax.accounts({ $filter: 'id in (111444, 222555)' })
29+
def account(id, include=[])
30+
if !include.empty?
31+
options = {
32+
'$include': include.join(','),
33+
}
34+
end
35+
get("/api/v2/accounts/#{id}", options || {})
36+
end
37+
38+
# Resets the existing license key for this account to a new key. To reset your account, you must specify the ID of the account you wish to reset and confirm the action. Resetting a license key cannot be undone. Any previous license keys will immediately cease to work when a new key is created.
39+
#
40+
# @param options [Hash] should be a hash with { confirmResetLicenseKey: true }
41+
#
42+
# @return [Hashie::Mash] The result of the action
43+
#
44+
# @example
45+
# AvaTax.reset_license_key(4444999, { confirmLicenseKey: true })
46+
#```
47+
# {
48+
# "accountId": 123456789,
49+
# "privateLicenseKey": "742A02AA463CDE741E932EC365360CA3CF378BD9",
50+
# "httpRequestHeader": "Basic MTIzNDU2Nzg5Ojc0MkEwMkFBNDYzQ0RFNzQxRTkzMkVDMzY1MzYwQ0EzQ0YzNzhCRDk="
51+
# }
52+
#```
53+
def reset_license_key(id, options={})
54+
post("/api/v2/accounts/#{id}/resetlicensekey", options)
55+
end
56+
57+
end
58+
end
59+
end

lib/avatax/configuration.rb

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require 'faraday'
2+
require File.expand_path('../version', __FILE__)
3+
4+
module AvaTax
5+
module Configuration
6+
7+
VALID_OPTIONS_KEYS = [
8+
:app_name,
9+
:app_version,
10+
:machine_name,
11+
:environment,
12+
:endpoint,
13+
:user_agent,
14+
:username,
15+
:password,
16+
:connection_options,
17+
:logger,
18+
].freeze
19+
20+
DEFAULT_APP_NAME = nil
21+
DEFAULT_APP_VERSION = nil
22+
DEFAULT_MACHINE_NAME = nil
23+
DEFAULT_ENDPOINT = 'https://rest.avatax.com'
24+
DEFAULT_USER_AGENT = "AvaTax Ruby Gem #{AvaTax::VERSION}".freeze
25+
DEFAULT_USERNAME = nil
26+
DEFAULT_PASSWORD = nil
27+
DEFAULT_CONNECTION_OPTIONS = {}
28+
DEFAULT_LOGGER = false
29+
30+
attr_accessor *VALID_OPTIONS_KEYS
31+
32+
# Reset config values when extended
33+
def self.extended(base)
34+
base.reset
35+
end
36+
37+
# Allow configuration options to be set in a block
38+
def configure
39+
yield self
40+
end
41+
42+
def options
43+
VALID_OPTIONS_KEYS.inject({}) do |option, key|
44+
option.merge!(key => send(key))
45+
end
46+
end
47+
48+
def reset
49+
self.app_name = DEFAULT_APP_NAME
50+
self.app_version = DEFAULT_APP_VERSION
51+
self.machine_name = DEFAULT_MACHINE_NAME
52+
self.endpoint = DEFAULT_ENDPOINT
53+
self.user_agent = DEFAULT_USER_AGENT
54+
self.username = DEFAULT_USERNAME
55+
self.password = DEFAULT_PASSWORD
56+
self.connection_options = DEFAULT_CONNECTION_OPTIONS
57+
self.logger = DEFAULT_LOGGER
58+
end
59+
60+
end
61+
end

lib/avatax/connection.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'faraday_middleware'
2+
3+
module AvaTax
4+
5+
module Connection
6+
private
7+
8+
def connection
9+
options = {
10+
:headers => {'Accept' => "application/json; charset=utf-8", 'User-Agent' => user_agent},
11+
:url => endpoint,
12+
}.merge(connection_options)
13+
14+
c = Faraday::Connection.new(options)
15+
c.use Faraday::Response::ParseJson
16+
c.response :logger if logger
17+
c.use Faraday::Request::UrlEncoded
18+
c.basic_auth(username, password)
19+
20+
c
21+
end
22+
end
23+
end

lib/avatax/request.rb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'hashie'
2+
3+
module AvaTax
4+
module Request
5+
6+
def get(path, options)
7+
request(:get, path, options)
8+
end
9+
10+
def post(path, options)
11+
request(:post, path, options)
12+
end
13+
14+
def put(path, options)
15+
request(:put, path, options)
16+
end
17+
18+
def delete(path, options)
19+
request(:delete, path, options)
20+
end
21+
22+
def request(method, path, options)
23+
response = connection.send('get') do |request|
24+
case method
25+
when :get, :delete
26+
request.url(URI.encode(path), options)
27+
when :post, :put
28+
request.path = URI.encode(path)
29+
request.body = options unless options.empty?
30+
end
31+
end
32+
33+
::Hashie::Mash.new response.body
34+
end
35+
36+
end
37+
end

lib/avatax/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module AvaTax
2+
VERSION = '0.0.1'.freeze unless defined?(::AvaTax::VERSION)
3+
end

spec/avatax/client/accounts_spec.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require File.expand_path('../../../spec_helper', __FILE__)
2+
3+
describe AvaTax::Client do
4+
before do
5+
@client = AvaTax::Client.new()
6+
end
7+
8+
describe ".accounts" do
9+
before do
10+
stub_get("/api/v2/accounts").
11+
to_return(:body => fixture("accounts.json"), :headers => {:content_type => "application/json; charset=utf-8"})
12+
end
13+
14+
it "should get the correct resource" do
15+
@client.accounts()
16+
expect(a_get("/api/v2/accounts")).to have_been_made
17+
end
18+
19+
it "should return an array of accounts" do
20+
accounts = @client.accounts
21+
expect(accounts).to be_a Object
22+
expect(accounts['value'].first['id']).to equal 200000251
23+
expect(accounts.value.first.id).to equal 200000251
24+
end
25+
end
26+
end

0 commit comments

Comments
 (0)