pina 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: 6ccfeab00b3b1f193fa241300d0af125cc09aa16
4
- data.tar.gz: 6181a34c205d9b50ec4a1f9ac6eed5c3bfa16b87
3
+ metadata.gz: fb1e813180ba4dfd749dcfb7cb17a029feec42e2
4
+ data.tar.gz: 59743e276d44b3ac8a5f64157526cc76654bc0ce
5
5
  SHA512:
6
- metadata.gz: 5d3c0a4d62e3116ed2f373d3aff5f2c833a3a725350bb96947fce5ce84ec4c104dae144abd27994812c8f36fd5978326d43566360f52f8638f8924184cf9ac32
7
- data.tar.gz: 66a35da4497a14416284ae333dcc10bd1823e6b442e0eeb1ffd7db9325b6d78471eb2e025e46b2b8bc979aed444db544af1abc34822bd33b799d828b07a8d501
6
+ metadata.gz: be6635f0d55fdd227f1cb80984825f57e2729f1b16540ad615566e7f6e976b46db274acec07a637514e543be6e405b290f08db47ea5b3dae77dcc333a4581b09
7
+ data.tar.gz: 8a3c1acb1e814483bcb77ddaf747935229753a9c99d3197a7bbc99c79c30633079c40c0efe18f7d1156aef1bc37e5fc3c9becee3633988ba1433994535b8cfc1
data/.rubocop.yml CHANGED
@@ -1,2 +1,6 @@
1
1
  Documentation:
2
2
  Enabled: false
3
+
4
+ Metrics/ClassLength:
5
+ Max: 130
6
+
data/.travis.yml CHANGED
@@ -1,7 +1,9 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.3
3
+ - 2.3.0
4
4
  - 2.2.3
5
5
  - 2.2
6
6
  - 2.1
7
+ env:
8
+ - secure: TENANT=test
7
9
  before_install: gem install bundler -v 1.10.6
data/README.md CHANGED
@@ -80,13 +80,63 @@ contact = Pina::Contact.find('existing')
80
80
  contact.email = 'brand_new@email.com'
81
81
  Pina::Contact.update('existing', contact)
82
82
  ```
83
+ ### All sales invoices
83
84
 
85
+ ```ruby
86
+ Pina::SalesInvoice.all
87
+ ```
88
+
89
+ Gets all sales invocies from your database. Results are paginated and you can access
90
+ first, next or previous page like this:
91
+
92
+ ```ruby
93
+ invoices = Pina::SalesInvoice.all
94
+ invoices.next_page
95
+ ```
96
+
97
+ ```ruby
98
+ invoices = Pina::SalesInvoice.all
99
+ invoices.previous_page
100
+
101
+ invoices.first_page
102
+ ```
103
+
104
+ ### Fetching specific sales invoice
105
+
106
+ ```ruby
107
+ Pina::SalesInvoice.find(invoice_id)
108
+ ```
109
+
110
+ ### Create new sales invoice
111
+
112
+ ```ruby
113
+ invoice = Pina::Models::SalesInvoice.new
114
+ Pina::SalesInvoice.create(invoice)
115
+ ```
116
+
117
+ ### Update existing sales invoice
118
+
119
+ ```ruby
120
+ invoice = Pina::SalesInvoice.find(2016000001)
121
+ invoice.status = :confirmed
122
+ Pina::SalesInvoice.update(2016000001, invoice)
123
+ ```
84
124
  ## Development
85
125
 
86
126
  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.
87
127
 
88
128
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
89
129
 
130
+ ## Testing
131
+
132
+ Create .env file with following variables in it:
133
+ ```
134
+ EMAIL
135
+ TENANT
136
+ API_TOKEN
137
+ ```
138
+ and fill them with appropriate values.
139
+
90
140
  ## Contributing
91
141
 
92
142
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pina.
data/lib/pina.rb CHANGED
@@ -5,12 +5,18 @@ require 'typhoeus'
5
5
  require 'virtus'
6
6
 
7
7
  require 'pina/contact'
8
+ require 'pina/sales_invoice'
8
9
  require 'pina/version'
9
10
  require 'pina/rest_adapter'
10
11
 
12
+ require 'pina/utils/pagination'
13
+
11
14
  require 'pina/models/address'
12
15
  require 'pina/models/contact'
13
16
  require 'pina/models/contact_list'
17
+ require 'pina/models/sales_item'
18
+ require 'pina/models/sales_invoice'
19
+ require 'pina/models/sales_invoice_list'
14
20
 
15
21
  module Pina
16
22
  class ConfigurationNotSet < StandardError; end
@@ -27,6 +33,7 @@ module Pina
27
33
 
28
34
  def configure
29
35
  self.configuration ||= Configuration.new
36
+ self.configuration.set_defaults
30
37
  yield(configuration)
31
38
  end
32
39
  end
@@ -36,13 +43,22 @@ module Pina
36
43
  attr_reader :api_version
37
44
 
38
45
  def initialize
46
+ set_defaults
47
+ end
48
+
49
+ def set_defaults
39
50
  @api_version = DEFAULT_API_VERSION
40
51
  @email = DEFAULT_EMAIL
41
52
  @tenant = DEFAULT_TENANT
53
+ @base_url = nil
54
+ end
55
+
56
+ def base_url=(base_url)
57
+ @base_url = base_url
42
58
  end
43
59
 
44
60
  def base_url
45
- SCHEME + tenant + API_PATH + "#{api_version}/"
61
+ @base_url ||= SCHEME + tenant + API_PATH + "#{api_version}/"
46
62
  end
47
63
  end
48
64
  end
data/lib/pina/contact.rb CHANGED
@@ -5,6 +5,23 @@ module Pina
5
5
  Pina::Models::Contact.new(params)
6
6
  end
7
7
 
8
+ def find_by(hash)
9
+ response = where(hash)
10
+
11
+ return response.items.first if response.is_a? Pina::Models::ContactList
12
+
13
+ response
14
+ end
15
+
16
+ def where(hash, page = nil)
17
+ response = Pina::RestAdapter.get(:contacts, hash)
18
+
19
+ return Pina::Models::ContactList.new(attributes(response)) if
20
+ response.ok?
21
+
22
+ response
23
+ end
24
+
8
25
  def find(id)
9
26
  response = Pina::RestAdapter.get(:contacts, id)
10
27
 
@@ -9,12 +9,14 @@ module Pina
9
9
  attribute :city
10
10
  attribute :country_id
11
11
  attribute :created_at
12
- attribute :creator, Hash
12
+ attribute :creator
13
+ attribute :creator_id
13
14
  attribute :email
14
15
  attribute :external_id
15
16
  attribute :hidden
16
17
  attribute :mobile_phone
17
- attribute :modifier, Hash
18
+ attribute :modifier
19
+ attribute :modifier_id
18
20
  attribute :name
19
21
  attribute :phone
20
22
  attribute :postal_code
@@ -24,6 +26,20 @@ module Pina
24
26
  attribute :updated_at
25
27
  attribute :url
26
28
  attribute '_destroy'
29
+
30
+ def creator=(value)
31
+ return unless value
32
+
33
+ self.creator_id = value.values[0]
34
+ super
35
+ end
36
+
37
+ def modifier=(value)
38
+ return unless value
39
+
40
+ self.modifier_id = value.values[0]
41
+ super
42
+ end
27
43
  end
28
44
  end
29
45
  end
@@ -6,27 +6,28 @@ module Pina
6
6
  attribute :acquisition_country_id
7
7
  attribute :addresses, Array[Address]
8
8
  attribute :business_entity
9
+ attribute :company_number
9
10
  attribute :consumption_country_id
10
11
  attribute :contact_id
11
12
  attribute :contract_id
12
13
  attribute :country_id
13
14
  attribute :created_at
14
- attribute :creator, Hash
15
+ attribute :creator
16
+ attribute :creator_id
15
17
  attribute :days_till_due_date
16
18
  attribute :department_id
17
19
  attribute :duplicate_variable_symbol
18
- attribute :email
19
20
  attribute :external_id
20
21
  attribute :hidden
21
22
  attribute :import_received_documents
22
23
  attribute :language_id
23
- attribute :mobile_phone
24
+ attribute :modifier
24
25
  attribute :modifier_id
25
26
  attribute :name
26
27
  attribute :note
27
28
  attribute :percent_discount
28
- attribute :phone
29
- attribute :price_list, Hash
29
+ attribute :price_list
30
+ attribute :price_list_id
30
31
  attribute :response
31
32
  attribute :status
32
33
  attribute :sipo_number
@@ -34,9 +35,33 @@ module Pina
34
35
  attribute :tin
35
36
  attribute :unreliable_payer
36
37
  attribute :updated_at
37
- attribute :url
38
38
  attribute :vat_payer
39
39
  attribute :vatin
40
+
41
+ def self.to_s
42
+ 'contact'
43
+ end
44
+
45
+ def creator=(value)
46
+ return unless value
47
+
48
+ self.creator_id = value.values[0]
49
+ super
50
+ end
51
+
52
+ def modifier=(value)
53
+ return unless value
54
+
55
+ self.modifier_id = value.values[0]
56
+ super
57
+ end
58
+
59
+ def price_list=(value)
60
+ return unless value
61
+
62
+ self.price_list_id = value.values[0]
63
+ super
64
+ end
40
65
  end
41
66
  end
42
67
  end
@@ -0,0 +1,159 @@
1
+ module Pina
2
+ module Models
3
+ class SalesInvoice
4
+ include Virtus.model
5
+
6
+ attribute :accounting_address
7
+ attribute :accounting_address_id
8
+ attribute :bank_account
9
+ attribute :bank_account_id
10
+ attribute :buyer_address
11
+ attribute :buyer_address_id
12
+ attribute :buyer
13
+ attribute :buyer_id
14
+ attribute :contract
15
+ attribute :contract_id
16
+ attribute :creator
17
+ attribute :creator_id
18
+ attribute :modifier
19
+ attribute :modifier_id
20
+ attribute :delivery_address
21
+ attribute :delivery_address_id
22
+ attribute :department
23
+ attribute :department_id
24
+ attribute :document_recipient_address
25
+ attribute :document_recipient_address_id
26
+ attribute :order
27
+ attribute :order_id
28
+ attribute :shipper
29
+ attribute :shipper_id
30
+ attribute :linked_doc
31
+ attribute :linked_doc_number
32
+ attribute :items, Array[SalesItem]
33
+
34
+ attribute :cancellation_date
35
+ attribute :constant_symbol
36
+ attribute :consumption_country_id
37
+ attribute :created_at
38
+ attribute :delivery_note
39
+ attribute :delivery_note_note
40
+ attribute :delivery_note_text
41
+ attribute :due_date
42
+ attribute :exchange_rate
43
+ attribute :external_id
44
+ attribute :invoice_id
45
+ attribute :issue_date
46
+ attribute :logo
47
+ attribute :note
48
+ attribute :payment_method
49
+ attribute :percent_discount
50
+ attribute :public_id
51
+ attribute :response
52
+ attribute :rounding
53
+ attribute :send_by_email
54
+ attribute :send_by_mail
55
+ attribute :shipping_method
56
+ attribute :specific_symbol
57
+ attribute :status
58
+ attribute :tax_payment_date
59
+ attribute :text
60
+ attribute :total_amount
61
+ attribute :type
62
+ attribute :type
63
+ attribute :updated_at
64
+ attribute :variable_symbol
65
+ attribute :vat1_amount
66
+ attribute :vat2_amount
67
+ attribute :vat3_amount
68
+ attribute :vat_document
69
+
70
+ def self.to_s
71
+ 'sales_invoice'
72
+ end
73
+
74
+ def accounting_address=(value)
75
+ return unless value
76
+
77
+ self.accounting_address_id = value.values[0]
78
+ super
79
+ end
80
+
81
+ def bank_account=(value)
82
+ return unless value
83
+
84
+ self.bank_account_id = value.values[0]
85
+ super
86
+ end
87
+
88
+ def buyer_address=(value)
89
+ return unless value
90
+
91
+ self.buyer_address_id = value.values[0]
92
+ super
93
+ end
94
+
95
+ def buyer=(value)
96
+ return unless value
97
+
98
+ self.buyer_id = value['_meta']['href'].split('/').last
99
+ super
100
+ end
101
+
102
+ def contract=(value)
103
+ return unless value
104
+
105
+ self.contract_id = value.values[0]
106
+ super
107
+ end
108
+
109
+ def delivery_address=(value)
110
+ return unless value
111
+
112
+ self.delivery_address_id = value.values[0]
113
+ super
114
+ end
115
+
116
+ def department=(value)
117
+ return unless value
118
+
119
+ self.department_id = value.values[0]
120
+ super
121
+ end
122
+
123
+ def document_recipient_address=(value)
124
+ return unless value
125
+
126
+ self.document_recipient_address_id = value.values[0]
127
+ super
128
+ end
129
+
130
+ def order=(value)
131
+ return unless value
132
+
133
+ self.order_id = value.values[0]
134
+ super
135
+ end
136
+
137
+ def linked_doc=(value)
138
+ return unless value
139
+
140
+ self.linked_doc_number = value.values[0]
141
+ super
142
+ end
143
+
144
+ def creator=(value)
145
+ return unless value
146
+
147
+ self.creator_id = value.values[0]
148
+ super
149
+ end
150
+
151
+ def modifier=(value)
152
+ return unless value
153
+
154
+ self.modifier_id = value.values[0]
155
+ super
156
+ end
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,41 @@
1
+ module Pina
2
+ module Models
3
+ class SalesInvoiceList
4
+ include Virtus.model
5
+
6
+ attribute :items, Array[SalesInvoice]
7
+ attribute :_meta
8
+ attribute :response
9
+
10
+ def next_page
11
+ Pina::SalesInvoice.all(extract_next_page)
12
+ end
13
+
14
+ def first_page
15
+ Pina::SalesInvoice.all
16
+ end
17
+
18
+ def previous_page
19
+ Pina::SalesInvoice.all(extract_prev_page)
20
+ end
21
+
22
+ private
23
+
24
+ def extract_next_page
25
+ string = _meta['pagination']['next']
26
+ return unless string
27
+
28
+ index = string.index('?')
29
+ string[index..-1]
30
+ end
31
+
32
+ def extract_prev_page
33
+ string = _meta['pagination']['prev']
34
+ return unless string
35
+
36
+ index = string.index('?')
37
+ string[index..-1]
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,77 @@
1
+ module Pina
2
+ module Models
3
+ class SalesItem
4
+ include Virtus.model
5
+
6
+ attribute :id
7
+ attribute :department
8
+ attribute :department_id
9
+ attribute :creator
10
+ attribute :creator_id
11
+ attribute :delivery
12
+ attribute :delivery_id
13
+ attribute :modifier
14
+ attribute :modifier_id
15
+ attribute :product
16
+ attribute :product_id
17
+ attribute :contract
18
+ attribute :contract_id
19
+ attribute :batch
20
+ attribute :created_at
21
+ attribute :description
22
+ attribute :expiration_date
23
+ attribute :external_id
24
+ attribute :percent_discount
25
+ attribute :quantity
26
+ attribute :serial_numbers
27
+ attribute :status
28
+ attribute :supplier_code
29
+ attribute :unit_price
30
+ attribute :updated_at
31
+ attribute :vat_rate
32
+ attribute '_destroy'
33
+
34
+ def department=(value)
35
+ return unless value
36
+
37
+ self.department_id = value.values[0]
38
+ super
39
+ end
40
+
41
+ def creator=(value)
42
+ return unless value
43
+
44
+ self.creator_id = value.values[0]
45
+ super
46
+ end
47
+
48
+ def delivery=(value)
49
+ return unless value
50
+
51
+ self.delivery_id = value.values[0]
52
+ super
53
+ end
54
+
55
+ def modifier=(value)
56
+ return unless value
57
+
58
+ self.modifier_id = value.values[0]
59
+ super
60
+ end
61
+
62
+ def product=(value)
63
+ return unless value
64
+
65
+ self.product_id = value.values[0]
66
+ super
67
+ end
68
+
69
+ def contract=(value)
70
+ return unless value
71
+
72
+ self.contract_id = value.values[0]
73
+ super
74
+ end
75
+ end
76
+ end
77
+ end
@@ -1,10 +1,10 @@
1
1
  module Pina
2
2
  class RestAdapter
3
3
  class << self
4
- def get(resource, id = nil)
4
+ def get(resource, id_or_params = nil)
5
5
  fail ConfigurationNotSet unless Pina.configuration
6
6
 
7
- request = Typhoeus.get(url(resource, id), headers: auth)
7
+ request = Typhoeus.get(url(resource, id_or_params), headers: auth)
8
8
  Response.new(request.response_code, request.body)
9
9
  end
10
10
 
@@ -42,8 +42,17 @@ module Pina
42
42
  }
43
43
  end
44
44
 
45
- def url(resource, id)
46
- Pina.configuration.base_url + "#{resource}/#{id}"
45
+ def url(resource, id_or_params)
46
+ if id_or_params.is_a? Hash
47
+ params = prepare_params_for_request(id_or_params)
48
+ Pina.configuration.base_url + "#{resource}?#{params}"
49
+ else
50
+ Pina.configuration.base_url + "#{resource}/#{id_or_params}"
51
+ end
52
+ end
53
+
54
+ def prepare_params_for_request(params)
55
+ params.map { |key, value| "#{URI::escape(key.to_s)}=#{URI::escape(value.to_s)}" }.join('&')
47
56
  end
48
57
  end
49
58
 
@@ -0,0 +1,51 @@
1
+ module Pina
2
+ class SalesInvoice
3
+ class << self
4
+ def new(params = nil)
5
+ Pina::Models::SalesInvoice.new(params)
6
+ end
7
+
8
+ def find(id)
9
+ response = Pina::RestAdapter.get(:sales_invoices, id)
10
+
11
+ return Pina::Models::SalesInvoice.new(attributes(response)) if
12
+ response.ok?
13
+
14
+ response
15
+ end
16
+
17
+ def all(page = nil)
18
+ response = Pina::RestAdapter.get(:sales_invoices, page)
19
+
20
+ return Pina::Models::SalesInvoiceList.new(attributes(response)) if
21
+ response.ok?
22
+
23
+ response
24
+ end
25
+
26
+ def create(sales_invoice)
27
+ response = Pina::RestAdapter.post(:sales_invoices, sales_invoice)
28
+
29
+ return Pina::Models::SalesInvoice.new(attributes(response)) if
30
+ response.ok?
31
+
32
+ response
33
+ end
34
+
35
+ def update(id, sales_invoice)
36
+ response = Pina::RestAdapter.patch(:sales_invoices, id, sales_invoice)
37
+
38
+ return Pina::Models::SalesInvoice.new(attributes(response)) if
39
+ response.ok?
40
+
41
+ response
42
+ end
43
+
44
+ private
45
+
46
+ def attributes(response)
47
+ response.to_hash.merge(response: response)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,35 @@
1
+ module Pina
2
+ module Utils
3
+ module Pagination
4
+ def next_page
5
+ RESOURCE.all(extract_next_page)
6
+ end
7
+
8
+ def first_page
9
+ RESOURCE.all
10
+ end
11
+
12
+ def previous_page
13
+ RESOURCE.all(extract_prev_page)
14
+ end
15
+
16
+ private
17
+
18
+ def extract_next_page
19
+ string = _meta['pagination']['next']
20
+ return unless string
21
+
22
+ index = string.index('?')
23
+ string[index..-1]
24
+ end
25
+
26
+ def extract_prev_page
27
+ string = _meta['pagination']['prev']
28
+ return unless string
29
+
30
+ index = string.index('?')
31
+ string[index..-1]
32
+ end
33
+ end
34
+ end
35
+ end
data/lib/pina/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pina
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
data/pina.gemspec CHANGED
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'vcr'
27
27
  spec.add_development_dependency 'factory_girl'
28
28
  spec.add_development_dependency 'dotenv'
29
+ spec.add_development_dependency('codeclimate-test-reporter')
29
30
 
30
31
  spec.add_runtime_dependency 'typhoeus'
31
32
  spec.add_runtime_dependency 'virtus'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pina
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
  - Pavel Hronek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-28 00:00:00.000000000 Z
11
+ date: 2016-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: codeclimate-test-reporter
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: typhoeus
127
141
  requirement: !ruby/object:Gem::Requirement
@@ -186,7 +200,12 @@ files:
186
200
  - lib/pina/models/address.rb
187
201
  - lib/pina/models/contact.rb
188
202
  - lib/pina/models/contact_list.rb
203
+ - lib/pina/models/sales_invoice.rb
204
+ - lib/pina/models/sales_invoice_list.rb
205
+ - lib/pina/models/sales_item.rb
189
206
  - lib/pina/rest_adapter.rb
207
+ - lib/pina/sales_invoice.rb
208
+ - lib/pina/utils/pagination.rb
190
209
  - lib/pina/version.rb
191
210
  - pina.gemspec
192
211
  homepage: https://github.com/ucetnictvi-on-line/pina