Sha256: ad54012951964dc243d407f5914ca554b4eec9b53ccd20af5cf3bdfdb2426db2

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

require 'base64'
require 'faraday_middleware'
require 'faraday/response/raise_http_error'

module Lelylan
  module Connection
    private

    def connection(method='get', path='', authenticate=true, raw=false, version=0, force_urlencoded=false)

      options = {
        :headers => {'Accept' => 'application/json', 'User-Agent' => user_agent, 'Content-Type' => 'application/json'},
        :proxy => proxy,
        :ssl => { :verify => false },
        :url => Lelylan.api_endpoint
      }

      if authenticated?
        self.token = token.refresh! if token.expired?
        options[:headers].merge!('Authorization' => "Bearer #{token.token}")
      end

      if path =~ /subscriptions/
        raise Lelylan::Error, 'To make a request to the realtime services you need both client id and client secret' if (!client_id or !client_secret) and path =~ /subscriptions/
      end

      if method == :delete
        options[:headers].merge!('Content-Length' => '0')
      end

      connection = Faraday.new(options) do |builder|
        builder.request :json
        builder.use Faraday::Request::BasicAuthentication, self.client_id, self.client_secret if path =~ /subscriptions/
        builder.use FaradayMiddleware::Mashify
        builder.use FaradayMiddleware::ParseJson
        builder.use Faraday::Response::RaiseHttpError
        builder.adapter(adapter)
      end

      connection
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lelylan-rb-0.1.0 lib/lelylan/connection.rb
lelylan-rb-0.0.5 lib/lelylan/connection.rb
lelylan-rb-0.0.4 lib/lelylan/connection.rb