pe_accounting 0.2.0 → 0.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9e00ed4390664edd42dc6632f828685e513e9aa3
4
- data.tar.gz: 5f2178a5bf64abe498c1c3f4e672c7fa5404f156
3
+ metadata.gz: 846d79de6e9ca386cd4a9b42099687a158865524
4
+ data.tar.gz: 7c78c8124ffdf7c3964538dacb6fe76d1c60a4c9
5
5
  SHA512:
6
- metadata.gz: 4644a4ac41f9f839b59e55624305275b7f53e7b418df4effa836faa536e34aaaf7d44c0e40bbb2390bd05d02756473f40b7b1d589a351320901eb45a30724f33
7
- data.tar.gz: b8a94ea6b35f72d51c2768ffa5cc45bbb76988952c0dc87f8046358cb1692c967fafc06d874d0e1d7cca6e66d6d56085917b2f55a09d777f88d009ab1918383d
6
+ metadata.gz: 3d2ad909081085b647d2f8b75bb0dd10359cd14efa900ede39c6a6d4b286616e80e3ff9ee0fa34a998410c6370b48c8aba524b66f8229323cc2fb473703abd51
7
+ data.tar.gz: aed1983fa8f54c2f19189e8dbafca3641246a4c81671cfb539ae84138003dbe659508ba33c698d59e2cf3821758541809a26e95f96d6988fc8fba5443f5d5ad2
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  A simple low-level wrapper for PE Accounting's public API.
6
6
  It's publicly available at https://my.accounting.pe/api/v1/doc
7
-
7
+
8
8
 
9
9
 
10
10
  ## Installation
@@ -28,25 +28,19 @@ Or install it yourself as:
28
28
  ```ruby
29
29
  require 'pe_accounting'
30
30
 
31
- # Initialize the API
32
- api = PeAccounting::Client.new('your-api-token')
31
+ # Initialize the API, the 3rd parameter is one of (:json, :xml) and sets how the wrapper will interact with the API.
32
+ >> api = PeAccounting::Client.new('your-api-token', 123)
33
+ => <PeAccounting::Client:0x007fd6b7ac74d0 @company_id=123, @format=:json, @token="your-api-token", @endpoint="https://my.accounting.pe/api/v1/">
33
34
 
34
35
  # 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
- # ]
36
+ >> clients = api.get('client')
37
+ => [ {"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}, {"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} ]
44
38
 
45
39
  john = clients.first
46
40
  john["name"] = "John Doe"
47
41
 
48
42
  ## Updates a client with the new name
49
- api.post(company_id: 123, request: "client/#{john["id"]}", payload: john)
43
+ api.post("client/#{john['id']}", john)
50
44
 
51
45
  ```
52
46
  Accepted methods: `get`, `put`, `post`, `delete`. Only `put` and `post` accept a payload.
@@ -1,98 +1,152 @@
1
- require 'rest-client'
2
1
  require 'multi_json'
3
2
  require 'gyoku'
4
3
  require 'nori'
4
+ require 'uri'
5
+ require 'net/http'
5
6
 
6
7
  module PeAccounting
7
8
  class PeAccouningError < StandardError; end
8
9
 
9
10
  class Client
10
11
 
11
-
12
- # Initializes the API Client
12
+ # Initializes an API Client
13
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
14
+ # @param token [String] The API token
15
+ # @param company_id [Integer] The company ID
16
+ # @param format [Symbol] The format by which the wrapper will interact with the API.
17
+ # `:xml` or `:json - leave empty for :json by default
18
+ # @param endpoint [String] 'https://my.accounting.pe/api/v1/' by default. This shouldn't change.
17
19
  # @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
20
+ #
21
+ # @example
22
+ # api = PeAccounting::Client.new("your-api-token", 123)
23
+ def initialize(token, company_id, format = :json, endpoint = 'https://my.accounting.pe/api/v1/')
24
+ @company_id = company_id
22
25
  @format = format
23
26
  @token = token
24
27
  @endpoint = endpoint
25
28
  end
26
29
 
27
30
 
28
- def get(kwargs = {})
29
- unless kwargs[:company_id] && kwargs[:request]
30
- raise PeAccouningError, 'You must specify a Company ID and a request'
31
+ # Performs a GET request
32
+ #
33
+ # @param request [String] The endpoint to perform the request to, e.g. `client`
34
+ # @return [Hash, Array] A hash or an array, depending on the specs
35
+ #
36
+ # @example
37
+ # api.get('client')
38
+ def get(request)
39
+ unless request
40
+ raise PeAccouningError, 'You must specify a request'
31
41
  end
32
42
 
33
- request(:get, url(kwargs[:company_id], kwargs[:request]))
43
+ request(:get, uri(request))
34
44
  end
35
45
 
36
- def put(kwargs = {})
37
- unless kwargs[:company_id] && kwargs[:request] && kwargs[:payload]
38
- raise PeAccouningError, 'You must specify a company_id, a request, and a payload'
46
+ # Performs a PUT request
47
+ #
48
+ # @param request [String] opts = {} The options to pass to the method
49
+ # @param payload [Hash] A native Ruby hash describing the data to send
50
+ # @param root [String] The enclosing root of the object (useful for xml)
51
+ # @return [Hash] A hash containing the result of the request
52
+ #
53
+ # @example
54
+ # api.put('client', {name: "John Doe"})
55
+ def put(request, payload = {}, root = nil)
56
+ unless request
57
+ raise PeAccouningError, 'You must specify a request'
39
58
  end
40
- request(:put, url(kwargs[:company_id], kwargs[:request]),
41
- kwargs[:payload])
59
+ payload = (root ? {root => payload} : payload)
60
+ request(:put, uri(request), payload)
42
61
  end
43
62
 
44
- def post(kwargs = {})
45
- unless kwargs[:company_id] && kwargs[:request] && kwargs[:payload]
46
- raise PeAccouningError, 'You must specify a company_id, a request, and a payload'
63
+ # Performs a POST request
64
+ #
65
+ # (see #put)
66
+ #
67
+ # @example
68
+ # api.post('client/123', {name: "John Doe"})
69
+ def post(request, payload = {}, root = nil)
70
+ unless request
71
+ raise PeAccouningError, 'You must specify a request'
47
72
  end
48
-
49
- request(:post, url(kwargs[:company_id], kwargs[:request]),
50
- kwargs[:payload])
73
+ payload = (root ? {root => payload} : payload)
74
+ request(:post, uri(request), payload)
51
75
  end
52
76
 
53
- def delete(kwargs = {})
54
- unless kwargs[:company_id] && kwargs[:request]
55
- raise PeAccouningError, 'You must specify a Company ID and a request'
77
+ # Performs a DELETE request
78
+ #
79
+ # (see #get)
80
+ #
81
+ # @example
82
+ # api.delete('client/123')
83
+ def delete(request)
84
+ unless request
85
+ raise PeAccouningError, 'You must specify a request'
56
86
  end
57
- request(:delete, url(kwargs[:company_id], kwargs[:request]))
87
+ request(:delete, uri(request))
58
88
  end
59
89
 
60
90
  private
61
91
 
62
- def url(company_id, path)
63
- "#{@endpoint}company/#{company_id}/#{path}"
92
+ def uri(path)
93
+ URI.parse("#{@endpoint}company/#{@company_id}/#{path}")
94
+ end
95
+
96
+ def denilize(h)
97
+ h.each_with_object({}) { |(k,v),g|
98
+ g[k] = (Hash === v) ? denilize(v) : v ? v : '' }
64
99
  end
65
100
 
66
101
  def generate_payload(payload)
67
- if payload
68
- if @format == xml
69
- Gyoku.xml(payload)
70
- else
71
- MultiJson.dump(payload)
72
- end
102
+ if @format == :xml
103
+ Gyoku.xml(payload)
104
+ else
105
+ MultiJson.dump(payload)
73
106
  end
74
107
  end
75
108
 
76
109
  def handle_body(body)
77
110
  if @format == :xml
78
- parser = Nori.new
79
- parser.parse(body)
111
+ parser = Nori.new(convert_dashes_to_underscores: false,
112
+ empty_tag_value: "")
113
+ hash = parser.parse(body)
80
114
  else
81
115
  hash = MultiJson.load(body)
82
- hash.length == 1 ? hash.values.first : hash
83
116
  end
117
+
118
+ while hash.is_a?(Hash) && hash.length == 1
119
+ hash = hash.values.first
120
+ end
121
+ hash
122
+
84
123
  end
85
124
 
86
- def request(method, url, payload = nil)
87
- res = RestClient::Request.execute(method: method, url: url,
88
- payload: generate_payload(payload),
89
- headers: { content_type: @format,
90
- x_token: @token })
91
- handle_body(res.body)
125
+ def request(method, uri, payload = nil)
126
+ req = Net::HTTP.const_get(method.capitalize, false).new(uri)
127
+ req.content_type = "application/#{@format.downcase}"
128
+ req['X-Token'] = @token
129
+ req.body = generate_payload(payload) if payload
130
+
131
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
132
+ http.request(req)
133
+ end
134
+
135
+ case res
136
+ when Net::HTTPSuccess
137
+ return handle_body(res.body)
138
+ else
139
+ raise PeAccouningError, (res.read_body ? handle_body(res.body) : res)
140
+ end
92
141
  end
93
142
 
94
- def file_to_json(file)
95
- file.read.each_byte.map { |b| b.to_s(16) }.to_json
143
+ # Converts a File to its hexadecimal representation
144
+ #
145
+ # @param f [File] A binary file.
146
+ # @return [String] A string representing the file in hexadecimal form.
147
+ def file_to_hex(f)
148
+ f.read.unpack('H*').first
96
149
  end
150
+
97
151
  end
98
152
  end
@@ -1,3 +1,3 @@
1
1
  module PeAccounting
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
@@ -22,10 +22,11 @@ Gem::Specification.new do |spec|
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  spec.require_paths = ['lib']
24
24
 
25
+ spec.has_rdoc = 'yard'
26
+
25
27
  spec.add_development_dependency 'bundler', '~> 1.14'
26
28
  spec.add_development_dependency 'rake', '~> 10.0'
27
29
  spec.add_runtime_dependency 'multi_json', '~> 1.3', '>= 1.3.0'
28
- spec.add_runtime_dependency 'rest-client', '~> 2.0'
29
30
  spec.add_runtime_dependency 'gyoku', '~> 1.0'
30
31
  spec.add_runtime_dependency 'nori', '~> 2.0'
31
32
  spec.add_runtime_dependency 'nokogiri', '>= 1.4.0'
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.2.0
4
+ version: 0.3.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-02-01 00:00:00.000000000 Z
11
+ date: 2017-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,20 +58,6 @@ dependencies:
58
58
  - - ">="
59
59
  - !ruby/object:Gem::Version
60
60
  version: 1.3.0
61
- - !ruby/object:Gem::Dependency
62
- name: rest-client
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: '2.0'
68
- type: :runtime
69
- prerelease: false
70
- version_requirements: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - "~>"
73
- - !ruby/object:Gem::Version
74
- version: '2.0'
75
61
  - !ruby/object:Gem::Dependency
76
62
  name: gyoku
77
63
  requirement: !ruby/object:Gem::Requirement