Sha256: ca0ba2b2c893d4fb88e105bca0108e92b5fe51c268831f9794aaffa29a046542

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 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

      @api.setup :url => "https://#{host}/" 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.6 lib/norseal/api.rb