Class: App

Inherits:
Object
  • Object
show all
Defined in:
app/redis.rb,
app/helpers.rb,
app/routes/api.rb,
app/views/help.rb,
app/routes/book.rb,
app/routes/user.rb,
app/views/about.rb,
app/views/error.rb,
app/views/index.rb,
app/views/login.rb,
app/views/naked.rb,
app/views/rules.rb,
app/rake-methods.rb,
app/routes/place.rb,
app/views/citing.rb,
app/views/layout.rb,
app/authentication.rb,
app/views/book_new.rb,
app/routes/api/auth.rb,
app/routes/api/book.rb,
app/routes/api/user.rb,
app/routes/instance.rb,
app/views/book_show.rb,
app/views/place_new.rb,
app/views/user_show.rb,
app/routes/api/place.rb,
app/views/books_show.rb,
app/views/place_show.rb,
app/views/users_show.rb,
app/views/places_show.rb,
app/views/instance_new.rb,
app/routes/api/instance.rb,
app/routes/api/nickname.rb,
app/views/book_new_post.rb,
app/views/instance_edit.rb,
app/views/instance_show.rb,
app/views/instances_show.rb,
app/views/search_results.rb,
app/routes/authentication.rb,
app/views/modal_place_saved.rb

Defined Under Namespace

Modules: RakeMethods, ViewHelpers, Views

Instance Method Summary collapse

Instance Method Details

#admin_only_pageObject


83
84
85
86
87
88
# File 'app/authentication.rb', line 83

def admin_only_page
  unless @user.admin?
    flash[:error] = "Not allowed to visit admin-only pages."
    redirect "/"
  end
end

#downcase_special_field(params) ⇒ Object

METHODS


113
114
115
116
117
118
119
# File 'app/routes/instance.rb', line 113

def downcase_special_field(params)
  if params[:special]
    params[:special].downcase.gsub(/\s+$/, "")
  else
    nil
  end
end

#establish_userObject


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/authentication.rb', line 14

def establish_user  # puts "#{request.path_info}\nThe access_token is: #{session[:access_token]}\nThe user_id is: #{session[:user_id]}"

  if ENV['RACK_ENV'] == "development"
    User[1]
  else
    if session[:user_id].nil?      # No logged in user

      if session[:access_token].nil?        # No token so just punt.

        nil
      else
        # Token so go get info from GitHub
        access_token = session[:access_token]
        begin
          auth_result = RestClient.get('https://api.github.com/user',
                                       {:params => {:access_token => access_token},
                                        :accept => :json})
        rescue => e          # request didn't succeed because the token was revoked so we
          # invalidate the token stored in the session and render the
          # index page so that the user can start the OAuth flow again

          puts e.message
          session[:access_token] = nil
          flash[:error] = "The access token was revoked by GitHub; please login again."
          redirect '/'
        end
        auth_result = JSON.parse(auth_result)
        user = User.where(username: auth_result['login']).first
        if user.nil? || user.enabled == false
          session[:access_token] = nil
          flash[:error] = "You are not allowed to use this application. Let Moacir know if you think this is in error."
          redirect "/login"
        else
          session[:user_id] = user.id
        end
      end
    else
      # return the user
      User[session[:user_id]]
    end
  end
end

#get_access_token_from_GitHub(session_code) ⇒ Object


5
6
7
8
9
10
11
12
# File 'app/authentication.rb', line 5

def get_access_token_from_GitHub(session_code)
  result = RestClient.post('https://github.com/login/oauth/access_token',
                          {:client_id => ENV['GITHUB_CLIENT_ID'],
                           :client_secret => ENV['GITHUB_CLIENT_SECRET'],
                           :code => session_code},
                           :accept => :json)
  JSON.parse(result)['access_token']
end

#get_instance_place(place, instance = nil) ⇒ Object


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/routes/instance.rb', line 122

def get_instance_place(place, instance = nil)
  if params[:place].match(/{.*}$/)
    place = Place.where(name: params[:place].match(/{(.*?)}$/)[1]).first
    if place.nil?
      dm_error_and_redirect(instance, request.path, "No such place found. Please add it below.")
    else
      place
    end
  else
    if instance.nil? # coming in from create
      dm_error_and_redirect(instance, request.path, "The place did not have a name coded inside {}s")
    else # the place hasn't changed.
      instance.place
    end
  end
end

#make_point_geometry(lat, lon) ⇒ Object

METHODS


194
195
196
# File 'app/routes/place.rb', line 194

def make_point_geometry(lat, lon)
  GeoRuby::SimpleFeatures::Point.from_x_y(lon, lat, 4326)
end

#nicknames_list(newlist = nil) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/redis.rb', line 12

def nicknames_list(newlist = nil)
  nick_key = "user-#{@user.username}-nicknames-list"
  if newlist.nil?
    nicks = redis.cache(
      key: nick_key,
      expire: 1800,
      timeout: 60
    ) do
      Nickname.sorted_by_instance_count.map{ |n| { string: n.list_string, instance_count: n.instance_count } }.to_json
    end
  else
    redis.set nick_key, newlist.to_json
    nicks = redis.get nick_key
  end
  JSON.parse(nicks, symbolize_names: true).sort_by { |n| n[:instance_count] }.reverse
end

#password_change_body_text(name, user_profile_url) ⇒ Object


65
66
67
68
69
70
71
72
73
74
75
# File 'app/routes/user.rb', line 65

def password_change_body_text(name, )
  <<-EOF
  Dear #{name},

  Your password for NYWalker has been changed.

  You can visit your profile page here:

  #{}
  EOF
end

#permitted_page(object) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/authentication.rb', line 64

def permitted_page(object)
  if object.class == Book
    if object.users.include?(@user)
      edit_code = true
    end
  else
    if object.user == @user
      edit_code = true
    end
  end
  if !@user.nil? && @user.admin?
    edit_code = true
  end
  unless edit_code
    flash[:error] = "Not allowed to add or edit this data."
    redirect "/"
  end
end

#protected_pageObject


57
58
59
60
61
62
# File 'app/authentication.rb', line 57

def protected_page
  unless @user
    flash[:error] = "Must be logged in."
    redirect "/"
  end
end