Sha256: 6804fb24c243d3e2dbf21b21ed072a40b5dc921f5e5ff2a235ee5043c4f177aa
Contents?: true
Size: 1.8 KB
Versions: 3
Compression:
Stored size: 1.8 KB
Contents
require 'faraday' require 'lolp/errors' require 'faraday_middleware' module Lolp module Connection def get(path, params = {}) request(:get, path, params) end def put(path, params = {}) request(:put, path, params) end def post(path, params = {}) request(:post, path, params) end def delete(path, params = {}) request(:delete, path, params) end def authenticated? !!@token end def last_response @last_response if defined? @last_response end def auto_loginable?(url) !authenticated? && url !~ /\/authenticate/ && @username && @password end def request(method, url = nil, data = nil, headers = nil, &block) login if auto_loginable?(url) @last_response = if %i(post put patch).include?(method) connection.run_request(method, url, data, headers, &block) else connection.run_request(method, url, nil, headers) { |r| r.params.update(data) if data yield(r) if block_given? } end if error = Error.from_response(@last_response) raise error end @last_response.body end private def connection project_url = 'https://github.com/pepabo/lolp.rb' user_agent = "lolp/#{VERSION} (+#{project_url}; ruby#{RUBY_VERSION})" ssl_verify = true args = { url: @api_endpoint, ssl: { verify: ssl_verify }, headers: { user_agent: user_agent, content_type: 'application/json' } } @connection.authorization(:Bearer, @token) if @connection && authenticated? @connection ||= Faraday.new(args) do |f| f.request :json f.response :json, content_type: /\bjson$/ f.adapter Faraday.default_adapter end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
lolp-0.8.1 | lib/lolp/connection.rb |
lolp-0.8.0 | lib/lolp/connection.rb |
lolp-0.7.0 | lib/lolp/connection.rb |