Skip to content

Commit

Permalink
Add payments class and upgrade depences (#1)
Browse files Browse the repository at this point in the history
* update dependencies version

* Add reload! method for bin/console

* Add params support to get method request

* Add params support to get method request to invoices class

* Add Payments class

* Add create payment

* update README

* update version gem

* add comment params and update payments functionality

* Add delete payments

* changes styles code
  • Loading branch information
nmenag authored Nov 7, 2019
1 parent 10f554a commit 735486d
Show file tree
Hide file tree
Showing 15 changed files with 424 additions and 261 deletions.
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in alegra.gemspec
gemspec
gemspec
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,44 @@ Send that invoice by email:
params = { emails: ['[email protected]', '[email protected]'], send_copy_to_user: true, invoice_type: 'copy'}
client.invoices.send_by_email(1, params)
```

### Payments

You can get all payments:
```ruby
client.payments.list()
```

Or get a specific payment by id:
```ruby
client.payments.find(1) # the parameter is the payment id
```

Also you are able to create a new payments, as follows:
```ruby
params = {
date: "2015-12-13",
invoices: [
{
id: 6,
amount: 150
},
{
id: 200,
amount: 500
}
],
bank_account: 1
}

client.payments.create(params)
```

## Development

This gem is under construction and I'm writing it with the goal that it will easy to use. However, if you have any recommendation is well received.

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.
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

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).

Expand All @@ -178,6 +211,10 @@ The next endpoints are pending:
- Retentions
- Categories
- Sellers
- payments
- cancel payment(void)
- open payment convert https://developer.alegra.com/docs/convertir-pago-a-abierto
- Add attachment https://developer.alegra.com/docs/adjuntar-archivos-a-pagos

## License

Expand All @@ -186,4 +223,4 @@ The gem is available as open source under the terms of the [MIT License](http://
## Contributors

- Diego Gomez

- Nicolas Mena
17 changes: 8 additions & 9 deletions alegra.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.13"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "vcr", "~> 3"
spec.add_development_dependency "webmock", "~> 2.1"
spec.add_development_dependency "jazz_fingers", "~> 4"

spec.add_dependency "faraday", "~> 0.9"
spec.add_dependency "json", "~> 1.7"
spec.add_development_dependency 'bundler', '~> 2.0'
spec.add_dependency 'faraday', '~> 0.17.0'
spec.add_development_dependency 'jazz_fingers', '~> 5.0'
spec.add_dependency 'json', '~> 2.2'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rspec', '~> 3.9.0'
spec.add_development_dependency 'vcr', '~> 5.0'
spec.add_development_dependency 'webmock', '~> 3.7'
end
18 changes: 16 additions & 2 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "alegra"
require 'bundler/setup'
require 'alegra'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand All @@ -10,5 +10,19 @@ require "alegra"
# require "pry"
# Pry.start

def reload!(print = true)
puts 'Reloading ...' if print
# Main project directory.
root_dir = File.expand_path('..', __dir__)
# Directories within the project that should be reloaded.
reload_dirs = %w{lib}
# Loop through and reload every file in all relevant project directories.
reload_dirs.each do |dir|
Dir.glob("#{root_dir}/#{dir}/**/*.rb").each { |f| load(f) }
end
# Return true when complete.
true
end

require "irb"
IRB.start
17 changes: 11 additions & 6 deletions lib/alegra/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
require 'alegra/invoices'
require 'alegra/contacts'
require 'alegra/items'
require 'alegra/payments'

module Alegra
class Client
def initialize(username=nil, apikey=nil, debug=false)
@setup = Alegra::Setup.new(username, apikey, debug)
end

def get(url, params={})
def get(url, params = {})
request = Alegra::Request.new(@setup.host, @setup.path, @setup.token)
request.get(url, params)
end

def post(url, params={})
def post(url, params = {})
request = Alegra::Request.new(@setup.host, @setup.path, @setup.token)
request.post(url, params)
end
Expand All @@ -30,16 +31,20 @@ def delete(url, params={})
request.delete(url, params)
end

def invoices
Alegra::Invoices.new(self)
end

def contacts
Alegra::Contacts.new(self)
end

def invoices
Alegra::Invoices.new(self)
end

def items
Alegra::Items.new(self)
end

def payments
Alegra::Payments.new(self)
end
end
end
27 changes: 19 additions & 8 deletions lib/alegra/contacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@ def initialize(client)
@client = client
end

# @param id [ Interger ]
# @param id [ Integer ]
# @return [ Hash ]
def find(id)
client.get("contacts/#{ id }")
end

# Returs all contacts
# @param params [ Hash ]
# - start [ Integer ]
# - limit [ Integer ]
# - order_direction [ String ]
# - order_field [ string ]
# - query [ String ]
# - type [ Integer ]
# - metadata [ Boolean ]
# - name [ String ]
# - identification [ String ]
# @return [ Array ]
def list()
client.get('contacts')
def list(params = {})
client.get('contacts', params)
end

# @param params [ Hash ]
Expand All @@ -36,8 +46,8 @@ def list()
# - internal_contacts [ Array ]
# @return [ Hash ]
def create(params)
_params = params.deep_camel_case_lower_keys
client.post('contacts', _params)
params = params.deep_camel_case_lower_keys
client.post('contacts', params)
end

# @param id [ Integer ]
Expand All @@ -59,14 +69,15 @@ def create(params)
# - internal_contacts [ Array ]
# @return [ Hash ]
def update(id, params)
_params = params.deep_camel_case_lower_keys
client.put("contacts/#{ id }", _params)
sanitize_params = params.deep_camel_case_lower_keys
client.put("contacts/#{id}", sanitize_params)

end

# @param id [ Integer ]
# @return [ Hash ]
def delete(id)
client.delete("contacts/#{ id }")
client.delete("contacts/#{id}")
end
end
end
33 changes: 23 additions & 10 deletions lib/alegra/invoices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,29 @@ def initialize(client)
@client = client
end

# @param id [ Interger ]
# @param id [ Integer ]
# @return [ Hash ]
def find(id)
client.get("invoices/#{ id }")
client.get("invoices/#{id}")
end

# Returs all invoices
# @param params [ Hash ]
# - start [ Integer ]
# - limit [ Integer ]
# - order_direction [ String ]
# - order_field [ string ]
# - metadata [ Boolean ]
# - id [ Integer ]
# - date [ String ]
# - due_date [ String ]
# - status [ String ]
# - client_name [ String ]
# - client_identification [ String ]
# - number_template_full_number
# @return [ Array ]
def list()
client.get('invoices')
def list(params = {})
client.get('invoices', params)
end

# Creates a invoice
Expand All @@ -36,8 +49,8 @@ def list()
# - seller [ String ]
# @return [ Hash ]
def create(params)
_params = params.deep_camel_case_lower_keys
client.post('invoices', _params)
sanitize_params = params.deep_camel_case_lower_keys
client.post('invoices', sanitize_params)
end

# Creates a invoice
Expand All @@ -58,8 +71,8 @@ def create(params)
# - seller [ String ]
# @return [ Hash ]
def update(id, params)
_params = params.deep_camel_case_lower_keys
client.put("invoices/#{ id }", _params)
sanitize_params = params.deep_camel_case_lower_keys
client.put("invoices/#{id}", sanitize_params)
end

# @param id [ Integer ]
Expand All @@ -69,8 +82,8 @@ def update(id, params)
# - invoiceType [ String ]
# @return [ Hash ]
def send_by_email(id, params)
_params = params.deep_camel_case_lower_keys
client.post("invoices/#{ id }/email", _params)
sanitize_params = params.deep_camel_case_lower_keys
client.post("invoices/#{id}/email", sanitize_params)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/alegra/items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def initialize(client)
@client = client
end

# @param id [ Interger ]
# @param id [ Integer ]
# @return [ Hash ]
def find(id)
client.get("items/#{ id }")
Expand Down
76 changes: 76 additions & 0 deletions lib/alegra/payments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module Alegra
class Payments
attr_reader :client

def initialize(client)
@client = client
end

# @param id [ Integer ]
# @return [ Hash ]
def find(id)
client.get("payments/#{id}")
end

# Returs all payments
# @param params [ Hash ]
# - start [ Integer ]
# - limit [ Integer ]
# - order_direction [ String ]
# - order_field [ string ]
# - type [ Integer ]
# - metadata [ Boolean ]
# - id [ Integer ]
# @return [ Array ]
def list(params = {})
client.get('payments', params)
end

# Creates a payment
# @param params [ Hash ]
# - date [ String ]
# - bank_account [ Integer ] or [ Hash ]
# - payment_method [ String ]
# - observations [ String ]
# - anotation [ String ]
# - type [ String ]
# - client [ Integer ] or [ Hash ]
# - invoices [ Array ]
# - bills [ Array ]
# - categories [ Array ]
# - retentions [ Array ]
# - currency [ Array ]
# @return [ Hash ]
def create(params)
sanitize_params = params.deep_camel_case_lower_keys
client.post('payments', sanitize_params)
end

# Update a payment
# @param id [ Integer ]
# @param params [ Hash ]
# - date [ String ]
# - bank_account [ Integer ] or [ Hash ]
# - payment_method [ String ]
# - observations [ String ]
# - anotation [ String ]
# - type [ String ]
# - client [ Integer ] or [ Hash ]
# - invoices [ Array ]
# - bills [ Array ]
# - categories [ Array ]
# - retentions [ Array ]
# - currency [ Array ]
# @return [ Hash ]
def update(id, params)
sanitize_params = params.deep_camel_case_lower_keys
client.put("payments/#{id}", sanitize_params)
end

# @param id [ Integer ]
# @return [ Hash ]
def delete(id)
client.delete("payments/#{id}")
end
end
end
Loading

0 comments on commit 735486d

Please sign in to comment.