lib/norseal/api.rb in norseal-api-0.1.4 vs lib/norseal/api.rb in norseal-api-0.1.5
- old
+ new
@@ -5,28 +5,50 @@
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 => "http://#{host}/" do |c|
- c.use FaradayMiddleware::EncodeJson
+ c.use Norseal::Api::MyTokenAuth, token: api_key
+ # c.use FaradayMiddleware::EncodeJson
c.use Her::Middleware::AcceptJSON
- c.use Her::Middleware::FirstLevelParseJSON
+ # c.use Her::Middleware::FirstLevelParseJSON
- c.authorization :token, api_key
+ # 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)
+ # 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