gh 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,99 +0,0 @@
1
- # Copyright (c) 2009 rick olson, zack hobson
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy of
4
- # this software and associated documentation files (the "Software"), to deal in
5
- # the Software without restriction, including without limitation the rights to
6
- # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
- # of the Software, and to permit persons to whom the Software is furnished to do
8
- # so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in all
11
- # copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
- # SOFTWARE.
20
-
21
- require 'faraday'
22
-
23
- if Faraday::VERSION < '0.8.0'
24
- $stderr.puts "please update faraday"
25
-
26
- # https://github.com/technoweenie/faraday/blob/master/lib/faraday/request/basic_authentication.rb
27
- require 'base64'
28
-
29
- module Faraday
30
- class Request::BasicAuthentication < Faraday::Middleware
31
- def initialize(app, login, pass)
32
- super(app)
33
- @header_value = "Basic #{Base64.encode64([login, pass].join(':')).gsub("\n", '')}"
34
- end
35
-
36
- def call(env)
37
- unless env[:request_headers]['Authorization']
38
- env[:request_headers]['Authorization'] = @header_value
39
- end
40
- @app.call(env)
41
- end
42
- end
43
- end
44
-
45
- # https://github.com/technoweenie/faraday/blob/master/lib/faraday/request/retry.rb
46
- module Faraday
47
- class Request::Retry < Faraday::Middleware
48
- def initialize(app, retries = 2)
49
- @retries = retries
50
- super(app)
51
- end
52
-
53
- def call(env)
54
- retries = @retries
55
- begin
56
- @app.call(env)
57
- rescue StandardError, Timeout::Error
58
- if retries > 0
59
- retries -= 1
60
- retry
61
- end
62
- raise
63
- end
64
- end
65
- end
66
- end
67
-
68
- # https://github.com/technoweenie/faraday/blob/master/lib/faraday/request/token_authentication.rb
69
- module Faraday
70
- class Request::TokenAuthentication < Faraday::Middleware
71
- def initialize(app, token, options={})
72
- super(app)
73
-
74
- # values = ["token=#{token.to_s.inspect}"]
75
- # options.each do |key, value|
76
- # values << "#{key}=#{value.to_s.inspect}"
77
- # end
78
- # comma = ",\n#{' ' * ('Authorization: Token '.size)}"
79
- # @header_value = "Token #{values * comma}"
80
- @header_value = "token #{token}"
81
- end
82
-
83
- def call(env)
84
- unless env[:request_headers]['Authorization']
85
- env[:request_headers]['Authorization'] = @header_value
86
- end
87
- @app.call(env)
88
- end
89
- end
90
- end
91
-
92
- # https://github.com/technoweenie/faraday/blob/master/lib/faraday/request.rb
93
- Faraday::Request.register_lookup_modules \
94
- :url_encoded => :UrlEncoded,
95
- :multipart => :Multipart,
96
- :retry => :Retry,
97
- :basic_auth => :BasicAuthentication,
98
- :token_auth => :TokenAuthentication
99
- end