Class: RestPack::Web::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/restpack-web/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#applicationObject

Returns the value of attribute application.



6
7
8
# File 'lib/restpack-web/context.rb', line 6

def application
  @application
end

#channelObject

Returns the value of attribute channel.



6
7
8
# File 'lib/restpack-web/context.rb', line 6

def channel
  @channel
end

#configurationsObject

Returns the value of attribute configurations.



6
7
8
# File 'lib/restpack-web/context.rb', line 6

def configurations
  @configurations
end

#domainObject

Returns the value of attribute domain.



6
7
8
# File 'lib/restpack-web/context.rb', line 6

def domain
  @domain
end

#requestObject

Returns the value of attribute request.



6
7
8
# File 'lib/restpack-web/context.rb', line 6

def request
  @request
end

#servicesObject

Returns the value of attribute services.



6
7
8
# File 'lib/restpack-web/context.rb', line 6

def services
  @services
end

#userObject

Returns the value of attribute user.



6
7
8
# File 'lib/restpack-web/context.rb', line 6

def user
  @user
end

#user_idObject

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_domainObject



35
36
37
# File 'lib/restpack-web/context.rb', line 35

def auth_domain
  @application.auth_domain || "auth.#{root_domain}"
end

#debug_infoObject

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**: #{(:twitter)}
 * **google oauth**: #{(: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_domainObject



31
32
33
# File 'lib/restpack-web/context.rb', line 31

def home_domain
  @application.home_domain || "www.#{root_domain}"
end

#is_authenticated?Boolean

Returns:

  • (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 (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_domainObject



39
40
41
# File 'lib/restpack-web/context.rb', line 39

def root_domain
  PublicSuffix.parse(@domain.host).domain
end