# Emites Client
This is the official Ruby client for the [Emites](https://app.emites.com.br) API.
[](https://rubygems.org/gems/emites-client)
[](https://travis-ci.org/myfreecomm/emites-client-ruby)
[](https://codeclimate.com/github/myfreecomm/emites-client-ruby)
[](https://codeclimate.com/github/myfreecomm/emites-client-ruby)
[](http://inch-ci.org/github/myfreecomm/emites-client-ruby)
## Installation
Add this line to your application's Gemfile:
gem 'emites-client', require: 'emites'
And then execute:
$ bundle
Or install it yourself as:
$ gem install emites-client
## Configuration
##### Use Emites.configure to setup your environment:
```ruby
require "emites"
Emites.configure do |config|
config.url = "https://sandbox.emites.com.br/api/v1" # defaults to "https://app.emites.com.br/api/v1"
config.user_agent = 'My App v1.0' # optional, but you should pass a custom user-agent identifying your app
end
```
##### Create a new token in your Emites account: https://app.emites.com.br/config/api
## Usage
##### Given your token, create an instance of Emites::Client, as below:
```ruby
client = Emites.client("YOUR_TOKEN_HERE")
```
##### Now you have access to every API endpoint:
* [Emitters API](http://myfreecomm.github.io/emites/sandbox/v1/modules/emitter.html) as `client.emitters`
* [Takers API](http://myfreecomm.github.io/emites/sandbox/v1/modules/taker.html) as `client.takers`
* [Services API](http://myfreecomm.github.io/emites/sandbox/v1/modules/service_values.html) as `client.services`
* [Webhooks API](http://myfreecomm.github.io/emites/sandbox/v1/modules/webhooks.html) as `client.webhooks`
* [NFSe API](http://myfreecomm.github.io/emites/sandbox/v1/modules/nfse.html) as `client.nfse`
### Endpoints
#### [Emitters](http://myfreecomm.github.io/emites/sandbox/v1/modules/emitter.html)
#### [Takers](http://myfreecomm.github.io/emites/sandbox/v1/modules/taker.html)
#### [Services](http://myfreecomm.github.io/emites/sandbox/v1/modules/service_values.html)
#### [Webhooks](http://myfreecomm.github.io/emites/sandbox/v1/modules/webhooks.html)
#### [NFSe](http://myfreecomm.github.io/emites/sandbox/v1/modules/nfse.html)
## Callbacks
All actions that change data triggers an event that you can subscribe to. This event allow you to extend the logic executed when you call a client method.
#### Subscribing to an event
All you have to do is create a class that responds to a method `#call` with two arguments:
```ruby
class MyListener
def call(result, args)
end
end
```
**Where:**
* `result` is the return of a client method
* `args` is an array of arguments passed to the client method you called
Now you have a listener, you can subscribe to an event:
```ruby
Emites.subscribe("emites.emitters.destroy", MyListener.new)
```
**Example:**
When you call `client.emitters.destroy(1)`, an event `emites.emitters.destroy` will be triggered. Your listener method `#call` will receive:
* `result` would be `true or false` - depending on what `client.emitters.destroy(1)` returned
* `args` would be `[1]` - an array with the arguments passed to the client method
#### Available hooks
Resource |
Events |
emitters |
emites.emitters.create
emites.emitters.destroy
|
takers |
emites.takers.create
emites.takers.destroy
|
services |
emites.services.create
emites.services.destroy
|
webhooks |
emites.webhooks.create
emites.webhooks.update
emites.webhooks.destroy
|
nfse |
emites.nfse.create
emites.nfse.update
emites.nfse.destroy
emites.nfse.cancel
|
## Contributing
1. Fork it ( https://github.com/myfreecomm/emites-client-ruby/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request