summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZack Hobson2013-12-26 12:59:03 -0800
committerZack Hobson2013-12-26 12:59:03 -0800
commit830ae68c2ee291ecd4e1d83b48d38224f434c98f (patch)
treef68e4f09bd60b69ed22eaaf8facf8598f1ed45e1
parent69cf87a9b35a85a25444590537ec49536f147299 (diff)
downloadhcl-830ae68c2ee291ecd4e1d83b48d38224f434c98f.tar.bz2
self-contained harvest adapter
-rw-r--r--lib/hcl/harvest_middleware.rb12
-rw-r--r--lib/hcl/net.rb5
2 files changed, 13 insertions, 4 deletions
diff --git a/lib/hcl/harvest_middleware.rb b/lib/hcl/harvest_middleware.rb
index 2b8b307..edf3b01 100644
--- a/lib/hcl/harvest_middleware.rb
+++ b/lib/hcl/harvest_middleware.rb
@@ -1,4 +1,4 @@
-require 'faraday/middleware'
+require 'faraday'
class HCl::HarvestMiddleware < Faraday::Middleware
Faraday.register_middleware harvest: ->{ self }
@@ -9,11 +9,21 @@ class HCl::HarvestMiddleware < Faraday::Middleware
require 'escape_utils'
end
+ def initialize app, user, password
+ super app
+ @auth = Faraday::Request::BasicAuthentication.new app, user, password
+ end
+
def call(env)
+ # basic authentication
+ @auth.call(env)
+
+ # encode with and accept json
env[:request_headers]['Accept'] = MIME_TYPE
env[:request_headers]['Content-Type'] = MIME_TYPE
env[:body] = Yajl::Encoder.encode(env[:body])
+ # response processing
@app.call(env).on_complete do |env|
case env[:status]
when 200..299
diff --git a/lib/hcl/net.rb b/lib/hcl/net.rb
index 28be3c1..67e3086 100644
--- a/lib/hcl/net.rb
+++ b/lib/hcl/net.rb
@@ -1,4 +1,4 @@
-require 'faraday'
+require 'hcl/harvest_middleware'
module HCl
module Net
@@ -24,8 +24,7 @@ module HCl
@http ||= Faraday.new(
"http#{ssl && 's'}://#{subdomain}.harvestapp.com"
) do |f|
- f.request :basic_auth, login, password
- f.use :harvest
+ f.use :harvest, login, password
f.adapter Faraday.default_adapter
end
end