Class: Sem4r::Credentials

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, client_email = nil) ⇒ Credentials

Returns a new instance of Credentials.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sem4r/credentials.rb', line 40

def initialize( opts, client_email = nil )
  case opts
  when Hash
    @environment=         opts[:environment].dup.freeze
    @email=               opts[:email].dup.freeze
    @password=            opts[:password].dup.freeze
    @useragent=           "Sem4r Adwords Ruby Client Library (http://github.com/sem4r/sem4r)"
    @developer_token=     opts[:developer_token].dup.freeze
    @mutable=             opts[:mutable] ? true : false
  when Credentials
    @credentials = opts
    @environment=         @credentials.environment
    @email=               @credentials.email
    @password=            @credentials.password
    @useragent=           @credentials.useragent
    @developer_token=     @credentials.developer_token
    @mutable=             @credentials.mutable?
  end

  if client_email
    @client_email = client_email.dup.freeze
  end
end

Instance Attribute Details

#client_emailObject (readonly)

Returns the value of attribute client_email.



32
33
34
# File 'lib/sem4r/credentials.rb', line 32

def client_email
  @client_email
end

#developer_tokenObject (readonly)

Returns the value of attribute developer_token.



33
34
35
# File 'lib/sem4r/credentials.rb', line 33

def developer_token
  @developer_token
end

#emailObject (readonly)

Returns the value of attribute email.



30
31
32
# File 'lib/sem4r/credentials.rb', line 30

def email
  @email
end

#environmentObject (readonly)

Returns the value of attribute environment.



29
30
31
# File 'lib/sem4r/credentials.rb', line 29

def environment
  @environment
end

#passwordObject (readonly)

Returns the value of attribute password.



31
32
33
# File 'lib/sem4r/credentials.rb', line 31

def password
  @password
end

#useragentObject (readonly)

Returns the value of attribute useragent.



34
35
36
# File 'lib/sem4r/credentials.rb', line 34

def useragent
  @useragent
end

Instance Method Details

#authentication_tokenObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sem4r/credentials.rb', line 76

def authentication_token
  return @authentication_token if @authentication_token
  return @authentication_token = @credentials.authentication_token if @credentials
  raise "no connector in credentials! use credentials.connector="  unless @connector

  url = "https://www.google.com/accounts/ClientLogin"
  query = "accountType=GOOGLE&Email=#{URI.escape(email)}&Passwd=#{URI.escape(password)}&service=adwords"
  headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
  response = @connector.post(url, query, headers )
  status = response.code.to_i
  if status != 200
    raise Sem4rError, "authentication failed status is #{status}"
  end
  @authentication_token = response.body[/Auth=(.*)/, 1]
end

#connector=(connector) ⇒ Object



72
73
74
# File 'lib/sem4r/credentials.rb', line 72

def connector=(connector)
  @connector= connector
end

#mutable?Boolean

Returns:

  • (Boolean)


36
# File 'lib/sem4r/credentials.rb', line 36

def mutable?;    @mutable; end

#production?Boolean

Returns:

  • (Boolean)


38
# File 'lib/sem4r/credentials.rb', line 38

def production?; !sandbox?; end

#sandbox?Boolean

Returns:

  • (Boolean)


37
# File 'lib/sem4r/credentials.rb', line 37

def sandbox?;    @environment != "production" end

#to_sObject



64
65
66
67
68
69
70
# File 'lib/sem4r/credentials.rb', line 64

def to_s
  if @client_email
    "client account: #{@email} - #{@client_email} (#{@mutable ? "mutable" : "read only"})"
  else
    "master account: #{@email} (#{@mutable ? "mutable" : "read only"})"
  end
end