frilans_finans_api 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.MD +64 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/frilans_finans_api/document.rb +72 -0
- data/lib/frilans_finans_api/nil_event_logger.rb +7 -0
- data/lib/frilans_finans_api/nil_logger.rb +11 -0
- data/lib/frilans_finans_api/parse_log.rb +61 -0
- data/lib/frilans_finans_api/resource.rb +25 -0
- data/lib/frilans_finans_api/statuses.rb +55 -0
- data/lib/frilans_finans_api/test_helper.rb +52 -0
- data/lib/frilans_finans_api/version.rb +5 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: '0012708294cbf004471c28d9c74c12eb34aff89f'
|
4
|
+
data.tar.gz: fcae44a56ac49405fad283e6ab5a66e5289ef68a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5290b1c50fc7d003885038c10aef303dc98fcd47c4792009ab2eadb2f3feca18f8883b358be4d57590e71c24569d3fef73071a9f6d42fcbe1d8e271ddf581c62
|
7
|
+
data.tar.gz: 3f08bab11dab79cf670ef17293262d2117568b94a3b95de3d6651ae0ad43b223216881e8acb428f15d7a7b2aac5574b0389f9e9fd78255fa937748d597d12e50
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Jacob Burenstam
|
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,64 @@
|
|
1
|
+
# FrilansFinansApi
|
2
|
+
|
3
|
+
Interact with Frilans Finans API.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'frilans_finans_api'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install frilans_finans_api
|
20
|
+
|
21
|
+
__Configure__
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
FrilansFinansApi.configure do |config|
|
25
|
+
config.client_klass = FrilansFinansApi::Client
|
26
|
+
config.base_uri = ENV.fetch('FRILANS_FINANS_BASE_URI')
|
27
|
+
config.client_id = ENV.fetch('FRILANS_FINANS_CLIENT_ID')
|
28
|
+
config.client_secret = ENV.fetch('FRILANS_FINANS_CLIENT_SECRET')
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
include FrilansFinansApi
|
36
|
+
|
37
|
+
# GET /professions?page=1
|
38
|
+
document = Profession.index(page: 1)
|
39
|
+
document.resources.each do |profession|
|
40
|
+
puts profession.attributes['title']
|
41
|
+
end
|
42
|
+
document.total_pages
|
43
|
+
|
44
|
+
# Iterate over all professions, page by page
|
45
|
+
Profession.walk(page: 1) do |document|
|
46
|
+
document.resources.each do |profession|
|
47
|
+
puts profession.attributes['title']
|
48
|
+
end
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
## Development
|
53
|
+
|
54
|
+
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.
|
55
|
+
|
56
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/buren/frilans_finans_api.
|
61
|
+
|
62
|
+
## License
|
63
|
+
|
64
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'frilans_finans_api'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module FrilansFinansApi
|
6
|
+
class Document
|
7
|
+
attr_reader :status, :json, :uri
|
8
|
+
|
9
|
+
def initialize(response)
|
10
|
+
@json = JSON.parse(response.body)
|
11
|
+
@status = response.code
|
12
|
+
@uri = response.request.uri.to_s
|
13
|
+
end
|
14
|
+
|
15
|
+
def resources
|
16
|
+
@resources ||= begin
|
17
|
+
if collection?
|
18
|
+
data.map { |resource| Resource.new(resource) }
|
19
|
+
else
|
20
|
+
[Resource.new(data)]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def resource
|
26
|
+
@resource ||= begin
|
27
|
+
resource_data = collection? ? data.first : data
|
28
|
+
Resource.new(resource_data)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def collection?
|
33
|
+
data = json['data']
|
34
|
+
return false if data.nil? || data.is_a?(Hash)
|
35
|
+
|
36
|
+
true
|
37
|
+
end
|
38
|
+
|
39
|
+
def data
|
40
|
+
json['data']
|
41
|
+
end
|
42
|
+
|
43
|
+
def next_page_link
|
44
|
+
json.dig('links', 'next')
|
45
|
+
end
|
46
|
+
|
47
|
+
def current_page
|
48
|
+
json.dig('meta', 'pagination', 'page', 'number')
|
49
|
+
end
|
50
|
+
|
51
|
+
def per_page
|
52
|
+
json.dig('meta', 'pagination', 'page', 'size')
|
53
|
+
end
|
54
|
+
|
55
|
+
def total_pages
|
56
|
+
json.dig('meta', 'pagination', 'pages')
|
57
|
+
end
|
58
|
+
|
59
|
+
def total
|
60
|
+
json.dig('meta', 'pagination', 'items')
|
61
|
+
end
|
62
|
+
|
63
|
+
def count
|
64
|
+
json.dig('meta', 'pagination', 'items')
|
65
|
+
end
|
66
|
+
|
67
|
+
def error_status?
|
68
|
+
# Consider HTTP status 3XX, 4XX and 5XX as errors
|
69
|
+
status >= 300
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FrilansFinansApi
|
4
|
+
module ParseLog
|
5
|
+
class LogRequest
|
6
|
+
attr_reader :uri, :params, :body, :status
|
7
|
+
|
8
|
+
def initialize(uri:, params:, body:, status:)
|
9
|
+
@uri = uri
|
10
|
+
@params = params
|
11
|
+
@body = body
|
12
|
+
@status = status
|
13
|
+
end
|
14
|
+
|
15
|
+
def access_token_request?
|
16
|
+
uri.include?('auth/accesstoken')
|
17
|
+
end
|
18
|
+
|
19
|
+
def inspect
|
20
|
+
parts = [
|
21
|
+
"status: #{status}",
|
22
|
+
"uri: #{uri}",
|
23
|
+
"params: #{params}",
|
24
|
+
"body: #{body}"
|
25
|
+
].join(', ')
|
26
|
+
"#<#{self.class.name} #{parts}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.call(filename)
|
31
|
+
lines = []
|
32
|
+
File.foreach(filename) do |log_line|
|
33
|
+
next unless log_line.index('[FrilansFinansApi::Request]')
|
34
|
+
|
35
|
+
# MATCH BODY
|
36
|
+
body_match = 'BODY: '
|
37
|
+
body_match_index = log_line.index(body_match)
|
38
|
+
body_content_index = body_match_index + body_match.length
|
39
|
+
body_json = log_line[body_content_index..-1].strip
|
40
|
+
body = JSON.parse(body_json)
|
41
|
+
|
42
|
+
# MATCH STATUS
|
43
|
+
status = string_between_markers(log_line, ' STATUS: ', ' BODY: ')
|
44
|
+
|
45
|
+
# MATCH PARAM
|
46
|
+
json_string = string_between_markers(log_line, ' PARAMS: ', ' STATUS: ')
|
47
|
+
params = JSON.parse(json_string)
|
48
|
+
|
49
|
+
# MATCH URI
|
50
|
+
uri = string_between_markers(log_line, ' URI: ', ' PARAMS: ')
|
51
|
+
|
52
|
+
lines << LogRequest.new(uri: uri, params: params, body: body, status: status)
|
53
|
+
end
|
54
|
+
lines
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.string_between_markers(string, start_marker, end_marker)
|
58
|
+
string[/#{Regexp.escape(start_marker)}(.*?)#{Regexp.escape(end_marker)}/m, 1]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FrilansFinansApi
|
4
|
+
class Resource
|
5
|
+
def initialize(document)
|
6
|
+
@data = document || {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def type
|
10
|
+
@data['type']
|
11
|
+
end
|
12
|
+
|
13
|
+
def id
|
14
|
+
@data['id']
|
15
|
+
end
|
16
|
+
|
17
|
+
def attributes
|
18
|
+
@data['attributes']
|
19
|
+
end
|
20
|
+
|
21
|
+
def self_link
|
22
|
+
@data.dig('links', 'self')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FrilansFinansApi
|
4
|
+
module Statuses
|
5
|
+
module Invoice
|
6
|
+
STATUSES = {
|
7
|
+
1 => 'Not paid',
|
8
|
+
2 => 'Paid',
|
9
|
+
3 => 'Credit invoice',
|
10
|
+
4 => 'Credited',
|
11
|
+
5 => 'Client loss',
|
12
|
+
6 => 'Partly paid',
|
13
|
+
7 => 'Saved',
|
14
|
+
9 => 'Credit errand',
|
15
|
+
10 => 'Dummy invoice',
|
16
|
+
11 => 'ROT/RUT invoice',
|
17
|
+
12 => 'ROT/RUT',
|
18
|
+
13 => 'Skatteverket (Swedish Tax Agency)'
|
19
|
+
}.freeze
|
20
|
+
|
21
|
+
PAYMENT_STATUS = {
|
22
|
+
1 => 'Not paid',
|
23
|
+
2 => 'Paid',
|
24
|
+
3 => 'Partly paid',
|
25
|
+
4 => 'Started'
|
26
|
+
}.freeze
|
27
|
+
|
28
|
+
APPROVAL_STATUS = {
|
29
|
+
1 => 'Waiting on approval',
|
30
|
+
2 => 'Approved',
|
31
|
+
3 => 'Not approved',
|
32
|
+
4 => 'Waiting on payment',
|
33
|
+
5 => 'Saved'
|
34
|
+
}.freeze
|
35
|
+
|
36
|
+
def self.status(status_int, with_id: false)
|
37
|
+
_format_name(status_int, STATUSES[status_int], with_id)
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.payment_status(status_int, with_id: false)
|
41
|
+
_format_name(status_int, PAYMENT_STATUS[status_int], with_id)
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.approval_status(status_int, with_id: false)
|
45
|
+
_format_name(status_int, APPROVAL_STATUS[status_int], with_id)
|
46
|
+
end
|
47
|
+
|
48
|
+
def self._format_name(status_int, name, with_id)
|
49
|
+
return nil if name.nil?
|
50
|
+
return "##{status_int} " + name if with_id
|
51
|
+
name
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FrilansFinansApi
|
4
|
+
module TestHelper
|
5
|
+
module_function
|
6
|
+
|
7
|
+
def isolate_frilans_finans_client(klass)
|
8
|
+
before_klass = FrilansFinansApi.config.client_klass
|
9
|
+
FrilansFinansApi.config.client_klass = klass
|
10
|
+
result = yield(before_klass)
|
11
|
+
FrilansFinansApi.config.client_klass = before_klass
|
12
|
+
result
|
13
|
+
end
|
14
|
+
|
15
|
+
def stub_frilans_finans_auth_request
|
16
|
+
# Stub auth request
|
17
|
+
base_uri = FrilansFinansApi.config.base_uri
|
18
|
+
headers = { 'User-Agent' => 'FrilansFinansAPI - Ruby client' }
|
19
|
+
body = [
|
20
|
+
'grant_type=client_credentials',
|
21
|
+
"client_id=#{FrilansFinansApi.config.client_id}",
|
22
|
+
"client_secret=#{FrilansFinansApi.config.client_secret}"
|
23
|
+
].join('&')
|
24
|
+
|
25
|
+
response_body = JSON.dump(
|
26
|
+
'access_token' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
|
27
|
+
'token_type' => 'Bearer',
|
28
|
+
'expires_in' => 1200
|
29
|
+
)
|
30
|
+
|
31
|
+
stub_request(:post, "#{base_uri}/auth/accesstoken").
|
32
|
+
with(body: body, headers: headers).
|
33
|
+
to_return(status: 200, body: response_body, headers: {})
|
34
|
+
end
|
35
|
+
|
36
|
+
def frilans_finans_authed_request_headers
|
37
|
+
{
|
38
|
+
'Authorization' => 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
|
39
|
+
'Content-Type' => 'application/json',
|
40
|
+
'User-Agent' => 'FrilansFinansAPI - Ruby client'
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
def mock_httparty_response(code: 200, body: '{}', uri: 'http://example.com')
|
45
|
+
request_struct = Struct.new(:uri)
|
46
|
+
request = request_struct.new(URI(uri))
|
47
|
+
|
48
|
+
response_struct = Struct.new(:code, :body, :request)
|
49
|
+
response_struct.new(code, body, request)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: frilans_finans_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jacob Burenstam
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.13'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: webmock
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.11'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.11'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description: Interact with Frilans Finans API (still under development)
|
84
|
+
email:
|
85
|
+
- burenstam@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- LICENSE.txt
|
91
|
+
- README.MD
|
92
|
+
- bin/console
|
93
|
+
- bin/setup
|
94
|
+
- lib/frilans_finans_api/document.rb
|
95
|
+
- lib/frilans_finans_api/nil_event_logger.rb
|
96
|
+
- lib/frilans_finans_api/nil_logger.rb
|
97
|
+
- lib/frilans_finans_api/parse_log.rb
|
98
|
+
- lib/frilans_finans_api/resource.rb
|
99
|
+
- lib/frilans_finans_api/statuses.rb
|
100
|
+
- lib/frilans_finans_api/test_helper.rb
|
101
|
+
- lib/frilans_finans_api/version.rb
|
102
|
+
homepage: https://github.com/buren/frilans_finans_api
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.6.13
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Interact with Frilans Finans API
|
126
|
+
test_files: []
|