Class: YahooGeminiClient::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



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

def initialize(options={})
  @consumer_key = options[:consumer_key]
  @consumer_secret = options[:consumer_secret]
  @oauth2_client ||= OAuth2::Client.new(consumer_key, consumer_secret, {
    :site => 'https://api.login.yahoo.com',
    :authorize_url => '/oauth2/request_auth',
    :token_url => '/oauth2/get_token',
  })
  @refresh_token = options[:refresh_token]
  @token = OAuth2::AccessToken.from_hash(
    @oauth2_client,
    refresh_token: @refresh_token,
  )
end

Instance Attribute Details

#consumer_keyObject

Returns the value of attribute consumer_key.



5
6
7
# File 'lib/yahoo_gemini_client/client.rb', line 5

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



5
6
7
# File 'lib/yahoo_gemini_client/client.rb', line 5

def consumer_secret
  @consumer_secret
end

#oauth2_clientObject

Returns the value of attribute oauth2_client.



5
6
7
# File 'lib/yahoo_gemini_client/client.rb', line 5

def oauth2_client
  @oauth2_client
end

Instance Method Details

#ad_groupsObject



85
86
87
# File 'lib/yahoo_gemini_client/client.rb', line 85

def ad_groups
  AdGroups.new(client: self)
end

#advertisersObject



65
66
67
# File 'lib/yahoo_gemini_client/client.rb', line 65

def advertisers
  Advertisers.new(client: self)
end

#api_request_headersObject



54
55
56
57
58
59
# File 'lib/yahoo_gemini_client/client.rb', line 54

def api_request_headers
  {
    "Authorization" => "Bearer #{token.token}",
    "User-Agent" => user_agent,
  }
end

#authorization_urlObject



27
28
29
30
31
32
33
# File 'lib/yahoo_gemini_client/client.rb', line 27

def authorization_url
  oauth2_client.auth_code.authorize_url(
    :redirect_uri => "oob",
    :response_type => "code",
    :language => "en-us",
  )
end

#campaignsObject



69
70
71
# File 'lib/yahoo_gemini_client/client.rb', line 69

def campaigns
  Campaigns.new(client: self)
end

#custom_reportObject



73
74
75
# File 'lib/yahoo_gemini_client/client.rb', line 73

def custom_report
  CustomReport.new(client: self)
end

#encoded_credsObject



35
36
37
# File 'lib/yahoo_gemini_client/client.rb', line 35

def encoded_creds
  Base64.encode64("#{consumer_key}:#{consumer_secret}").gsub(/\n/,"").strip
end

#get_token(authorization_code) ⇒ Object



39
40
41
42
43
44
# File 'lib/yahoo_gemini_client/client.rb', line 39

def get_token(authorization_code)
  self.token = oauth2_client.auth_code.get_token(authorization_code, {
    :redirect_uri => 'oob',
    :headers => oauth2_headers,
  })
end

#oauth2_headersObject



46
47
48
49
50
51
52
# File 'lib/yahoo_gemini_client/client.rb', line 46

def oauth2_headers
  {
    'Authorization' => "Basic #{encoded_creds}",
    'Content-Type' => 'application/x-www-form-urlencoded',
    'User-Agent' => user_agent,
  }
end

#token_refresh!Object



77
78
79
80
81
82
83
# File 'lib/yahoo_gemini_client/client.rb', line 77

def token_refresh!
  # TODO: handle when there's no token
  self.token = self.token.refresh!({
    :redirect_uri => 'oob',
    :headers => oauth2_headers
  })
end

#user_agentObject



61
62
63
# File 'lib/yahoo_gemini_client/client.rb', line 61

def user_agent
  @user_agent ||= "YahooGeminiClientRubyGem/#{YahooGeminiClient::VERSION}"
end