faraday-conductivity 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,19 +2,36 @@
2
2
 
3
3
  Extra Faraday Middleware! Geared towards a service oriented architecture.
4
4
 
5
+ These middlewares are currently included:
6
+
7
+ * **user_agent**, adds a dynamic `User-Agent` header to the request, so you
8
+ know which server and process the request is coming from.
9
+ * **extended_logging**, logs *all* the information of the request.
10
+ * **request_id**, passes along the `X-Request-Id` header to track API request
11
+ back to the source.
12
+ * **mimetype**, allows you to specify the `Accept` header for API versioning.
13
+
14
+ Further information:
15
+
16
+ * [faraday](https://github.com/lostisland/faraday)
17
+ * [faraday_middleware](https://github.com/lostisland/faraday_middleware)
18
+
19
+ ## Example
20
+
5
21
  Here is an overview of my favorite stack. More information about each
6
22
  middleware is below.
7
23
 
8
24
  ``` ruby
9
25
  APP_VERSION = IO.popen(["git", "rev-parse", "HEAD", :chdir => Rails.root]).read.chomp
10
26
 
27
+ require "faraday_middleware"
28
+ require "faraday/conductivity"
29
+
11
30
  connection = Faraday.new(url: "http://widgets.yourapp.com") do |faraday|
12
31
 
13
32
  # provided by Faraday itself
14
- faraday.token_auth "secret"
33
+ faraday.token_auth "secret"
15
34
 
16
- # provided by this gem
17
- faraday.use :extended_logging, logger: Rails.logger
18
35
 
19
36
  # provided by Faraday
20
37
  faraday.request :multipart
@@ -23,6 +40,10 @@ connection = Faraday.new(url: "http://widgets.yourapp.com") do |faraday|
23
40
  # provided by this gem
24
41
  faraday.request :user_agent, app: "MarketingSite", version: APP_VERSION
25
42
  faraday.request :request_id
43
+ faraday.request :mimetype, accept: "application/vnd.widgets-v2+json"
44
+
45
+ # provided by this gem
46
+ faraday.use :extended_logging, logger: Rails.logger
26
47
 
27
48
  # provided by faraday_middleware
28
49
  faraday.response :json, content_type: /\bjson$/
@@ -32,24 +53,25 @@ connection = Faraday.new(url: "http://widgets.yourapp.com") do |faraday|
32
53
  end
33
54
  ```
34
55
 
35
- You should also take a look at
36
- [faraday_middleware](https://github.com/lostisland/faraday_middleware).
37
-
38
- More middleware to come!
39
-
40
56
  ## Installation
41
57
 
42
58
  Add this line to your application's Gemfile:
43
59
 
44
- gem 'faraday-conductivity'
60
+ ``` ruby
61
+ gem 'faraday-conductivity'
62
+ ```
45
63
 
46
64
  And then execute:
47
65
 
48
- $ bundle
66
+ ```
67
+ $ bundle
68
+ ```
49
69
 
50
70
  Or install it yourself as:
51
71
 
52
- $ gem install faraday-conductivity
72
+ ```
73
+ $ gem install faraday-conductivity
74
+ ```
53
75
 
54
76
  ## Usage
55
77
 
@@ -66,6 +88,9 @@ connection = Faraday.new(url: "http://widgets.yourapp.com") do |faraday|
66
88
  end
67
89
  ```
68
90
 
91
+ Be sure to put this middleware after other middleware that add headers,
92
+ otherwise it will log incomplete requests.
93
+
69
94
  ### RequestID
70
95
 
71
96
  Pass on a request ID from your frontend applications to your backend services.
@@ -91,7 +116,15 @@ class Application < ActionController::Base
91
116
  end
92
117
  ```
93
118
 
94
- It's a hack, but it works really well.
119
+ It's a hack, because it uses a thread local variable, but it works really well.
120
+
121
+ Don't forget to turn on uuid logging in Rails too, by uncommenting the line in
122
+ `config/environments/production.rb`:
123
+
124
+ ``` ruby
125
+ # Prepend all log lines with the following tags
126
+ config.log_tags = [ :uuid ]
127
+ ```
95
128
 
96
129
  ### User Agent
97
130
 
@@ -38,26 +38,22 @@ module Faraday
38
38
  end
39
39
 
40
40
  def request_debug(env)
41
- <<-MESSAGE
42
- Request Headers:
43
- ----------------
44
- #{format_headers env[:request_headers]}
45
-
46
- Request Body:
47
- -------------
48
- #{env[:body]}
49
- MESSAGE
41
+ debug_message("Request", env[:request_headers], env[:body])
50
42
  end
51
43
 
52
44
  def response_debug(env)
53
- <<-MESSAGE
54
- Response Headers:
55
- -----------------
56
- #{format_headers env[:response_headers]}
45
+ debug_message("Response", env[:response_headers], env[:body])
46
+ end
47
+
48
+ def debug_message(name, headers, body)
49
+ <<-MESSAGE.gsub(/^ +([^ ])/m, '\\1')
50
+ #{name} Headers:
51
+ ----------------
52
+ #{format_headers(headers)}
57
53
 
58
- Response Body:
59
- --------------
60
- #{env[:body]}
54
+ #{name} Body:
55
+ -------------
56
+ #{body}
61
57
  MESSAGE
62
58
  end
63
59
 
@@ -0,0 +1,17 @@
1
+ module Faraday
2
+ module Conductivity
3
+ class Mimetype < Faraday::Middleware
4
+
5
+ def initialize(app, options = {})
6
+ super(app)
7
+ @accept = options.fetch(:accept)
8
+ end
9
+
10
+ def call(env)
11
+ env[:request_headers]['Accept'] ||= @accept
12
+ @app.call(env)
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -1,5 +1,5 @@
1
1
  module Faraday
2
2
  module Conductivity
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -1,10 +1,12 @@
1
1
  require "faraday"
2
2
 
3
3
  require "faraday/conductivity/version"
4
+
5
+ require "faraday/conductivity/extended_logging"
6
+ require "faraday/conductivity/mimetype"
4
7
  require "faraday/conductivity/request_id"
5
8
  require "faraday/conductivity/request_id_filter"
6
9
  require "faraday/conductivity/user_agent"
7
- require "faraday/conductivity/extended_logging"
8
10
 
9
11
  module Faraday
10
12
  module Conductivity
@@ -12,5 +14,6 @@ module Faraday
12
14
  register_middleware :middleware, :extended_logging => Faraday::Conductivity::ExtendedLogging
13
15
  register_middleware :request, :user_agent => Faraday::Conductivity::UserAgent
14
16
  register_middleware :request, :request_id => Faraday::Conductivity::RequestId
17
+ register_middleware :request, :mimetype => Faraday::Conductivity::Mimetype
15
18
  end
16
19
 
@@ -45,6 +45,14 @@ describe Faraday::Conductivity do
45
45
  response.env[:request_headers]["X-Request-Id"].should eq "my-request-id"
46
46
  end
47
47
 
48
+ example "mimetype" do
49
+ mimetype = "application/vnd.users-v2+json"
50
+ response = request_with do |faraday|
51
+ faraday.request :mimetype, accept: mimetype
52
+ end
53
+ response.env[:request_headers]["Accept"].should eq mimetype
54
+ end
55
+
48
56
  def request_with
49
57
  stubs = Faraday::Adapter::Test::Stubs.new do |stub|
50
58
  stub.get('/test') { |env| [200, {"X-Response-Header" => "header-value"}, {foo:"the dummy response"}.to_json] }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-conductivity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-22 00:00:00.000000000 Z
12
+ date: 2013-01-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -91,6 +91,7 @@ files:
91
91
  - faraday-conductivity.gemspec
92
92
  - lib/faraday/conductivity.rb
93
93
  - lib/faraday/conductivity/extended_logging.rb
94
+ - lib/faraday/conductivity/mimetype.rb
94
95
  - lib/faraday/conductivity/request_id.rb
95
96
  - lib/faraday/conductivity/request_id_filter.rb
96
97
  - lib/faraday/conductivity/user_agent.rb