taxbear 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3fb8147306789ac6efa1d0823fc9e4567c93ff08
4
- data.tar.gz: 0163982a788b881c63bd9a9ba74b5d4d32be13a6
3
+ metadata.gz: ee7df243ec9b37ad77afa11cae811c5d8b59bf0a
4
+ data.tar.gz: 553a58b8a16626dc983fc7047de87d923c1964dc
5
5
  SHA512:
6
- metadata.gz: 4aa79d68dcd0cb329f44b16d1077cea9bc51b8e71c7954f3b48c3ecfddf7f0cfec53e61c76b7e9f2a1b6e4d22be5808efbebc820e02bf77b9f593e52db5ccfeb
7
- data.tar.gz: 1325356af9f596d784a206b2d8a24a10ce4dc66acb54ee19aba4d2e731c009f2dabe2c75550606c398888856badf8c2451d868795b0de13551c87c3c771da852
6
+ metadata.gz: 01f20233998f3540f4e1c7de846848b52d22f4ea26df7f74e8e25b0ca0ce3b4ba4fce48f063290a55c4de7b5ad4aaca04822f382828415814ca4b5e2428951c2
7
+ data.tar.gz: 4e08d0850ebfd51d3022908e5dadace182de29c77b4db93cf4e43e0c6a2c0da8cf17fbb68f90b261f16df91b2f8c6a7bb76b49b13e45e30d24484510d424f490
@@ -2,3 +2,6 @@ language: ruby
2
2
  rvm:
3
3
  - 2.3.0
4
4
  before_install: gem install bundler -v 1.11.2
5
+ addons:
6
+ code_climate:
7
+ repo_token: 35170382ac79bbc14ea9cd8c1d41655cd7fb2eefd408eea597e082a247c03f21
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.3 (2016-10-18)
4
+ - Move login to a separate command.
5
+ - Validate token provided against TaxJar API.
6
+
3
7
  ## 0.1.2 (2016-10-18)
4
8
  - Improve CLI help.
5
9
 
data/README.md CHANGED
@@ -1,14 +1,26 @@
1
1
  # Taxbear
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/taxbear.svg)](https://badge.fury.io/rb/taxbear)
4
+ [![Build Status](https://travis-ci.org/jcypret/taxbear.svg?branch=master)](https://travis-ci.org/jcypret/taxbear)
5
+ [![Code Climate](https://codeclimate.com/github/jcypret/taxbear/badges/gpa.svg)](https://codeclimate.com/github/jcypret/taxbear)
6
+ [![Test Coverage](https://codeclimate.com/github/jcypret/taxbear/badges/coverage.svg)](https://codeclimate.com/github/jcypret/taxbear/coverage)
4
7
 
5
8
  ## Installation
6
9
 
7
- $ gem install taxbear
10
+ ```bash
11
+ $ gem install taxbear
12
+ ```
8
13
 
9
14
  ## Usage
10
15
 
11
- Lookup sales tax rates for a zip code:
16
+ (1) Set your TaxJar API token:
17
+
18
+ ```bash
19
+ $ taxbear login
20
+ #=> What is your TaxJar API token?
21
+ ```
22
+
23
+ (2) Lookup sales tax rates for a zip code:
12
24
 
13
25
  ```bash
14
26
  $ taxbear zip 72034
@@ -1,9 +1,9 @@
1
1
  require "taxbear/cli"
2
+ require "taxbear/config"
2
3
  require "taxbear/exceptions"
3
4
  require "taxbear/table_builder"
4
5
  require "taxbear/taxjar"
5
6
  require "taxbear/version"
6
7
 
7
8
  module Taxbear
8
- CONFIG_FILE = ENV["HOME"] + "/.taxbear"
9
9
  end
@@ -2,27 +2,52 @@ require "thor"
2
2
 
3
3
  module Taxbear
4
4
  class CLI < Thor
5
+ desc "login", "Set API key used to authenticate with TaxJar"
6
+ def login
7
+ valid_token = false
8
+ while !valid_token
9
+ token = ask("What is your TaxJar API token?")
10
+ valid_token = Taxjar.validate_token(token)
11
+ say_nope if !valid_token
12
+ end
13
+ Config.save_token(token)
14
+ print_success "Success! You are now ready to access the TaxJar API."
15
+ end
16
+
5
17
  desc "zip [ZIPCODE]", "Display sales tax rates for specified zip code"
6
18
  def zip(zipcode)
7
- ensure_api_key_is_available
8
19
  rates = Taxjar.get_rates_by_zipcode(zipcode)
9
20
  TableBuilder.print_rates_for_zipcode(rates, zipcode)
10
- rescue TaxjarError => e
21
+ rescue => e
11
22
  print_error(e.message)
12
23
  end
13
24
 
14
25
  private
15
26
 
16
- def ensure_api_key_is_available
17
- unless File.exists?(CONFIG_FILE)
18
- token = ask("What is your TaxJar API token?")
19
- File.write(CONFIG_FILE, token)
20
- end
21
- end
22
-
23
27
  def print_error(error)
24
28
  error_message = "✘ #{error}"
25
29
  puts set_color(error_message, :red)
26
30
  end
31
+
32
+ def print_success(message)
33
+ success_message = "✔︎ #{message}"
34
+ puts set_color(success_message, :green)
35
+ end
36
+
37
+ def say_nope
38
+ print_error(random_no)
39
+ end
40
+
41
+ def random_no
42
+ [
43
+ "Nope, that's not correct.",
44
+ "Sorry, not quite right.",
45
+ "Maybe you should check the website? https://app.taxjar.com/smartcalcs",
46
+ "A for effort… but no.",
47
+ "I'll give you a hint, it's a bunch of random unguessable letters/numbers.",
48
+ "Good try, but unfortunately wrong.",
49
+ "Hmm… that doesn't seem to be working. Maybe something different?"
50
+ ].sample
51
+ end
27
52
  end
28
53
  end
@@ -0,0 +1,32 @@
1
+ module Taxbear
2
+ class Config
3
+ # This class manages the configuration file stored on the user's computer.
4
+
5
+ class << self
6
+ # Checks whether the Taxbear config file exists on the users computer.
7
+ #
8
+ # @return [Boolean] whether the config file exists
9
+ def exists?
10
+ File.exist?(config_file)
11
+ end
12
+
13
+ # Returns the TaxJar API token loaded from the users config file.
14
+ #
15
+ # @return [String] the TaxJar API token
16
+ def get_token
17
+ File.read(config_file)
18
+ end
19
+
20
+ # Saves the token to a .taxbear config file in the users home directory.
21
+ def save_token(token)
22
+ File.write(config_file, token)
23
+ end
24
+
25
+ private
26
+
27
+ def config_file
28
+ File.join(Dir.home, "/.taxbear")
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,4 +1,4 @@
1
1
  module Taxbear
2
- class TaxjarError < StandardError
3
- end
2
+ class TaxjarError < StandardError; end
3
+ class TaxjarUnauthorized < StandardError; end
4
4
  end
@@ -10,13 +10,16 @@ module Taxbear
10
10
  # @param rates [Hash] the rates
11
11
  # @return [nil] returns nothing, prints to STDOUT
12
12
  def print_rates_for_zipcode(rates, zipcode)
13
- puts ""
14
- puts Terminal::Table.new(
13
+ output = ""
14
+ output << "\n"
15
+ output << Terminal::Table.new(
15
16
  title: "Sales Tax Rates for #{zipcode}",
16
17
  headings: align_each_cell_center(get_header_rows(rates)),
17
18
  rows: [align_each_cell_center(format_rates(rates))]
18
- )
19
- puts ""
19
+ ).to_s
20
+ output << "\n\n"
21
+
22
+ puts output
20
23
  end
21
24
 
22
25
  private
@@ -14,7 +14,8 @@ module Taxbear
14
14
  # @param zipcode [Integer] the zip code
15
15
  # @return [Hash] the rates returned by API
16
16
  def get_rates_by_zipcode(zipcode)
17
- response = get("/rates/#{zipcode}", {headers: headers})
17
+ require_api_key()
18
+ response = get("/rates/#{zipcode}", {headers: auth_header})
18
19
 
19
20
  if response.success?
20
21
  response.fetch("rate")
@@ -23,14 +24,26 @@ module Taxbear
23
24
  end
24
25
  end
25
26
 
27
+ # Takes a tokens and returns whether the token is valid.
28
+ #
29
+ # @param token [String] the token
30
+ # @return [Boolean] whether token is valid
31
+ def validate_token(token)
32
+ get("/categories", headers: auth_header(token)).success?
33
+ end
34
+
26
35
  private
27
36
 
28
- def headers
29
- {"Authorization" => "Token token=#{api_token}"}
37
+ def require_api_key
38
+ raise_unauthorized unless Config.exists?
39
+ end
40
+
41
+ def raise_unauthorized
42
+ raise TaxjarUnauthorized, "You must have a TaxJar API key set to access rates. Use `taxbear login`."
30
43
  end
31
44
 
32
- def api_token
33
- File.read(CONFIG_FILE)
45
+ def auth_header(token = Config.get_token)
46
+ {"Authorization" => "Token token=#{token}"}
34
47
  end
35
48
  end
36
49
  end
@@ -1,3 +1,3 @@
1
1
  module Taxbear
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -20,6 +20,9 @@ Gem::Specification.new do |spec|
20
20
  spec.add_development_dependency "bundler", "~> 1.11"
21
21
  spec.add_development_dependency "rake", "~> 10.0"
22
22
  spec.add_development_dependency "rspec", "~> 3.0"
23
+ spec.add_development_dependency "webmock", "~> 2.1.0"
24
+ spec.add_development_dependency "aruba", "~> 0.14.2"
25
+ spec.add_development_dependency "codeclimate-test-reporter"
23
26
 
24
27
  spec.add_dependency "thor", "~> 0.19"
25
28
  spec.add_dependency "httparty", "~> 0.14"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taxbear
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Cypret
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-18 00:00:00.000000000 Z
11
+ date: 2016-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,48 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: aruba
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.14.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.14.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: codeclimate-test-reporter
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
55
97
  - !ruby/object:Gem::Dependency
56
98
  name: thor
57
99
  requirement: !ruby/object:Gem::Requirement
@@ -115,6 +157,7 @@ files:
115
157
  - exe/taxbear
116
158
  - lib/taxbear.rb
117
159
  - lib/taxbear/cli.rb
160
+ - lib/taxbear/config.rb
118
161
  - lib/taxbear/exceptions.rb
119
162
  - lib/taxbear/table_builder.rb
120
163
  - lib/taxbear/taxjar.rb