billogram 0.4.2 → 0.5.0

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: 66bc9d6fc1707cc2615a8aa63e3fdd211e68ca4f
4
- data.tar.gz: 8ad753d1c3b129104bb8c0557e4f92251e264e7d
3
+ metadata.gz: 018140ce51586895b8b6126ac450537e8591d5dd
4
+ data.tar.gz: a96d6e9e69756df541e6120ae4c5624a3ebd46b5
5
5
  SHA512:
6
- metadata.gz: 829e2453b693f4e409f6d5374656e7ead261a505e21ae6a68d3efffe15c145964bb1ba9959aec4b855f9ffcb8b06042da8e341f1e4fa3962b97fd6197f4ebf40
7
- data.tar.gz: 45e1132b2af23d470cdb4c0cc94e125bda47142d6ed4e1a89d66900d8a9a183ce9f0046f60375d6b8d3a17b7eab00a4c8b1cca7bec5eb4d74dc7072e55a45121
6
+ metadata.gz: 33c07c2eb69403500864bab82c19cf2163aae70d03df52a355f0be39bf08117dcd2c38ff32a610702c5dd894e136315fd18410f3c22b4317051108cc2f39d6b2
7
+ data.tar.gz: 490ac7762d7a05fb1b4271eb8bb545d6ccdde426b0494611d43dc6e1b6c39a43ed91e71db9a0913c29f03f0e171d257ce90525eb04ee8a379ce18db8bbd16fdd
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Billogram
2
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/billogram`. 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
3
+ Simple ruby wrapper for [Billogram](https://billogram.com) API
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,32 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ First you need to configure the client
24
+
25
+ ```ruby
26
+ Billogram.configure do |config|
27
+ config.username = "6402-qz0UHqRJ"
28
+ config.password = "55e041056840d54dc8274f70c0a4170f"
29
+ end
30
+ ```
31
+
32
+ Add this code to `config/initializers/billogram.rb` if you're using Rails and don't forget to restart the server.
33
+
34
+ Next step is to actually make API calls. Dead simple.
35
+
36
+ ## Resources
37
+
38
+ We have 7 endpoints each represented by a class: `Invoice`, `Item`, `Customer`, `Event`, `Settings`, `Report`, `Logotype`
39
+
40
+ Most of them support create, fetch, update actions as well as [search](https://billogram.com/api/documentation#object_search_parameters).
41
+
42
+ Also there are nested resources (Address for instance), you cannot get them directly but only through the resource they belong.
43
+
44
+ ```ruby
45
+ Billogram::Address.search() # => undefined method `search' for Billogram::Address:Class
46
+ customer = Billogram::Customer.fetch(32)
47
+ customer.address # => #<Billogram::Address:0x007f9aebafd358>
48
+ ````
26
49
 
27
50
  ## Development
28
51
 
@@ -38,4 +61,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
38
61
  ## License
39
62
 
40
63
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
@@ -23,8 +23,7 @@ Gem::Specification.new do |spec|
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.10"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
- spec.add_development_dependency "rspec", "~> 3.0"
27
- spec.add_development_dependency "rspec-its"
26
+ spec.add_development_dependency "rspec", "~> 3.3"
28
27
  spec.add_development_dependency "webmock", "~> 1.21"
29
28
  spec.add_development_dependency "dotenv", "~> 2.0"
30
29
  spec.add_dependency "httparty", "~> 0.13"
@@ -31,7 +31,10 @@ require "billogram/version"
31
31
 
32
32
  module Billogram
33
33
  class << self
34
- attr_accessor :username, :password, :base_uri
34
+ BASE_URI = "https://billogram.com/api/v2/"
35
+
36
+ attr_accessor :username, :password
37
+ attr_writer :base_uri
35
38
 
36
39
  def client
37
40
  @client ||= Client.new(username, password, base_uri)
@@ -40,5 +43,9 @@ module Billogram
40
43
  def configure
41
44
  yield self if block_given?
42
45
  end
46
+
47
+ def base_uri
48
+ @base_uri ||= BASE_URI
49
+ end
43
50
  end
44
51
  end
@@ -1,32 +1,15 @@
1
1
  module Billogram
2
2
  class Client
3
3
  include HTTParty
4
- format :json
5
4
 
6
- def initialize(username, password, base_uri)
7
- self.class.default_options.merge!(base_uri: base_uri, basic_auth: {username: username, password: password})
8
- end
5
+ extend Forwardable
9
6
 
10
- def get(*args)
11
- handle_request(:get, *args)
12
- end
13
-
14
- def post(*args)
15
- handle_request(:post, *args)
16
- end
7
+ delegate [:get, :post, :put, :delete] => self
17
8
 
18
- def put(*args)
19
- handle_request(:put, *args)
20
- end
21
-
22
- def delete(*args)
23
- handle_request(:delete, *args)
24
- end
9
+ format :json
25
10
 
26
- def handle_request(method, *args)
27
- response = self.class.send(method, *args)
28
- return response.parsed_response["data"] if response.success?
29
- raise Billogram::Error.from_response(response)
11
+ def initialize(username, password, base_uri)
12
+ self.class.default_options.merge!(base_uri: base_uri, basic_auth: {username: username, password: password})
30
13
  end
31
14
  end
32
15
  end
@@ -10,10 +10,6 @@ module Billogram
10
10
  self.class.perform_request(:put, "#{endpoint}/#{id}", attributes)
11
11
  end
12
12
 
13
- def delete
14
- self.class.perform_request(:delete, "#{endpoint}/#{id}")
15
- end
16
-
17
13
  def endpoint
18
14
  self.class.endpoint
19
15
  end
@@ -7,16 +7,21 @@ module Billogram
7
7
  class NotFound < Error; end
8
8
  class InternalServerError < Error; end
9
9
 
10
- def self.from_response(response)
11
- if klass = case response.code.to_i
12
- when 400 then BadRequest
13
- when 401 then Unauthorized
14
- when 403 then Forbidden
15
- when 404 then NotFound
16
- when 500 then InternalServerError
17
- else Billogram::Error
18
- end
19
- klass.new(response["data"]["message"])
10
+ class << self
11
+ def from_response(response)
12
+ message = response["data"]["message"]
13
+ error_class(response).new(message)
14
+ end
15
+
16
+ def error_class(response)
17
+ case response.code.to_i
18
+ when 400 then BadRequest
19
+ when 401 then Unauthorized
20
+ when 403 then Forbidden
21
+ when 404 then NotFound
22
+ when 500 then InternalServerError
23
+ else Billogram::Error
24
+ end
20
25
  end
21
26
  end
22
27
  end
@@ -20,7 +20,15 @@ module Billogram
20
20
  end
21
21
 
22
22
  def execute
23
- Billogram.client.send(type, url, content)
23
+ response.success? ? response["data"] : raise_from(response)
24
+ end
25
+
26
+ def raise_from(response)
27
+ raise Billogram::Error.from_response(response)
28
+ end
29
+
30
+ def response
31
+ @response ||= Billogram.client.send(type, url, content)
24
32
  end
25
33
  end
26
34
  end
@@ -30,10 +30,23 @@ module Billogram
30
30
  end
31
31
 
32
32
  def to_json(*args)
33
+ to_hash.to_json(*args)
34
+ end
35
+
36
+ def to_hash
33
37
  instance_variables
34
- .map{|var| ["#{var}"[1..-1], instance_variable_get(var)]}
35
- .to_h
36
- .to_json(*args)
38
+ .each_with_object({}) do |variable, obj|
39
+ value = instance_variable_get(variable)
40
+
41
+ case value
42
+ when Resource
43
+ value = value.to_hash
44
+ when Array
45
+ value = value.map(&:to_hash)
46
+ end
47
+
48
+ obj[variable[1..-1]] = value
49
+ end
37
50
  end
38
51
  end
39
52
  end
@@ -19,6 +19,7 @@ module Billogram
19
19
 
20
20
  relation :items, :many
21
21
  relation :events, :many
22
+ relation :automatic_reminders_settings, :many, class_override: 'AutomaticReminder'
22
23
 
23
24
  COMMANDS = [ :sell, :remind, :collect, :writeoff, :resend, :remind, :payment, :credit, :message, :attach ]
24
25
 
@@ -9,5 +9,9 @@ module Billogram
9
9
 
10
10
  relation :bookkeeping, :one
11
11
  relation :regional_sweden, :one
12
+
13
+ def delete
14
+ self.class.perform_request(:delete, "#{endpoint}/#{id}")
15
+ end
12
16
  end
13
17
  end
@@ -4,11 +4,11 @@ module Billogram
4
4
 
5
5
  attr_accessor :name, :org_no
6
6
 
7
- relation :contact, :one
8
- relation :address, :one
9
- relation :payment, :one
10
- relation :tax, :one
11
- relation :bookkeeping, :one
12
- relation :invoices, :one, class_override: "InvoiceDefaults"
7
+ relation :tax, :one
8
+ relation :contact, :one
9
+ relation :address, :one
10
+ relation :payment, :one
11
+ relation :invoices, :one, class_override: "InvoiceDefaults"
12
+ relation :bookkeeping, :one
13
13
  end
14
14
  end
@@ -1,3 +1,3 @@
1
1
  module Billogram
2
- VERSION = "0.4.2"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: billogram
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Birman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-07 00:00:00.000000000 Z
11
+ date: 2015-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,28 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '3.3'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.0'
55
- - !ruby/object:Gem::Dependency
56
- name: rspec-its
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'
54
+ version: '3.3'
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: webmock
71
57
  requirement: !ruby/object:Gem::Requirement