fortnox-ruby 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.env.example +3 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/fortnox-ruby.gemspec +26 -0
- data/lib/fortnox.rb +30 -0
- data/lib/fortnox/client.rb +61 -0
- data/lib/fortnox/configurable.rb +33 -0
- data/lib/fortnox/default.rb +31 -0
- data/lib/fortnox/error.rb +35 -0
- data/lib/fortnox/resource.rb +15 -0
- data/lib/fortnox/resources/customer.rb +11 -0
- data/lib/fortnox/version.rb +3 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 877675a2208a85a6c996d1f0f879632036f1e571
|
4
|
+
data.tar.gz: 0f20a586bac0adeaf84e0bea11e26173678c05cd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4eb2aa4d8a9ea18e007e5d84f3b33140a74ac2e4bc8f9e913846c6f2b37bf89bb3537d5a001d3cbf9fdb5455a527191ea78de6d27e9ac9593fc44c3764b20148
|
7
|
+
data.tar.gz: 785bebce3acf38def0cdac0b9676fe47241154e41c761524833733e212411e01cc2724502d1bd8014206e1f68e51756b16b99f23d9e2d0931680aa67446309de
|
data/.env.example
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Mark
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Fortnox
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fortnox`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'fortnox'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install fortnox
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fortnox.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "fortnox"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fortnox/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fortnox-ruby"
|
8
|
+
spec.version = Fortnox::VERSION
|
9
|
+
spec.authors = ["Mark"]
|
10
|
+
spec.email = ["birmanmark@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby wrapper for Fortnox API}
|
13
|
+
spec.homepage = "https://github.com/mbirman/fortnox"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "dotenv", "~> 2.0"
|
25
|
+
spec.add_dependency "httparty", "~> 0.13"
|
26
|
+
end
|
data/lib/fortnox.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'fortnox/version'
|
3
|
+
require 'fortnox/client'
|
4
|
+
require 'fortnox/default'
|
5
|
+
|
6
|
+
module Fortnox
|
7
|
+
class << self
|
8
|
+
include Fortnox::Configurable
|
9
|
+
|
10
|
+
def client
|
11
|
+
@client ||= Fortnox::Client.new(options)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def respond_to_missing?(method_name, include_private=false)
|
17
|
+
client.respond_to?(method_name, include_private)
|
18
|
+
end
|
19
|
+
|
20
|
+
def method_missing(method_name, *args, &block)
|
21
|
+
if client.respond_to?(method_name)
|
22
|
+
client.send(method_name, *args, &block)
|
23
|
+
else
|
24
|
+
super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
Fortnox.setup
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'fortnox/configurable'
|
2
|
+
require 'fortnox/resource'
|
3
|
+
require 'fortnox/error'
|
4
|
+
|
5
|
+
module Fortnox
|
6
|
+
class Client
|
7
|
+
extend Forwardable
|
8
|
+
|
9
|
+
include Fortnox::Configurable
|
10
|
+
include HTTParty
|
11
|
+
|
12
|
+
# debug_output $stdout
|
13
|
+
|
14
|
+
delegate [:get, :post, :put, :delete] => self
|
15
|
+
|
16
|
+
headers 'Accept' => 'application/json', 'Content-Type' => 'application/json'
|
17
|
+
|
18
|
+
format :json
|
19
|
+
|
20
|
+
def initialize(options = {})
|
21
|
+
Fortnox::Configurable.keys.each do |key|
|
22
|
+
instance_variable_set(:"@#{key}", options[key] || Fortnox.instance_variable_get(:"@#{key}"))
|
23
|
+
end
|
24
|
+
|
25
|
+
self.class.default_options.merge!(base_uri: api_endpoint)
|
26
|
+
end
|
27
|
+
|
28
|
+
def fetch_access_token
|
29
|
+
headers = {
|
30
|
+
'Client-Secret' => client_secret,
|
31
|
+
'Authorization-Code' => authorization_code
|
32
|
+
}
|
33
|
+
|
34
|
+
response = get(root_path, headers: headers)
|
35
|
+
@access_token = extract_token(response)
|
36
|
+
end
|
37
|
+
|
38
|
+
def request(verb, url, options = {})
|
39
|
+
response = send(verb, url, options.merge(headers: auth_headers))
|
40
|
+
if response.success?
|
41
|
+
response
|
42
|
+
else
|
43
|
+
raise Fortnox::Error.from_response(response)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def extract_token(response)
|
50
|
+
response.fetch('Authorization').fetch('AccessToken')
|
51
|
+
end
|
52
|
+
|
53
|
+
def root_path
|
54
|
+
'/'.freeze
|
55
|
+
end
|
56
|
+
|
57
|
+
def auth_headers
|
58
|
+
{ 'Client-Secret' => client_secret, 'Access-Token' => access_token}
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Fortnox
|
2
|
+
module Configurable
|
3
|
+
|
4
|
+
attr_accessor :access_token, :client_id, :client_secret, :authorization_code, :api_endpoint
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def keys
|
8
|
+
@keys ||= [
|
9
|
+
:access_token,
|
10
|
+
:api_endpoint,
|
11
|
+
:client_id,
|
12
|
+
:client_secret,
|
13
|
+
:authorization_code
|
14
|
+
]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def configure
|
19
|
+
yield self
|
20
|
+
end
|
21
|
+
|
22
|
+
def reset!
|
23
|
+
Fortnox::Configurable.keys.each do |key|
|
24
|
+
instance_variable_set(:"@#{key}", Fortnox::Default.options[key])
|
25
|
+
end
|
26
|
+
end
|
27
|
+
alias setup reset!
|
28
|
+
|
29
|
+
def options
|
30
|
+
Hash[Fortnox::Configurable.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Fortnox
|
2
|
+
module Default
|
3
|
+
API_ENDPOINT = 'https://api.fortnox.se/3/'.freeze
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def options
|
7
|
+
Hash[Fortnox::Configurable.keys.map{|key| [key, send(key)]}]
|
8
|
+
end
|
9
|
+
|
10
|
+
def access_token
|
11
|
+
ENV['FORTNOX_ACCESS_TOKEN']
|
12
|
+
end
|
13
|
+
|
14
|
+
def client_id
|
15
|
+
ENV['FORTNOX_CLIENT_ID']
|
16
|
+
end
|
17
|
+
|
18
|
+
def client_secret
|
19
|
+
ENV['FORTNOX_CLIENT_SECRET']
|
20
|
+
end
|
21
|
+
|
22
|
+
def api_endpoint
|
23
|
+
ENV['FORTNOX_API_ENDPOINT'] || API_ENDPOINT
|
24
|
+
end
|
25
|
+
|
26
|
+
def authorization_code
|
27
|
+
ENV['FORTNOX_AUTHORIZATION_CODE']
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Fortnox
|
2
|
+
class Error < StandardError
|
3
|
+
|
4
|
+
class BadRequest < Error; end
|
5
|
+
class Forbidden < Error; end
|
6
|
+
class NotFound < Error; end
|
7
|
+
class InternalServerError < Error; end
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def from_response(response)
|
11
|
+
error_class(response.code).new(error_message(response.parsed_response))
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def error_class(code)
|
17
|
+
case code
|
18
|
+
when 400 then BadRequest
|
19
|
+
when 403 then Forbidden
|
20
|
+
when 404 then NotFound
|
21
|
+
when 500 then InternalServerError
|
22
|
+
else Fortnox::Error
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def error_message(response)
|
27
|
+
info = response['ErrorInformation']
|
28
|
+
message = info['Message']
|
29
|
+
code = info['Code']
|
30
|
+
|
31
|
+
"#{message} (code: #{code})"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fortnox-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mark
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: dotenv
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.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.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: httparty
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.13'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.13'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- birmanmark@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".env.example"
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- fortnox-ruby.gemspec
|
101
|
+
- lib/fortnox.rb
|
102
|
+
- lib/fortnox/client.rb
|
103
|
+
- lib/fortnox/configurable.rb
|
104
|
+
- lib/fortnox/default.rb
|
105
|
+
- lib/fortnox/error.rb
|
106
|
+
- lib/fortnox/resource.rb
|
107
|
+
- lib/fortnox/resources/customer.rb
|
108
|
+
- lib/fortnox/version.rb
|
109
|
+
homepage: https://github.com/mbirman/fortnox
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.4.8
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Ruby wrapper for Fortnox API
|
133
|
+
test_files: []
|