Class: YTAnalytics::OAuth2Client

Inherits:
Client
  • Object
show all
Defined in:
lib/yt_analytics/client.rb

Instance Method Summary collapse

Methods inherited from Client

#analytics, #day_totals, #day_watch_time, #demographic_percentages, #enable_http_debugging, #month_totals, #seven_day_totals, #thirty_day_totals

Methods included from Logging

#logger

Constructor Details

#initialize(options) ⇒ OAuth2Client

Returns a new instance of OAuth2Client.



79
80
81
82
83
84
85
86
87
# File 'lib/yt_analytics/client.rb', line 79

def initialize(options)
  @client_id = options[:client_id]
  @client_secret = options[:client_secret]
  @client_access_token = options[:client_access_token]
  @client_refresh_token = options[:client_refresh_token]
  @client_token_expires_at = options[:client_token_expires_at]
  @dev_key = options[:dev_key]
  @legacy_debug_flag = options[:debug]
end

Instance Method Details

#access_tokenObject



98
99
100
# File 'lib/yt_analytics/client.rb', line 98

def access_token
  @access_token ||= ::OAuth2::AccessToken.new(oauth_client, @client_access_token, :refresh_token => @client_refresh_token, :expires_at => @client_token_expires_at)
end

#current_userObject



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/yt_analytics/client.rb', line 117

def current_user
  profile = access_token.get("http://gdata.youtube.com/feeds/api/users/default")
  response_code = profile.status

  if response_code/10 == 20 # success
    Nokogiri::XML(profile.body).at("entry/author/name").text
  elsif response_code == 403 || response_code == 401 # auth failure
    raise YTAnalytics::YTAuth::AuthenticationError.new(profile.inspect, response_code)
  else
    raise YTAnalytics::YTAuth::UploadError.new(profile.inspect, response_code)
  end
end

#oauth_clientObject



89
90
91
92
93
94
95
96
# File 'lib/yt_analytics/client.rb', line 89

def oauth_client
  options = {:site => "https://accounts.google.com",
    :authorize_url => '/o/oauth2/auth',
    :token_url => '/o/oauth2/token'
   }
  options.merge(:connection_opts => @connection_opts) if @connection_opts
  @oauth_client ||= ::OAuth2::Client.new(@client_id, @client_secret, options)
end

#refresh_access_token!Object



102
103
104
105
106
107
108
109
110
# File 'lib/yt_analytics/client.rb', line 102

def refresh_access_token!
  new_access_token = access_token.refresh!
  require 'thread' unless Thread.respond_to?(:exclusive)
  Thread.exclusive do
    @access_token = new_access_token
    @client = nil
  end
  @access_token
end

#session_token_infoObject



112
113
114
115
# File 'lib/yt_analytics/client.rb', line 112

def session_token_info
  response = Faraday.get("https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=#{@client_access_token}")
  {:code => response.status, :body => response.body }
end

#user_idObject



130
131
132
133
# File 'lib/yt_analytics/client.rb', line 130

def user_id
  profile ||= access_token.get("http://gdata.youtube.com/feeds/api/users/default?v=2&alt=json").parsed
  profile['entry']['author'][0]["yt$userId"]["$t"]
end