Class: MainController

Inherits:
ApplicationController
  • Object
show all
Includes:
RailsDribbbleOauth
Defined in:
app/controllers/main_controller.rb

Constant Summary

Constants included from RailsDribbbleOauth

RailsDribbbleOauth::VERSION

Instance Method Summary collapse

Methods included from RailsDribbbleOauth

#clear_dribbble_user, #current_dribbble_user

Instance Method Details

#callbackObject



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
56
57
58
59
60
61
62
63
# File 'app/controllers/main_controller.rb', line 22

def callback
  # get back the code to make next API call
  params = {
    "client_id" => ENV["DRIBBBLE_CLIENT_ID"],
    "client_secret" => ENV["DRIBBBLE_CLIENT_SECRET"],
    "code" => request.env["QUERY_STRING"][5..-1],
    "button1" => "Submit"
  }

  # use code to request access token
  uri = URI.parse("https://dribbble.com/oauth/token")
  request = Net::HTTP.post_form(uri, params)
  token = JSON.parse(request.response.body)["access_token"]

  # use access token to get access to the API
  params = {"access_token" => token}
  uri = URI.parse("https://api.dribbble.com/v1/user")
  uri.query = URI.encode_www_form(params)
  response = Net::HTTP.get(uri)

  data = {
    success: true,
    status: "200 OK",
    message: nil,
    user_id: current_dribbble_user,
    user_data: nil
  }

  response = JSON.parse(response)

  if request.code != "200"
    # if there was some sort of failure
    data[:success] = false
    data[:status] = "204 No Content"
    data[:message] = "No user data returned from Dribbble. Reason provided by Dribbble: '#{response["message"]}'"
  else
    data[:user_data] = response
  end

  clear_dribbble_user
  redirect_to dribbble_information_path(data)
end

#oauth_requestObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/main_controller.rb', line 4

def oauth_request
  # sets current dribbble user session, cleared on callback
  # does not rely on main app to set a current_user
  id = request.env["QUERY_STRING"].split("=").last.to_i
  session[:current_dribbble_user] = id > 0 ? id : nil

  # redirect to https://dribbble.com/oauth/authorize
  dribbble = "https://dribbble.com/oauth/authorize"

  # in order for params to be built, you must have set environment variables:
  # get your DRIBBBLE_CLIENT_ID and DRIBBBLE_CLIENT_SECRET from
  # your Dribbble account directly: https://dribbble.com/account/applications/
  params = {"client_id" => ENV["DRIBBBLE_CLIENT_ID"]}

  # makes first request to Dribbble with your Client ID
  redirect_to "#{dribbble}?#{params.to_query}"
end