Sha256: c0e1a683a7e5e06243679ee2cdcde661f6ee20f9b8ddbac9681d862e031bc78f

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require 'her'
require 'faraday_middleware'
require 'active_support/concern'
require 'norseal/api/model'

module Norseal
  module Api
    class ClientNotConfigured < Exception; end

    class MyTokenAuth < Faraday::Middleware
      def initialize(app, options = {})
        @app = app
        @options = options
      end

      def call(env)
        env[:request_headers]["authorization"] = "Token token=#{@options[:token]}" if @options.include?(:token)
        @app.call(env)
      end
    end

    def self.configure(host, api_key, &block)
      @api = Her::API.new

      ssl_options = {ca_file: '/usr/lib/ssl/certs/ca-certificates.crt'}
      @api.setup :url => "https://#{host}/", ssl: ssl_options do |c|
        c.use Norseal::Api::MyTokenAuth, token: api_key
        # c.use FaradayMiddleware::EncodeJson
        c.use Her::Middleware::AcceptJSON
        # c.use Her::Middleware::FirstLevelParseJSON

        # Response
  c.use Her::Middleware::DefaultParseJSON

  # Adapter
  c.use Faraday::Adapter::NetHttp

        c.use Faraday::Request::UrlEncoded

        # c.authorization :token, Token: api_key

        yield c if block_given?

        # c.adapter Faraday.default_adapter# unless c.builder.handlers.include?(Faraday::Adapter::Test)
      end

      require "norseal/api/resources/collection"
      require "norseal/api/resources/product"
      require "norseal/api/resources/question"
      require "norseal/api/resources/manufacturer"
      require "norseal/api/resources/attribute"
    end

    def self.api
      raise ClientNotConfigured.new("Norseal") unless @api
      @api
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
norseal-api-0.1.7 lib/norseal/api.rb