Plonquo Faraday Wrapper is a simple wrapper for the faraday gem for easy and clean communication between Plonquo Microservices.
gem 'plonquo_faraday_wrapper', git: 'https://github.com/Fabriquartz/faraday-wrapper.git'
wrapper = PlonquoFaradayWrapper.new('https://json-api.rail.io')
You can pass a hash with no_auth_required: true
to disable authentication checks before every request
wrapper = PlonquoFaradayWrapper.new(@config_hash[:rli_base_url], no_auth_required: true)
By calling the authenticate method you can set authentication for the wrapper object by either a token or using basic auth credentials, authorization will be required if initalized without the no_auth_required: false
option
wrapper.authenticate(username: '[email protected]', password: 'somepasswordhere')
wrapper.authenticate(token: 'Basic Y5VcmmljLnJlbW9uZEBmYWJyaXF1YaJ0e785l20321vbk2FhbG9vcmQxMDIx')
The authenticate method will return a hash of the user object
{
"username" => "cedric",
"email" => "[email protected]",
"first-name" => "Cedric",
"last-name" => "Remond",
"is-inactive" => false,
"valid-through" => nil,
"valid-from" => "2016-01-01T00:00:00.000Z",
"iseb" => true
}
The following methods are available for making requests:
- Post
- Get
- Put
- Patch
Requests that are made by calling the post or get method will by default have a content-type application/json and an authorization token set in the header. It is possible to overwrite the content-type header or add other headers or params by passing on a hash. The post and get methods wil return a hash created from returned json of the request
# Post request with default content-type and timeout (30 sec)
response = wrapper.post('/users', body: new_user.to_json)
# Get request with some custom headers and options
response = wrapper.get('/users/current', { timeout: 10, headers: { example_header: example_value }})
# Post request with custom options
options = {
content_type: 'application/xml',
timeout: 10,
headers: { example_header: example_value },
params: { example_param: example_value },
body: new_user.to_xml
}
response = wrapper.post('/users', options)
- Request
The request method enables custom requests without preset headers or params (except for timeout). The request method wil incontrary to the post and get method return the full response object from faraday instead of a hash.
options = {
url: 'https://bci.prorail.nl',
path: "/LIMessageProcessing/http/UICCCMessageProcessing",
body: message,
timeout: 10,
headers: { "Content-Type": 'application/xml', },
params: { example_param: example_value }
}
response = wrapper.request('post', options)
The wrapper object has multiple public attributes which can be accessed
-
conn
The faraday connection object. This will enable you acces to alle te settings and properties of the connection object e.g
wrapper.conn.ssl.verify = false
-
token
The token that has been set or has been generated by the authenticate method
-
user
A hash with the current user generated after calling the authenticate method