fakturownia_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a0feb037e85b52731732c639ec90277417840ac7
4
+ data.tar.gz: f2ce6405b249a71076262794cd7d0fa24e384562
5
+ SHA512:
6
+ metadata.gz: 7bc2c5ca4fea74cfcefc410bf12b713bfff60a09b5a80ba9e065f22c0379382a2035b5fd7e53a732a6eb5fdc61c54f7cfdb050bf3f9e215fc69e2fc2f18afd30
7
+ data.tar.gz: 0cac2d26746d53a5ff522be8b7d98a0d01bb7f35ec10f91f7ad6276028955c43dcded68fde295f571a8767a40324f880f48599431199a3603fcf105b39a9be86
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fakturownia.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Sebastian Wojtczak
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Fakturownia
2
+
3
+ Ruby wrapper around API of invoice service fakturownia.pl
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'fakturownia'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install fakturownia
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/fakturownia/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ # If you want to make this the default task
7
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fakturownia/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fakturownia_api"
8
+ spec.version = Fakturownia::VERSION
9
+ spec.authors = ["Shelly Cloud team"]
10
+ spec.email = ["devs@shellycloud.com"]
11
+ spec.summary = %q{Ruby API for invoicing service fakturownia.pl}
12
+ spec.homepage = "https://github.com/shellycloud/fakturownia"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency "rest-client"
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "fakeweb"
24
+ spec.add_development_dependency "rspec", "~> 3.1.0"
25
+ end
@@ -0,0 +1,15 @@
1
+ module Fakturownia
2
+ module Api
3
+ class Base
4
+ attr_reader :client
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def connection
11
+ @connection ||= Fakturownia::Connection.new(client)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ module Fakturownia
2
+ module Api
3
+ class Invoice < Base
4
+ def list(options = {})
5
+ connection.get("/invoices", options)
6
+ end
7
+
8
+ def show(id, options = {})
9
+ connection.get("/invoices/#{id}", options)
10
+ end
11
+
12
+ def create(params)
13
+ connection.post("/invoices", invoice: params)
14
+ end
15
+
16
+ def update(id, params)
17
+ connection.put("/invoices/#{id}", invoice: params)
18
+ end
19
+
20
+ def delete(id)
21
+ connection.delete("/invoices/#{id}")
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+ module Fakturownia
2
+ class Client
3
+ attr_reader :subdomain, :api_token
4
+
5
+ def initialize(options = {})
6
+ @subdomain = options[:subdomain] ||
7
+ raise(ArgumentError.new('subdomain is required'))
8
+ @api_token = options[:api_token] ||
9
+ raise(ArgumentError.new('api_token is required'))
10
+ end
11
+
12
+ def invoice
13
+ Fakturownia::Api::Invoice.new(self)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,86 @@
1
+ require "rest_client"
2
+ require "json"
3
+
4
+ module Fakturownia
5
+ class Connection
6
+ attr_reader :client, :format
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ def get(path, options = {})
13
+ @format = options.fetch(:format, :json)
14
+ request(:get, path, options)
15
+ end
16
+
17
+ def post(path, options = {})
18
+ request(:post, path, options)
19
+ end
20
+
21
+ def put(path, options = {})
22
+ request(:put, path, options)
23
+ end
24
+
25
+ def delete(path, options = {})
26
+ request(:delete, path, options)
27
+ end
28
+
29
+ def request(method, path, options = {})
30
+ options = request_parameters(method, path, options)
31
+ RestClient::Request.execute(options) do |response, request|
32
+ process_response(response)
33
+ end
34
+ end
35
+
36
+ def process_response(response)
37
+ case response.code.to_i
38
+ when 200...300
39
+ body = parse(response)
40
+ else
41
+ raise Fakturownia::APIException.new(response.body, response.code)
42
+ end
43
+ response.return!
44
+ body
45
+ end
46
+
47
+ def parse(response)
48
+ case format
49
+ when :json
50
+ JSON.parse(response.body) rescue JSON::ParserError && {}
51
+ when :pdf
52
+ response.body
53
+ else
54
+ raise StandardError.new("Unknown format #{format}")
55
+ end
56
+ end
57
+
58
+ def request_parameters(method, path, options = {})
59
+ parameters = {
60
+ method: method,
61
+ url: "#{api_url}#{path}.#{format}",
62
+ headers: headers
63
+
64
+ }
65
+ unless [:get, :head].include?(method)
66
+ parameters = parameters.merge(payload: options.to_json)
67
+ end
68
+ parameters
69
+ end
70
+
71
+ def format
72
+ @format ||= :json
73
+ end
74
+
75
+ def api_url
76
+ "https://#{client.subdomain}.fakturownia.pl"
77
+ end
78
+
79
+ def headers
80
+ {accept: :json,
81
+ content_type: :json,
82
+ params: {api_token: client.api_token}
83
+ }
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,14 @@
1
+ module Fakturownia
2
+ class APIException < Exception
3
+ attr_reader :code, :body
4
+
5
+ def initialize(body, code)
6
+ @code = code
7
+ @body = body
8
+ end
9
+
10
+ def inspect
11
+ [code, body].join(" - ")
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Fakturownia
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "fakturownia/version"
2
+ require "fakturownia/errors"
3
+ require "fakturownia/client"
4
+ require "fakturownia/connection"
5
+ require "fakturownia/api/base"
6
+ require "fakturownia/api/invoice"
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fakturownia::Api::Invoice do
4
+ let(:client) { Fakturownia::Client.new(subdomain: "test", api_token: "123") }
5
+ subject { described_class.new(client) }
6
+ let(:connection) { subject.connection }
7
+
8
+ describe "#list" do
9
+ it "should perform GET request on connection at '/invoices'" do
10
+ expect(connection).to receive(:get).with('/invoices', period: 'this_month')
11
+ subject.list(period: 'this_month')
12
+ end
13
+ end
14
+
15
+ describe "#get" do
16
+ it "should perform GET request on connection at '/invoices'" do
17
+ expect(connection).to receive(:get).with('/invoices/123', format: :pdf)
18
+ subject.show(123, format: :pdf)
19
+ end
20
+ end
21
+
22
+ describe "#create" do
23
+ it "should perform POST request on connection at '/invoices'" do
24
+ expect(connection).to receive(:post).with('/invoices',
25
+ invoice: {buyer: 'John Doe'})
26
+ subject.create(buyer: 'John Doe')
27
+ end
28
+ end
29
+
30
+ describe "#list" do
31
+ it "should perform PUT request on connection at '/invoices'" do
32
+ expect(connection).to receive(:put).with('/invoices/123',
33
+ invoice: {buyer: 'John Doe'})
34
+ subject.update(123, buyer: 'John Doe')
35
+ end
36
+ end
37
+
38
+ describe "#list" do
39
+ it "should perform DELETE request on connection at '/invoices'" do
40
+ expect(connection).to receive(:delete).with('/invoices/123')
41
+ subject.delete(123)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fakturownia::Client do
4
+ subject { described_class.new(subdomain: "test", api_token: "123") }
5
+
6
+ it "api_token is required" do
7
+ expect { Fakturownia::Client.new(subdomain: "test") }.
8
+ to raise_error(ArgumentError, 'api_token is required')
9
+ end
10
+
11
+ it "api_token is required" do
12
+ expect { Fakturownia::Client.new(api_token: "test") }.
13
+ to raise_error(ArgumentError, 'subdomain is required')
14
+ end
15
+
16
+ describe "#invoice" do
17
+ it "should return Invoice class" do
18
+ expect(Fakturownia::Api::Invoice).to receive(:new).with(subject)
19
+ subject.invoice
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,104 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fakturownia::Connection do
4
+ let(:client) { Fakturownia::Client.new(subdomain: 'test', api_token: '123') }
5
+ subject { Fakturownia::Connection.new(client) }
6
+
7
+ describe "#request" do
8
+ it "should get request parameters" do
9
+ expect(RestClient::Request).to receive(:execute).with(
10
+ {method: :get,
11
+ url: "https://test.fakturownia.pl/invoices.json",
12
+ headers: {accept: :json, content_type: :json, params: {api_token: '123'}}
13
+ }
14
+ )
15
+ subject.request(:get, "/invoices", {sth: "foo"})
16
+ end
17
+
18
+ it "should pass response to process_response method" do
19
+ response = double(RestClient::Response)
20
+ request = double(RestClient::Request)
21
+ expect(subject).to receive(:process_response).with(response)
22
+ allow(RestClient::Request).to receive(:execute).and_yield(response, request)
23
+ subject.request("/invoices", :get)
24
+ end
25
+ end
26
+
27
+
28
+ describe "#process_response" do
29
+ let(:response) { double(RestClient::Response, code: 200, body: "{}",
30
+ return!: nil) }
31
+ let(:request) { double(RestClient::Request) }
32
+
33
+ before do
34
+ allow(RestClient::Request).to receive(:execute).and_yield(response, request)
35
+ end
36
+
37
+ it "should not follow redirections" do
38
+ expect(response).to receive(:return!)
39
+ subject.get('/invoices')
40
+ end
41
+
42
+ it "should parse JSON" do
43
+ allow(response).to receive(:body).and_return({ok: true}.to_json)
44
+ expect(subject.get("/invoices")).to eq('ok' => true)
45
+ end
46
+
47
+ it "should return body for PDF" do
48
+ allow(subject).to receive(:format).and_return(:pdf)
49
+ allow(response).to receive(:body).and_return("pdf content")
50
+ expect(subject.get("/invoices")).to eq("pdf content")
51
+ end
52
+
53
+ it "should return empty hash if response is not a valid JSON" do
54
+ expect(JSON).to receive(:parse).with("").and_raise(JSON::ParserError)
55
+ allow(response).to receive(:body).and_return("")
56
+ expect(subject.post("/invoices")).to eq({})
57
+ end
58
+
59
+ context "on unsupported response" do
60
+ it "should raise generic APIException" do
61
+ allow(response).to receive(:code).and_return(500)
62
+ allow(response).to receive(:body).and_return("")
63
+ expect { subject.post("/") }.to raise_error { |error|
64
+ expect(error).to be_a(Fakturownia::APIException)
65
+ expect(error.body).to eq("")
66
+ expect(error.code).to eq(500)
67
+ }
68
+ end
69
+ end
70
+ end
71
+
72
+ describe "#get" do
73
+ it "should make GET request to given path" do
74
+ expect(subject).to receive(:request).with(:get, "/invoices", {})
75
+ subject.get("/invoices")
76
+ end
77
+ end
78
+
79
+ describe "#post" do
80
+ it "should make POST request to given path with parameters" do
81
+ expect(subject).to receive(:request).with(:post, "/invoices", name: "pink-one")
82
+ subject.post("/invoices", name: "pink-one")
83
+ end
84
+ end
85
+
86
+ describe "#put" do
87
+ it "should make PUT resquest to given path with parameters" do
88
+ expect(subject).to receive(:request).with(:put, "/invoices", name: "new-one")
89
+ subject.put("/invoices", name: "new-one")
90
+ end
91
+ end
92
+
93
+ describe "#delete" do
94
+ it "should make DELETE request to given path with parameters" do
95
+ expect(subject).to receive(:request).with(:delete, "/invoices", name: "new-one")
96
+ subject.delete("/invoices", name: "new-one")
97
+ end
98
+
99
+ it "should make DELETE request to apps with parameters" do
100
+ expect(subject).to receive(:request).with(:delete, "/invoices/1", {})
101
+ subject.delete("/invoices/1")
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,14 @@
1
+ require 'fakturownia'
2
+ require 'fakeweb'
3
+
4
+ FakeWeb.allow_net_connect = false
5
+
6
+ RSpec.configure do |config|
7
+ config.expect_with :rspec do |expectations|
8
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
9
+ end
10
+
11
+ config.mock_with :rspec do |mocks|
12
+ mocks.verify_partial_doubles = true
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fakturownia_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Shelly Cloud team
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: fakeweb
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '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.1.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.1.0
83
+ description:
84
+ email:
85
+ - devs@shellycloud.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - fakturownia.gemspec
96
+ - lib/fakturownia.rb
97
+ - lib/fakturownia/api/base.rb
98
+ - lib/fakturownia/api/invoice.rb
99
+ - lib/fakturownia/client.rb
100
+ - lib/fakturownia/connection.rb
101
+ - lib/fakturownia/errors.rb
102
+ - lib/fakturownia/version.rb
103
+ - spec/api/invoice_spec.rb
104
+ - spec/client_spec.rb
105
+ - spec/connection_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: https://github.com/shellycloud/fakturownia
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.3
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Ruby API for invoicing service fakturownia.pl
131
+ test_files:
132
+ - spec/api/invoice_spec.rb
133
+ - spec/client_spec.rb
134
+ - spec/connection_spec.rb
135
+ - spec/spec_helper.rb