Class: Oauth

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

Direct Known Subclasses

OauthFb, OauthGg, OauthIm, OauthMr, OauthVk, OauthYx

Instance Method Summary collapse

Constructor Details

#initialize(verify_url, state) ⇒ Oauth

Returns a new instance of Oauth.



6
7
8
9
10
# File 'lib/oauth/oauth.rb', line 6

def initialize verify_url, state
  @verify_url = verify_url
  @key = state.keys.first
  @name = state.values.first[:name].to_sym
end

Instance Method Details

#auth_request(step = :step1, add_params = nil) ⇒ Object



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

def auth_request step = :step1, add_params = nil
  uri = URI CONFIG[:oauth][@name][:auth][step][:uri]
  params = CONFIG[:oauth][@name][:auth][:united_params].merge(CONFIG[:oauth][@name][:auth][step].except(:uri)).merge({redirect_uri: @verify_url, state: @key})
  # для второго шага
  params.merge!(add_params) unless add_params.nil?
  uri.query = URI.encode_www_form params

  case step
    when :step1
      uri.to_s
    when :step2
      oauth_access_card_params Net::HTTP.get_response(uri)
  end

end

#error_response(error_message) ⇒ Object



65
66
67
68
69
70
# File 'lib/oauth/oauth.rb', line 65

def error_response error_message
  {
      state: :shit,
      error: error_message
  }
end

#get_user_info(access_params) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/oauth/oauth.rb', line 55

def  access_params
  uri = URI "#{CONFIG[:oauth][@name][:api][:uri]}#{CONFIG[:oauth][@name][:api][:users_get][:prefix]}"
  params = CONFIG[:oauth][@name][:api][:united_params].merge!(CONFIG[:oauth][@name][:api][:users_get][:params])
  params.merge!(access_params) unless access_params.nil?
  uri.query = URI.encode_www_form params
  result = Net::HTTP.get_response uri

  ActiveSupport::JSON.decode(result.body)['response'][0]
end

#oauth_access_card_params(result) ⇒ Object



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
# File 'lib/oauth/oauth.rb', line 28

def oauth_access_card_params result
  begin
    params = ActiveSupport::JSON.decode(result.body)
    unless params['error'].present?
      add_info = ({access_token: params['access_token']})
      {
        state: :ok,
        oauth_name: @name,
        oauth_uid: params['user_id'],
        access_token: params['access_token'],
        token_expired: (DateTime.now + params['expires_in'].second).to_s(:db),
        photourl: add_info['photo_100']
      }
    else
      {
          state: :shit,
          error: "#{params['error']}:#{params['error_description']}"
      }
    end
  rescue Exception => error
    {
        state: :shit,
        error: error.message
    }
  end
end