lib/omniauth/strategies/osm.rb in omniauth-osm-0.1.3 vs lib/omniauth/strategies/osm.rb in omniauth-osm-0.2.0
- old
+ new
@@ -1,55 +1,60 @@
require 'omniauth-oauth'
-require 'multi_json'
+require 'rexml/document'
module OmniAuth
module Strategies
class Osm < OmniAuth::Strategies::OAuth
option :name, "osm"
- option :client_options, {
- :site => 'http://www.openstreetmap.org',
- :request_token_url => 'http://www.openstreetmap.org/oauth/request_token',
- :access_token_url => 'http://www.openstreetmap.org/oauth/access_token',
- :authorize_url => 'http://www.openstreetmap.org/oauth/authorize'
- }
+ option :client_options, :site => 'http://www.openstreetmap.org'
- uid{ raw_info['user']['id'] }
+ uid { raw_info['id'] }
info do
- {
- 'name' => raw_info['user']['display_name'],
- 'languages' => languages,
- 'lat' => lat,
- 'lon' => lon,
- 'image_url' => image_url
- }
+ raw_info
end
- def lat
- raw_info['user']['home']['lat'].to_f rescue nil
- end
-
- def lon
- raw_info['user']['home']['lon'].to_f rescue nil
- end
-
- def languages
- raw_info['user']['languages']['lang'] rescue []
- end
-
- def image_url
- raw_info['user']['img']['href'] rescue nil
- end
-
- extra do
- { 'raw_info' => raw_info }
- end
-
def raw_info
- @raw_info ||= MultiJson.decode(access_token.get('/api/0.6/user/details.json').body)['osm']
+ @raw_info ||= parse_info(access_token.get('/api/0.6/user/details').body)
rescue ::Errno::ETIMEDOUT
raise ::Timeout::Error
+ end
+
+ # EXAMPLE XML
+ #
+ # <?xml version="1.0" encoding="UTF-8"?>
+ # <osm generator="OpenStreetMap server" version="0.6">
+ # <user display_name="freundchen" account_created="2011-01-07T14:35:24Z" id="392638">
+ # <img href='http://foo.bar.net/logo.gif' />
+ # <description>Test description</description>
+ # <contributor-terms pd="false" agreed="true"/>
+ # <home lon="13.411681556178" zoom="3" lat="52.524360979625"/>
+ # <languages>
+ # <lang>de-DE</lang>
+ # <lang>de</lang>
+ # </languages>
+ # </user>
+ # </osm>
+
+ private
+ def parse_info(xml_data)
+ # extract event information
+ doc = REXML::Document.new(xml_data)
+ user = doc.elements['//user']
+ home = doc.elements['//home']
+ languages = doc.get_elements('//lang')
+ image = doc.elements['//img']
+ description = doc.elements['//description']
+ basic_attributes = { }
+ basic_attributes['id'] = user.attribute('id').value
+ basic_attributes['display_name'] = user.attribute('display_name').value
+ basic_attributes['languages'] = languages.map(&:text) if languages
+ basic_attributes['image_url'] = image.attribute('href').value if image
+ basic_attributes['lat'] = home.attribute('lat').value.to_f if home
+ basic_attributes['lon'] = home.attribute('lon').value.to_f if home
+ basic_attributes['description'] = description.text if description
+ basic_attributes
end
end
end
end