Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional config parameter for user-agent header, alias for OSMGeocoder #195

Closed
wants to merge 3 commits into from

Conversation

chriso0710
Copy link
Contributor

Made the following changes regarding issue #194 :

  • new optional parameter Geokit::Geocoders::useragent
  • send user agent header for both net adapters
  • some small readme changes

Sorry for not providing any tests, but I was having some issues with

rake test

and as I am not familiar with vcr and simplecov, I was not very keen to debug the issue :-)

Additionally I created a new alias OsmGeocoder because of the following error in Multigeocoding with OSM:

E, [2016-10-27T17:37:51.610364 #9174] ERROR -- : Caught an error during Multi geocoding call: uninitialized constant Geokit::Geocoders::OsmGeocoder
E, [2016-10-27T17:37:51.610421 #9174] ERROR -- : /Users/co/projects/geokit/lib/geokit/multi_geocoder.rb:65:in `const_get'
/Users/co/projects/geokit/lib/geokit/multi_geocoder.rb:65:in `geocoder'
/Users/co/projects/geokit/lib/geokit/multi_geocoder.rb:32:in `block in do_geocode'
/Users/co/projects/geokit/lib/geokit/multi_geocoder.rb:31:in `each'
/Users/co/projects/geokit/lib/geokit/multi_geocoder.rb:31:in `do_geocode'
/Users/co/projects/geokit/lib/geokit/geocoders.rb:90:in `geocode'

…r because of class_name = "#{Geokit::Inflector.camelize(provider.to_s)}Geocoder"
@@ -2,7 +2,8 @@ module Geokit
module NetAdapter
class Typhoeus
def self.do_get(url)
::Typhoeus.get(url.to_s)
Geokit::Geocoders.useragent ? headers = {'User-Agent' => Geokit::Geocoders.useragent} : headers = {}
::Typhoeus.get(url.to_s, :headers => headers)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the new Ruby 1.9 hash syntax.

@@ -2,7 +2,8 @@ module Geokit
module NetAdapter
class Typhoeus
def self.do_get(url)
::Typhoeus.get(url.to_s)
Geokit::Geocoders.useragent ? headers = {'User-Agent' => Geokit::Geocoders.useragent} : headers = {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the return of the conditional for variable assignment and comparison.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you address this comment, e.g. headers = Geokit::Geocoders.useragent ? {'User-Agent' => Geokit::Geocoders.useragent} : {}

@@ -3,7 +3,8 @@ module NetAdapter
class NetHttp
def self.do_get(url)
uri = URI(url)
req = Net::HTTP::Get.new(uri.request_uri)
Geokit::Geocoders.useragent ? headers = {'User-Agent' => Geokit::Geocoders.useragent} : headers = {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the return of the conditional for variable assignment and comparison.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Trailing whitespace detected.

@coveralls
Copy link

coveralls commented Oct 27, 2016

Coverage Status

Coverage decreased (-0.07%) to 95.96% when pulling beb538a on chriso0710:master into ddd4130 on geokit:master.

@mnoack
Copy link
Member

mnoack commented Oct 27, 2016

I mostly like the Pull Request, I think it just needs a test that verifies if Net::HTTP receives the user agent.

e.g. mock 'Net::HTTP::Get' class
and see if that mock recevies 'new' with the URI and headers containing the agent. And of course a test that if user agent isn't set that it receives empty headers.

@chriso0710
Copy link
Contributor Author

Thanks, I will give it a try with webmock next week...

…aram, reverse_geocode accepts :provider_order param
def test_typhoeus_set_to_default
stub_request(:get, URL).with(:headers => TYPHOEUSDEFAULTHEADERS.merge('User-Agent' => TYPHOEUSDEFAULT))

Geokit::Geocoders::useragent = nil

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use :: for method calls.

end

def test_typhoeus_set_to_default
stub_request(:get, URL).with(:headers => TYPHOEUSDEFAULTHEADERS.merge('User-Agent' => TYPHOEUSDEFAULT))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 (not 4) spaces for indentation.
Use the new Ruby 1.9 hash syntax.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

def test_typhoeus_set_to_testagent
stub_request(:get, URL).with(:headers => TYPHOEUSDEFAULTHEADERS.merge('User-Agent' => TESTAGENT))

Geokit::Geocoders::useragent = TESTAGENT

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use :: for method calls.

end

def test_typhoeus_set_to_testagent
stub_request(:get, URL).with(:headers => TYPHOEUSDEFAULTHEADERS.merge('User-Agent' => TESTAGENT))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 (not 4) spaces for indentation.
Use the new Ruby 1.9 hash syntax.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

def test_nethttp_useragent_set_to_default
stub_request(:get, URL).with(:headers => NETHTTPDEFAULTHEADERS.merge('User-Agent' => NETHTTPDEFAULT))

Geokit::Geocoders::useragent = nil

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use :: for method calls.

end

def test_nethttp_useragent_set_to_default
stub_request(:get, URL).with(:headers => NETHTTPDEFAULTHEADERS.merge('User-Agent' => NETHTTPDEFAULT))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 (not 4) spaces for indentation.
Use the new Ruby 1.9 hash syntax.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

def test_nethttp_useragent_set_to_testagent
stub_request(:get, URL).with(:headers => NETHTTPDEFAULTHEADERS.merge('User-Agent' => TESTAGENT))

Geokit::Geocoders::useragent = TESTAGENT

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use :: for method calls.

URL = 'http://www.example.com'

def test_nethttp_useragent_set_to_testagent
stub_request(:get, URL).with(:headers => NETHTTPDEFAULTHEADERS.merge('User-Agent' => TESTAGENT))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 (not 4) spaces for indentation.
Use the new Ruby 1.9 hash syntax.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

TYPHOEUSDEFAULT = 'Typhoeus - https://github.com/typhoeus/typhoeus'
TYPHOEUSDEFAULTHEADERS = {'Expect'=>''}
TESTAGENT = 'MyAgent'
URL = 'http://www.example.com'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Freeze mutable objects assigned to constants.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

NETHTTPDEFAULTHEADERS = {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3'}
TYPHOEUSDEFAULT = 'Typhoeus - https://github.com/typhoeus/typhoeus'
TYPHOEUSDEFAULTHEADERS = {'Expect'=>''}
TESTAGENT = 'MyAgent'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Freeze mutable objects assigned to constants.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

NETHTTPDEFAULT = 'Ruby'
NETHTTPDEFAULTHEADERS = {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3'}
TYPHOEUSDEFAULT = 'Typhoeus - https://github.com/typhoeus/typhoeus'
TYPHOEUSDEFAULTHEADERS = {'Expect'=>''}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Freeze mutable objects assigned to constants.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Surrounding space missing for operator =>.


NETHTTPDEFAULT = 'Ruby'
NETHTTPDEFAULTHEADERS = {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3'}
TYPHOEUSDEFAULT = 'Typhoeus - https://github.com/typhoeus/typhoeus'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Freeze mutable objects assigned to constants.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

class UserAgentTest < Test::Unit::TestCase

NETHTTPDEFAULT = 'Ruby'
NETHTTPDEFAULTHEADERS = {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3'}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Freeze mutable objects assigned to constants.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Surrounding space missing for operator =>.


class UserAgentTest < Test::Unit::TestCase

NETHTTPDEFAULT = 'Ruby'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 (not 4) spaces for indentation.
Freeze mutable objects assigned to constants.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

require File.join(File.dirname(__FILE__), '../lib/geokit.rb')

class UserAgentTest < Test::Unit::TestCase

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at class body beginning.

require 'test/unit'
require 'webmock/test_unit'

require File.join(File.dirname(__FILE__), '../lib/geokit.rb')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

@coveralls
Copy link

coveralls commented Nov 11, 2016

Coverage Status

Coverage increased (+0.02%) to 96.04% when pulling 3bd27f3 on chriso0710:master into ddd4130 on geokit:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 96.04% when pulling 3bd27f3 on chriso0710:master into ddd4130 on geokit:master.

@chriso0710
Copy link
Contributor Author

Now here is my shot at webmock for testing the netadapters useragent. Hope it's ok this way. Please feel free to modify it.

I also fixed a bug with missing options param in bing geocoder, when using MultiGeocoder.
Added the :provider_order param in reverse_geocode. Same as in geocode.

@@ -110,4 +110,6 @@ def self.set_bounds(result_json, loc)
end
end
end

Geokit::Geocoders::OsmGeocoder = Geokit::Geocoders::OSMGeocoder
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you do this. I feel we're just adding another alias when people are already using OSM, if anything the class should reflect the file name (or file name reflect the class, e.g. OpenStreetMapGeocoder

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see my first comment. Multigeocode throws an error in const_get, reason is the wrong class name in

class_name = "#{Geokit::Inflector.camelize(provider.to_s)}Geocoder"
E, [2016-10-27T17:37:51.610364 #9174] ERROR -- : Caught an error during Multi geocoding call: uninitialized constant Geokit::Geocoders::OsmGeocoder
E, [2016-10-27T17:37:51.610421 #9174] ERROR -- : /Users/co/projects/geokit/lib/geokit/multi_geocoder.rb:65:in `const_get'
/Users/co/projects/geokit/lib/geokit/multi_geocoder.rb:65:in `geocoder'
/Users/co/projects/geokit/lib/geokit/multi_geocoder.rb:32:in `block in do_geocode'
/Users/co/projects/geokit/lib/geokit/multi_geocoder.rb:31:in `each'
/Users/co/projects/geokit/lib/geokit/multi_geocoder.rb:31:in `do_geocode'
/Users/co/projects/geokit/lib/geokit/geocoders.rb:90:in `geocode'

@coveralls
Copy link

coveralls commented Nov 20, 2016

Coverage Status

Coverage increased (+0.02%) to 96.04% when pulling d34386b on chriso0710:master into ddd4130 on geokit:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 96.04% when pulling d34386b on chriso0710:master into ddd4130 on geokit:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 96.04% when pulling d34386b on chriso0710:master into ddd4130 on geokit:master.

@mnoack mnoack mentioned this pull request Mar 26, 2017
@mnoack
Copy link
Member

mnoack commented Mar 26, 2017

@chriso0710 I've fixed tests in #202 so am closing this. Thanks very much for contributing to the fix.

@mnoack mnoack closed this Mar 26, 2017
@chriso0710
Copy link
Contributor Author

My pleasure, thanks for merging :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants