Skip to content

Commit

Permalink
Add company, banks accounts, categories and users endpoints wrappers (#2
Browse files Browse the repository at this point in the history
)

* add company class with find action

* add company endpoint wrapper

* add alegra company endpoint basic tests

* add options hash to requests, refactor code a bit and return raw response if option format == raw

* add alegra record.rb and put contacts, payments and all records to inherit from it to refactor a bit

* gitignore byebug history

* pass options to put on company too

* delete byebug historu

* add wrapper with tests for users endpoints

* add categories endpoint wrapper with tests

* adds wrapper for bank accounts endpoint with tests

* details

* update readme
  • Loading branch information
Miguel Ferrer Isaza authored Feb 26, 2020
1 parent 735486d commit 042a180
Show file tree
Hide file tree
Showing 29 changed files with 1,566 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/pkg/
/spec/reports/
/tmp/
.byebug_history
74 changes: 73 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,78 @@ params = {

client.payments.create(params)
```
### Company

You can get the company:
```ruby
client.company.find
```

Also you can update it, as follows:
```ruby
params = { website: 'nominapp.com' }

client.company.update(params)
```

### Users

You can get the users:
```ruby
client.users.list
```

Also you can retrive a specific user by doing:
```ruby
client.users.find(1)
```

Lastly you can retrive the current user, as follows:
```ruby
client.users.find_current
```

### Categories

You can get all the categories on the client account like this:
```ruby
client.categories.list #this will retrieve the tree format by default
```
Or if you prefer the plain format from the Alegra API just pass it as a paramater:
```ruby
client.categories.list(format: 'plain')
```
You can also retrive a specific category, as follows:
```ruby
client.categories.find('5047')
```

### BankAccounts

To retrive all bank accounts:
```ruby
client.bank_accounts.list
```
Or get a specific bank_account by id:
```ruby
client.bank_accounts.find(2)
```
Also you are able to create a new bank accounts, as follows:
```ruby
params = { name: 'test',
type: 'bank',
initial_balance: '100000',
initial_balance_date: '2020-02-25' }

client.bank_accounts.create(params)
```
And to create a bank transfer between accounts:
```ruby
params = { id_destination: 4, # 4 is the destination bank_account_id
amount: 100000,
date: '2020-02-25' }
client.bank_accounts.transfer(7, params) # 7 is the origin bank_account_id
```

## Development

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

- Diego Gomez
- Nicolas Mena
- Nicolas Mena
21 changes: 21 additions & 0 deletions lib/alegra/bank_accounts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Alegra
class BankAccounts < Alegra::Record
def list(options = { format: :formated })
client.get('bank-accounts', {}, options)
end

def find(id, options = { format: :formated })
client.get("bank-accounts/#{id}", {}, options)
end

def create(params, options = { format: :formated })
params = params.deep_camel_case_lower_keys
client.post('bank-accounts', params, options)
end

def transfer(id, params, options = { format: :formated })
params = params.deep_camel_case_lower_keys
client.post("bank-accounts/#{id}/transfer", params, options)
end
end
end
11 changes: 11 additions & 0 deletions lib/alegra/categories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Alegra
class Categories < Alegra::Record
def list(params = {}, options = { format: :formated })
client.get('categories', params, options)
end

def find(id, options = { format: :formated })
client.get("categories/#{id}", {}, options)
end
end
end
37 changes: 29 additions & 8 deletions lib/alegra/client.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
require 'alegra/setup'
require 'alegra/request'
require 'alegra/record'
require 'alegra/invoices'
require 'alegra/contacts'
require 'alegra/items'
require 'alegra/payments'
require 'alegra/company'
require 'alegra/users'
require 'alegra/categories'
require 'alegra/bank_accounts'

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 = {}, options = { format: :formated })
request = Alegra::Request.new(@setup.host, @setup.path, @setup.token)
request.get(url, params)
request.get(url, params, options)
end

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

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

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

def contacts
Expand All @@ -46,5 +51,21 @@ def items
def payments
Alegra::Payments.new(self)
end

def company
Alegra::Company.new(self)
end

def users
Alegra::Users.new(self)
end

def categories
Alegra::Categories.new(self)
end

def bank_accounts
Alegra::BankAccounts.new(self)
end
end
end
12 changes: 12 additions & 0 deletions lib/alegra/company.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Alegra
class Company < Alegra::Record
def find(options = { format: :formated })
client.get('company', {}, options)
end

def update(params, options = { format: :formated })
sanitize_params = params.deep_camel_case_lower_keys
client.put('company', sanitize_params, options)
end
end
end
10 changes: 2 additions & 8 deletions lib/alegra/contacts.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
module Alegra
class Contacts
attr_reader :client

def initialize(client)
@client = client
end

class Contacts < Alegra::Record
# @param id [ Integer ]
# @return [ Hash ]
def find(id)
Expand Down Expand Up @@ -80,4 +74,4 @@ def delete(id)
client.delete("contacts/#{id}")
end
end
end
end
8 changes: 1 addition & 7 deletions lib/alegra/invoices.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
module Alegra
class Invoices
attr_reader :client

def initialize(client)
@client = client
end

class Invoices < Alegra::Record
# @param id [ Integer ]
# @return [ Hash ]
def find(id)
Expand Down
10 changes: 2 additions & 8 deletions lib/alegra/items.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
module Alegra
class Items
attr_reader :client

def initialize(client)
@client = client
end

class Items < Alegra::Record
# @param id [ Integer ]
# @return [ Hash ]
def find(id)
Expand Down Expand Up @@ -53,4 +47,4 @@ def delete(id)
client.delete("items/#{ id }")
end
end
end
end
10 changes: 2 additions & 8 deletions lib/alegra/payments.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
module Alegra
class Payments
attr_reader :client

def initialize(client)
@client = client
end

class Payments < Alegra::Record
# @param id [ Integer ]
# @return [ Hash ]
def find(id)
Expand Down Expand Up @@ -73,4 +67,4 @@ def delete(id)
client.delete("payments/#{id}")
end
end
end
end
9 changes: 9 additions & 0 deletions lib/alegra/record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Alegra
class Record
attr_reader :client

def initialize(client)
@client = client
end
end
end
50 changes: 34 additions & 16 deletions lib/alegra/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(host, path, token=nil)
@session = Faraday.new url: host
end

def get(url, params = {})
def get(url, params = {}, options = { format: :formated })
params = URI.encode_www_form(params)

response = @session.get do |req|
Expand All @@ -18,12 +18,10 @@ def get(url, params = {})
req.headers['Accept'] = 'application/json'
req.headers['Authorization'] = "Basic #{@token}"
end

cast_error(response) unless response.status == 200 || response.status == 201
Alegra::Response.new(response.body).call
response_of_request(response, options)
end

def post(url, params = {})
def post(url, params = {}, options = { format: :formated })
params = JSON.generate(params)
response = @session.post do |req|
req.url "#{ @path }#{ url }"
Expand All @@ -32,11 +30,10 @@ def post(url, params = {})
req.headers['Authorization'] = "Basic #{ @token }"
req.body = params
end
cast_error(response) unless (response.status == 200 || response.status == 201)
return Alegra::Response.new(response.body).call
response_of_request(response, options)
end

def put(url, params={})
def put(url, params={}, options = { format: :formated })
params = JSON.generate(params)
response = @session.put do |req|
req.url "#{ @path }#{ url }"
Expand All @@ -45,12 +42,10 @@ def put(url, params={})
req.headers['Authorization'] = "Basic #{ @token }"
req.body = params
end
cast_error(response) unless (response.status == 200 || response.status == 201)
return Alegra::Response.new(response.body).call
response_of_request(response, options)
end


def delete(url, params={})
def delete(url, params={}, options = { format: :formated })
params = JSON.generate(params)
response = @session.delete do |req|
req.url "#{ @path }#{ url }"
Expand All @@ -59,12 +54,28 @@ def delete(url, params={})
req.headers['Authorization'] = "Basic #{ @token }"
req.body = params
end
cast_error(response) unless (response.status == 200 || response.status == 201)
return Alegra::Response.new(response.body).call
response_of_request(response, options)
end

private

def response_of_request(response, options = { format: :formated })
cast_error(response, options) unless response.status == 200 || response.status == 201

raise_invalid_format options[:format]

return response if options[:format] == :raw

Alegra::Response.new(response.body).call
end

def cast_error(response)
def cast_error(response, options = { format: :formated })
raise_invalid_format options[:format]

return response if options[:format] == :raw

message = response.body.empty? ? response.body : Alegra::Response.new(response.body).call['message']

error_map = {
500 => 'Sever error! Something were wrong in the server.',
400 => "Bad request!, #{ message }",
Expand All @@ -76,5 +87,12 @@ def cast_error(response)
}
raise StandardError, "Status: #{ response.status }. Error: #{ error_map[response.status] }"
end

def raise_invalid_format(format)
return if %i[formated raw].include?(format)
return if format.nil?

raise StandardError, "#{format} is not a valid format, valid_formats[:formated, :raw]"
end
end
end
end
2 changes: 1 addition & 1 deletion lib/alegra/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ def call(options={})
end
end
end
end
end
Loading

0 comments on commit 042a180

Please sign in to comment.