pe_accounting 0.1.0 → 0.2.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: cde00eb1f7da6b0b3273b8b44988b292a68e96ff
4
- data.tar.gz: 6d63ce0048c516e31b6f2faa0040c0b07f5ba215
3
+ metadata.gz: 9e00ed4390664edd42dc6632f828685e513e9aa3
4
+ data.tar.gz: 5f2178a5bf64abe498c1c3f4e672c7fa5404f156
5
5
  SHA512:
6
- metadata.gz: afd51489f1f8dc8b6fee09036985af1412faa0acb205c47e24b3483e3d27a01263d6080582a1eeba215cbe743f1f61eab7507b5f248b10f594c4619f7a3e5ac6
7
- data.tar.gz: b7554759568b140c0ff0c142d6a9ba5a1d0278b1d91aa38ad74aeaee7206a9e87443d3b9769b5c5a005e3a188b4c43e81ea89de5f6d3a186a04d0832c0c19758
6
+ metadata.gz: 4644a4ac41f9f839b59e55624305275b7f53e7b418df4effa836faa536e34aaaf7d44c0e40bbb2390bd05d02756473f40b7b1d589a351320901eb45a30724f33
7
+ data.tar.gz: b8a94ea6b35f72d51c2768ffa5cc45bbb76988952c0dc87f8046358cb1692c967fafc06d874d0e1d7cca6e66d6d56085917b2f55a09d777f88d009ab1918383d
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
- # PeAccounting::Client
1
+ # PE Accounting Ruby Client
2
+
3
+ [![Gem](https://img.shields.io/gem/v/pe_accounting.svg?style=flat-square)](https://rubygems.org/gems/pe_accounting)
2
4
 
3
5
  A simple low-level wrapper for PE Accounting's public API.
6
+ It's publicly available at https://my.accounting.pe/api/v1/doc
7
+
4
8
 
5
9
 
6
10
  ## Installation
@@ -21,7 +25,31 @@ Or install it yourself as:
21
25
 
22
26
  ## Usage
23
27
 
24
- TODO: Write usage instructions here
28
+ ```ruby
29
+ require 'pe_accounting'
30
+
31
+ # Initialize the API
32
+ api = PeAccounting::Client.new('your-api-token')
33
+
34
+ # Fetch a list of all the given company's clients. Returns a ruby Array or Hash, depending on the ressource's specifications
35
+ clients = api.get(company_id: 123, request: 'client')
36
+
37
+ puts clients
38
+
39
+ ##
40
+ # [
41
+ # {"id"=>12345, "foreign-id"=>"", "name"=>"fdsmfkls", "contact"=>"fslmdkfsdmlkf", "address"=>{"address1"=>"sdfmsdlmfksdm", "address2"=>"", "zip-code"=>"12345", "state"=>"msdlfkdsmlk", "country"=>"sdflkdslkfj"}, "email"=>"sdflkjsfs@fdsd.fr", "country-code"=>"FR", "accountnr"=>0, "payment-days"=>14, "orgno"=>"123456-1234", "phone"=>"+33123456789", "user"=>{"id"=>12345}, "delivery-type"=>"Email", "vat-nr"=>"", "template"=>{"id"=>1234}, "active"=>true},
42
+ # {"id"=>9876, "foreign-id"=>"", "name"=>"fdsmfkls", "contact"=>"fslmdkfsdmlkf", "address"=>{"address1"=>"sdfmsdlmfksdm", "address2"=>"", "zip-code"=>"12345", "state"=>"msdlfkdsmlk", "country"=>"sdflkdslkfj"}, "email"=>"sdflkjsfs@fdsd.fr", "country-code"=>"FR", "accountnr"=>0, "payment-days"=>14, "orgno"=>"123456-1235", "phone"=>"+33123456789", "user"=>{"id"=>9875}, "delivery-type"=>"Email", "vat-nr"=>"", "template"=>{"id"=>1234}, "active"=>true}
43
+ # ]
44
+
45
+ john = clients.first
46
+ john["name"] = "John Doe"
47
+
48
+ ## Updates a client with the new name
49
+ api.post(company_id: 123, request: "client/#{john["id"]}", payload: john)
50
+
51
+ ```
52
+ Accepted methods: `get`, `put`, `post`, `delete`. Only `put` and `post` accept a payload.
25
53
 
26
54
  ## Development
27
55
 
@@ -1,15 +1,30 @@
1
1
  require 'rest-client'
2
2
  require 'multi_json'
3
+ require 'gyoku'
4
+ require 'nori'
3
5
 
4
6
  module PeAccounting
5
7
  class PeAccouningError < StandardError; end
6
8
 
7
9
  class Client
8
- def initialize(token, endpoint = 'https://my.accounting.pe/api/v1/')
10
+
11
+
12
+ # Initializes the API Client
13
+ #
14
+ # @param [String] token - API token
15
+ # @param [Symbol] format - :xml or :json - :xml by default
16
+ # @param [Type] endpoint = 'https://my.accounting.pe/api/v1/'. This shouldn't change
17
+ # @return [PeAccounting::Client] A PeAccounting::Client object
18
+ def initialize(token, format = :xml, endpoint = 'https://my.accounting.pe/api/v1/')
19
+ unless [:xml,:json].include?(format)
20
+ raise PeAccouningError, 'You must specify a Company ID and a request'
21
+ end
22
+ @format = format
9
23
  @token = token
10
24
  @endpoint = endpoint
11
25
  end
12
26
 
27
+
13
28
  def get(kwargs = {})
14
29
  unless kwargs[:company_id] && kwargs[:request]
15
30
  raise PeAccouningError, 'You must specify a Company ID and a request'
@@ -48,13 +63,32 @@ module PeAccounting
48
63
  "#{@endpoint}company/#{company_id}/#{path}"
49
64
  end
50
65
 
66
+ def generate_payload(payload)
67
+ if payload
68
+ if @format == xml
69
+ Gyoku.xml(payload)
70
+ else
71
+ MultiJson.dump(payload)
72
+ end
73
+ end
74
+ end
75
+
76
+ def handle_body(body)
77
+ if @format == :xml
78
+ parser = Nori.new
79
+ parser.parse(body)
80
+ else
81
+ hash = MultiJson.load(body)
82
+ hash.length == 1 ? hash.values.first : hash
83
+ end
84
+ end
85
+
51
86
  def request(method, url, payload = nil)
52
87
  res = RestClient::Request.execute(method: method, url: url,
53
- payload: MultiJson.dump(payload),
54
- headers: { content_type: :json,
88
+ payload: generate_payload(payload),
89
+ headers: { content_type: @format,
55
90
  x_token: @token })
56
- hash = MultiJson.load(res.body)
57
- hash.length == 1 ? hash.values.first : hash
91
+ handle_body(res.body)
58
92
  end
59
93
 
60
94
  def file_to_json(file)
@@ -1,3 +1,3 @@
1
1
  module PeAccounting
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
@@ -26,4 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'rake', '~> 10.0'
27
27
  spec.add_runtime_dependency 'multi_json', '~> 1.3', '>= 1.3.0'
28
28
  spec.add_runtime_dependency 'rest-client', '~> 2.0'
29
+ spec.add_runtime_dependency 'gyoku', '~> 1.0'
30
+ spec.add_runtime_dependency 'nori', '~> 2.0'
31
+ spec.add_runtime_dependency 'nokogiri', '>= 1.4.0'
29
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pe_accounting
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mehdi Rejraji
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-31 00:00:00.000000000 Z
11
+ date: 2017-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,6 +72,48 @@ dependencies:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
74
  version: '2.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: gyoku
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.0'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '1.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: nori
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '2.0'
96
+ type: :runtime
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '2.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: nokogiri
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 1.4.0
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 1.4.0
75
117
  description:
76
118
  email: mehdi.rejraji@gmail.com
77
119
  executables: []