Class: RestPack::Web::Context
- Inherits:
-
Object
- Object
- RestPack::Web::Context
- Defined in:
- lib/restpack-web/context.rb
Instance Attribute Summary collapse
-
#application ⇒ Object
Returns the value of attribute application.
-
#channel ⇒ Object
Returns the value of attribute channel.
-
#configurations ⇒ Object
Returns the value of attribute configurations.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#request ⇒ Object
Returns the value of attribute request.
-
#services ⇒ Object
Returns the value of attribute services.
-
#user ⇒ Object
Returns the value of attribute user.
-
#user_id ⇒ Object
Returns the value of attribute user_id.
Instance Method Summary collapse
- #auth_domain ⇒ Object
-
#debug_info ⇒ Object
TODO: GJ: move to a mixin.
- #get_service(name) ⇒ Object
- #home_domain ⇒ Object
-
#initialize(env) ⇒ Context
constructor
A new instance of Context.
- #is_authenticated? ⇒ Boolean
- #login_url(provider = :twitter, next_url = nil) ⇒ Object
-
#logout_url(next_url = nil) ⇒ Object
TODO: GJ: whitelist the next_url.
- #root_domain ⇒ Object
Constructor Details
#initialize(env) ⇒ Context
Returns a new instance of Context.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/restpack-web/context.rb', line 8 def initialize(env) restpack = env[:restpack] if restpack @request = restpack[:request] @domain = restpack[:domain] @application = restpack[:application] @channel = restpack[:channel] @configurations = restpack[:configurations] @services = restpack[:services] @user = restpack[:user] @user_id = @user[:id] if @user end end |
Instance Attribute Details
#application ⇒ Object
Returns the value of attribute application.
6 7 8 |
# File 'lib/restpack-web/context.rb', line 6 def application @application end |
#channel ⇒ Object
Returns the value of attribute channel.
6 7 8 |
# File 'lib/restpack-web/context.rb', line 6 def channel @channel end |
#configurations ⇒ Object
Returns the value of attribute configurations.
6 7 8 |
# File 'lib/restpack-web/context.rb', line 6 def configurations @configurations end |
#domain ⇒ Object
Returns the value of attribute domain.
6 7 8 |
# File 'lib/restpack-web/context.rb', line 6 def domain @domain end |
#request ⇒ Object
Returns the value of attribute request.
6 7 8 |
# File 'lib/restpack-web/context.rb', line 6 def request @request end |
#services ⇒ Object
Returns the value of attribute services.
6 7 8 |
# File 'lib/restpack-web/context.rb', line 6 def services @services end |
#user ⇒ Object
Returns the value of attribute user.
6 7 8 |
# File 'lib/restpack-web/context.rb', line 6 def user @user end |
#user_id ⇒ Object
Returns the value of attribute user_id.
6 7 8 |
# File 'lib/restpack-web/context.rb', line 6 def user_id @user_id end |
Instance Method Details
#auth_domain ⇒ Object
35 36 37 |
# File 'lib/restpack-web/context.rb', line 35 def auth_domain @application.auth_domain || "auth.#{root_domain}" end |
#debug_info ⇒ Object
TODO: GJ: move to a mixin
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/restpack-web/context.rb', line 53 def debug_info #TODO: GJ: move to a mixin user_debug_info = "" if is_authenticated? user_debug_info = %{ * **id** : #{@user[:id]} * **name** : #{@user[:name]} * **nickname** : #{@user[:nickname]} * **location** : #{@user[:location]} * **description** : #{@user[:description]} * **avatar** : #{@user[:avatar]} ![Image](#{@user[:avatar]}) } end %{ ### RestPack Context: #### User: * **is authenticated**: #{is_authenticated?} #{user_debug_info} #### Channel: * **id**: #{@channel.id} * **name**: #{@channel.name} * **applications**: #{@channel.applications.map { |a| a.name }.join(', ')} #### Application: * **id**: #{@application.id} * **name**: #{@application.name} * **domains**: #{@application.domains.map { |a| a.host }.join(', ')} #### Domain: * **id**: #{@domain.id} * **host**: #{@domain.host} #### Configuration: * **keys**: #{@configurations.map { |c| c.key }.join(', ')} #### Authentication: * **logout**: #{logout_url} * **twitter oauth**: #{login_url(:twitter)} * **google oauth**: #{login_url(:google_oauth2)} } end |
#get_service(name) ⇒ Object
27 28 29 |
# File 'lib/restpack-web/context.rb', line 27 def get_service(name) @services.find { |s| s[:name] == name.to_s } end |
#home_domain ⇒ Object
31 32 33 |
# File 'lib/restpack-web/context.rb', line 31 def home_domain @application.home_domain || "www.#{root_domain}" end |
#is_authenticated? ⇒ Boolean
23 24 25 |
# File 'lib/restpack-web/context.rb', line 23 def is_authenticated? !@user.nil? end |
#login_url(provider = :twitter, next_url = nil) ⇒ Object
48 49 50 51 |
# File 'lib/restpack-web/context.rb', line 48 def login_url(provider = :twitter, next_url = nil) next_url ||= "http://#{home_domain}/" "http://#{auth_domain}/auth/#{provider}?next=#{next_url}" end |
#logout_url(next_url = nil) ⇒ Object
TODO: GJ: whitelist the next_url
43 44 45 46 |
# File 'lib/restpack-web/context.rb', line 43 def logout_url(next_url = nil) #TODO: GJ: whitelist the next_url next_url ||= "http://#{home_domain}/" "http://#{auth_domain}/auth/logout?next=#{next_url}" end |
#root_domain ⇒ Object
39 40 41 |
# File 'lib/restpack-web/context.rb', line 39 def root_domain PublicSuffix.parse(@domain.host).domain end |