Class: Rack::Webmoney
- Inherits:
-
Object
- Object
- Rack::Webmoney
- Defined in:
- lib/rack/webmoney.rb
Overview
A Rack middleware that provides a more HTTPish API around Webmoney authentication
You trigger an Webmoney request similar to HTTP authentication. From your app, return a “401 Unauthorized” and a “WWW-Authenticate” header with the identifier you would like to validate.
On competition, the Webmoney response is automatically verified and assigned to env["rack.webmoney.response"]
.
Defined Under Namespace
Constant Summary collapse
- HTTP_METHODS =
:stopdoc:
%w(GET HEAD PUT POST DELETE OPTIONS)
- RESPONSE =
"rack.webmoney.response"
- AUTHENTICATE_HEADER =
"WWW-Authenticate"
- AUTHENTICATE_REGEXP =
/^Webmoney/
- URL_FIELD_SELECTOR =
lambda { |field| field.to_s =~ %r{^https://} }
Instance Attribute Summary collapse
-
#credentials ⇒ Object
readonly
Returns the value of attribute credentials.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#wm_instance ⇒ Object
readonly
Returns the value of attribute wm_instance.
Class Method Summary collapse
-
.build_header(params = {}) ⇒ Object
Helper method for building the “WWW-Authenticate” header value.
-
.parse_header(str) ⇒ Object
Helper method for parsing “WWW-Authenticate” header values into a hash.
Instance Method Summary collapse
-
#call(env) ⇒ Object
Standard Rack
call
dispatch that accepts anenv
and returns a[status, header, body]
response. -
#initialize(app, opts = {}) ⇒ Webmoney
constructor
Initialize middleware with application.
Constructor Details
#initialize(app, opts = {}) ⇒ Webmoney
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/rack/webmoney.rb', line 149 def initialize(app, opts ={}) @app = app @credentials = opts[:credentials] @mode = opts[:mode] @wm_instance = if defined?(Rails) Rails.webmoney else WmLib.new(:wmid => @credentials[:site_holder_wmid]) end end |
Instance Attribute Details
#credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
139 140 141 |
# File 'lib/rack/webmoney.rb', line 139 def credentials @credentials end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
139 140 141 |
# File 'lib/rack/webmoney.rb', line 139 def mode @mode end |
#wm_instance ⇒ Object (readonly)
Returns the value of attribute wm_instance.
139 140 141 |
# File 'lib/rack/webmoney.rb', line 139 def wm_instance @wm_instance end |
Class Method Details
.build_header(params = {}) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/rack/webmoney.rb', line 89 def self.build_header(params = {}) 'Webmoney ' + params.map { |key, value| if value.is_a?(Array) "#{key}=\"#{value.join(',')}\"" else "#{key}=\"#{value}\"" end }.join(', ') end |
.parse_header(str) ⇒ Object
Helper method for parsing “WWW-Authenticate” header values into a hash.
Rack::Webmoney.parse_header("Webmoney site_rid="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
#=> {:site_rid => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rack/webmoney.rb', line 104 def self.parse_header(str) params = {} if str =~ AUTHENTICATE_REGEXP str = str.gsub(/#{AUTHENTICATE_REGEXP}\s+/, '') str.split(', ').each { |pair| key, *value = pair.split('=') value = value.join('=') value.gsub!(/^\"/, '').gsub!(/\"$/, "") value = value.split(',') params[key] = value.length > 1 ? value : value.first } end params end |
Instance Method Details
#call(env) ⇒ Object
Standard Rack call
dispatch that accepts an env
and returns a [status, header, body]
response.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/rack/webmoney.rb', line 162 def call(env) req = Rack::Request.new(env) if req.params["WmLogin_WMID"] || req.params["WmLogin_KeeperRetStr"] complete_authentication(env) end status, headers, body = @app.call(env) qs = headers[AUTHENTICATE_HEADER] if status.to_i == 401 && qs && qs.match(AUTHENTICATE_REGEXP) begin_authentication(env, qs) else [status, headers, body] end end |