Module: Madvertise

Defined in:
lib/madvertise.rb

Constant Summary collapse

VERSION =
'1.0.1'
DEFAULT_TIMEOUT =
1.0

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.serverObject

Returns the value of attribute server.



15
16
17
# File 'lib/madvertise.rb', line 15

def server
  @server
end

.site_tokenObject

Returns the value of attribute site_token.



15
16
17
# File 'lib/madvertise.rb', line 15

def site_token
  @site_token
end

Class Method Details

.config {|Madvertise| ... } ⇒ Object

Yields:



18
19
20
# File 'lib/madvertise.rb', line 18

def self.config
  yield Madvertise
end

.get_ad(request, cookies) ⇒ Object



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

def self.get_ad(request, cookies)
  user_agent = request.env['HTTP_USER_AGENT']
  ip = request.env['REMOTE_ADDR']

  user_token = cookies['madvertise']
  puts "User Tokken from cookie = #{user_token}"  
  serv = self.server || 'http://ad.madvertise.de/site/'
  endpoint = URI.parse("#{serv}#{self.site_token}")
  req = Net::HTTP::Post.new(endpoint.path)
  req.set_form_data({
    'ua' => user_agent || '',
    'ip' => ip || '',
    'uu' => user_token || ''
      })
  conn = Net::HTTP.new(endpoint.host, endpoint.port)
  conn.read_timeout = DEFAULT_TIMEOUT
  conn.open_timeout = DEFAULT_TIMEOUT
  contents = ''
  begin
    response = conn.start{|http| http.request(req)}
    contents = response.body
    puts "Response Cookie = #{response.get_fields('set-cookie').first}"
    new_cookie = {
      :value => response.get_fields('set-cookie').first[11..-1],
      :expires => Time.now + 7.years
    }
    cookies['madvertise'] = new_cookie
  rescue Timeout::Error => te
  rescue
  end
  return contents
end