Module: InvisibleCollector::DefaultHandlers
- Included in:
- Resources::AlarmResource, Resources::CompanyResource, Resources::CustomerResource, Resources::DebtResource, Resources::EmailResource, Resources::GroupResource, Resources::PaymentResource, Resources::SmsResource, Resources::VmsResource
- Defined in:
- lib/invisible_collector/resources/default_handlers.rb
Instance Method Summary collapse
- #execute ⇒ Object
- #execute_get(endpoint, params) ⇒ Object
- #execute_post(endpoint, body) ⇒ Object
- #handle(code, &block) ⇒ Object
- #handles ⇒ Object
- #initialize(options = {}) ⇒ Object
Instance Method Details
#execute ⇒ Object
10 11 12 13 14 |
# File 'lib/invisible_collector/resources/default_handlers.rb', line 10 def execute response = yield(@connection) handles[response.status].call(response) if handles.key?(response.status) response end |
#execute_get(endpoint, params) ⇒ Object
16 17 18 19 20 |
# File 'lib/invisible_collector/resources/default_handlers.rb', line 16 def execute_get(endpoint, params) execute do |connection| connection.get(endpoint, params) end end |
#execute_post(endpoint, body) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/invisible_collector/resources/default_handlers.rb', line 22 def execute_post(endpoint, body) execute do |connection| connection.post do |req| req.url endpoint req.headers['Content-Type'] = 'application/json' req.body = (body.is_a?(String) ? body : body.to_json) end end end |
#handle(code, &block) ⇒ Object
36 37 38 |
# File 'lib/invisible_collector/resources/default_handlers.rb', line 36 def handle(code, &block) handles[code] = block end |
#handles ⇒ Object
32 33 34 |
# File 'lib/invisible_collector/resources/default_handlers.rb', line 32 def handles @handles ||= {} end |
#initialize(options = {}) ⇒ Object
5 6 7 8 |
# File 'lib/invisible_collector/resources/default_handlers.rb', line 5 def initialize( = {}) @connection = [:connection] handle(401) { |response| raise InvisibleCollector::Unauthorized.from_json(response.body) } end |