Class: Hull::Paywall

Inherits:
Object
  • Object
show all
Defined in:
lib/hull/paywall.rb

Defined Under Namespace

Classes: Request

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Paywall

Returns a new instance of Paywall.



71
72
73
74
75
76
77
78
79
80
# File 'lib/hull/paywall.rb', line 71

def initialize app, options={}
  @app      = app
  @options  = options
  Hull.configure do |config|
    config.app_id      = @options[:app_id]      || ENV['HULL_APP_ID']
    config.app_secret  = @options[:app_secret]  || ENV['HULL_APP_SECRET']
    config.org_url     = @options[:org_url]    || ENV['HULL_ORG_URL']
  end
  @paths = @options[:paths].map { |k,v| [Regexp.new(k), v] }
end

Instance Method Details

#call(env) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/hull/paywall.rb', line 115

def call env
  rule = content_rule_for(env['PATH_INFO'])

  if rule
    secure_content(env, rule)
  else
    @app.call(env)
  end
end

#content_rule_for(path) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/hull/paywall.rb', line 105

def content_rule_for(path)
  ret = false
  @paths.map do |p|
    match, rule = p
    break if ret
    ret = rule if match && match =~ path
  end
  ret
end

#secure_content(env, rule) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hull/paywall.rb', line 82

def secure_content env, rule
  paywall = Hull::Paywall::Request.new(env, Hull.app_secret)
  if paywall.check_authorization_for(rule[:permalink])
    status, headers, body = @app.call(env)
    paywall.set_cookie(headers)
    [status, headers, body]
  else
    status  = 302
    redirect_to = rule[:redirect] || '/'
    headers = { 'Content-Type' => 'text/html' }
    body = ''
    if env['PATH_INFO'] != redirect_to
      headers['Location'] = redirect_to
    else
      status  = 401
      body    = "Access Denied..."
    end
    paywall.set_cookie(headers)
    [status, headers, [body]]
  end
  
end