Sha256: da9cd42a06a212e9123c997e321c672c228b13197be56203792295c9c23e494d

Contents?: true

Size: 1.38 KB

Versions: 9

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module ApiUserAuth
  module Providers
    # Get info from Google token
    class Google
      API_PATH = 'https://www.googleapis.com/plus/v1/people/me'.freeze

      def initialize(token)
        @token = token
        @data = {}
      end

      def api_info_url
        params = {
          access_token: @token
        }
        URI("#{API_PATH}?#{params.to_query}")
      end

      def get_user_data
        api_get_request
        user_data
      end

      def user_data
        {
          id: @data[:id], name: @data[:displayName],
          email: @data[:emails].first.try(:[], :value), provider: 'google',
          img_url: (@data[:image] || {}).try(:[], :url),
          info: {
            birthday: @data[:birthday],
            city: (@data[:hometown] || {}).try(:[], :name),
            gender: @data[:gender]
          }
        }
      end

      def self.get_user(token)
        g = Google.new(token)
        g.get_user_data
      end

      private

      def api_get_request
        response = ::Net::HTTP.get_response(api_info_url)
        case response.code.to_i
        when 200
          @data = JSON.parse(response.body, symbolize_names: true)
        when 400
          raise ApiUserAuth::Exceptions::InvalidToken, 'Invalid Token'
        else
          raise ApiUserAuth::Exceptions::ProviderError, 'Provider Error'
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
api_user_auth-0.1.9 lib/api_user_auth/providers/google.rb
api_user_auth-0.1.8 lib/api_user_auth/providers/google.rb
api_user_auth-0.1.7 lib/api_user_auth/providers/google.rb
api_user_auth-0.1.6 lib/api_user_auth/providers/google.rb
api_user_auth-0.1.5 lib/api_user_auth/providers/google.rb
api_user_auth-0.1.4 lib/api_user_auth/providers/google.rb
api_user_auth-0.1.2 lib/api_user_auth/providers/google.rb
api_user_auth-0.1.1 lib/api_user_auth/providers/google.rb
api_user_auth-0.1.0 lib/api_user_auth/providers/google.rb