Class: Sipwizard::Connection
- Inherits:
-
Object
- Object
- Sipwizard::Connection
- Defined in:
- lib/sipwizard/connection.rb
Constant Summary collapse
- BASE_REST_API =
"https://www.sipsorcery.com/rest/v0.1"
- API_PATHS =
{ provisioning: "#{BASE_REST_API}/provisioning.svc", accounting: "#{BASE_REST_API}/accounting.svc", }
Instance Attribute Summary collapse
-
#faraday_connection ⇒ Object
Returns the value of attribute faraday_connection.
Instance Method Summary collapse
-
#get(path, params = {}) ⇒ Object
Public make a get request to the api.
-
#initialize(options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #post(path, params) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Connection
Returns a new instance of Connection.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/sipwizard/connection.rb', line 14 def initialize(={}) faraday_adapter = .fetch(:adapter){ Faraday.default_adapter } api_type = .fetch(:api_type){ :provisioning } params = .fetch(:connection_params){{}} @faraday_connection = Faraday.new(API_PATHS[api_type], params ) do |faraday| faraday.request :url_encoded #for post/put params faraday.response :logger faraday.response :raise_error faraday.response :json, content_type: /\bjson\z/ faraday.adapter faraday_adapter end @faraday_connection.headers['apikey'] = Sipwizard.config.api_key end |
Instance Attribute Details
#faraday_connection ⇒ Object
Returns the value of attribute faraday_connection.
12 13 14 |
# File 'lib/sipwizard/connection.rb', line 12 def faraday_connection @faraday_connection end |
Instance Method Details
#get(path, params = {}) ⇒ Object
Public make a get request to the api
path - The path of the api params - The query parameters (default: {}):
Examples
get('cdr/count')
get('cdr/count', { where: 'FromName="John Doe"'})
Returns Faraday::Response
44 45 46 |
# File 'lib/sipwizard/connection.rb', line 44 def get(path, params={}) self.faraday_connection.get(path, params).body end |
#post(path, params) ⇒ Object
48 49 50 51 52 |
# File 'lib/sipwizard/connection.rb', line 48 def post(path, params) self.faraday_connection.headers['Content-Type'] ='application/json; charset=utf-8' payload = params.to_json self.faraday_connection.post(path, payload).body end |