Module: Ym4r::Anonymizer::ClassMethods

Defined in:
lib/ym4r/anonymizer.rb

Instance Method Summary collapse

Instance Method Details

#google_api_keysObject



72
73
74
# File 'lib/ym4r/anonymizer.rb', line 72

def google_api_keys
  Ym4r::Anonymizer.config[:google_api_keys] || Array(Ym4r::GoogleMaps::API_KEY)
end

#open(url) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ym4r/anonymizer.rb', line 100

def open(url)
  url = url_with_next_key(url)
  puts "[#{Time.now}] fetching #{url}#{' with proxy ' + proxy_connection_string if use_proxy?}" if Ym4r::Anonymizer.verbos?
  if use_proxy?
    opts = if proxy_use_auth?
      {:proxy_http_basic_authentication => [proxy_connection_string, proxy_username, proxy_password]}
    else
      {:proxy => proxy_connection_string}
    end
    Kernel.open(url, opts)
  else
    Kernel.open(url)
  end
end

#proxyObject



48
49
50
# File 'lib/ym4r/anonymizer.rb', line 48

def proxy
  proxy = Ym4r::Anonymizer.config[:proxy]
end

#proxy_connection_stringObject



60
61
62
# File 'lib/ym4r/anonymizer.rb', line 60

def proxy_connection_string
  proxy ? "http://#{proxy[:host]}:#{proxy[:port]}" : nil
end

#proxy_passwordObject



68
69
70
# File 'lib/ym4r/anonymizer.rb', line 68

def proxy_password
  proxy ? proxy[:password] : nil
end

#proxy_use_auth?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/ym4r/anonymizer.rb', line 56

def proxy_use_auth?
  !!(proxy_username && proxy_password)
end

#proxy_usernameObject



64
65
66
# File 'lib/ym4r/anonymizer.rb', line 64

def proxy_username
  proxy ? proxy[:username] : nil
end

#url_with_next_key(url) ⇒ Object

iterate next API key



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ym4r/anonymizer.rb', line 81

def url_with_next_key(url)
  case self.name
    when "Ym4r::GoogleMaps::Geocoding"
      @@google_api_key_index ||= 0
      @@google_api_key_index = 0 if @@google_api_key_index >= google_api_keys.size
      key = google_api_keys[@@google_api_key_index]
      @@google_api_key_index += 1
      url.sub(/&key=(.*)&output=/, "&key=#{key}&output=")
    when "Ym4r::YahooMaps::BuildingBlock::Geocoding"
      @@yahoo_app_id_index ||= 0
      @@yahoo_app_id_index = 0 if @@yahoo_app_id_index >= yahoo_app_ids.size
      key = yahoo_app_ids[@@yahoo_app_id_index]
      @@yahoo_app_id_index += 1
      url.sub(/appid=(.+)&/, "appid=#{key}&")
    else
      raise "unknown Geocoder #{self.name} for anonymizing ym4r"
  end
end

#use_proxy?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/ym4r/anonymizer.rb', line 52

def use_proxy?
  !!proxy
end

#yahoo_app_idsObject



76
77
78
# File 'lib/ym4r/anonymizer.rb', line 76

def yahoo_app_ids
  Ym4r::Anonymizer.config[:yahoo_app_ids] || Array(Ym4r::YahooMaps::APP_ID)
end