Sha256: 47472fdd1c25b4e746d0a9e7d7050f4e147d90ad9a29d382954bf72cf348e4f6

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

module AuthLh
  class Api
    def self.configure(args={})
      @endpoint = (args[:endpoint] || 'https://auth.lhconfort.com.ar')
      @return_url = args[:return_url]
      @application_code = args[:application_code]
      @access_token = args[:access_token]
    end

    def self.logged_user(session_token, remote_ip)
      result = get_request '/logged_user', {
        app_code: @application_code,
        session_token: session_token,
        remote_ip: remote_ip
      }

      SessionResponse.new(result)
    end

    def self.login_url
      login_attempt = create_login_attempt
      "#{@endpoint}/login?attempt=#{login_attempt.token}"
    end

    def self.logout_url
      "#{@endpoint}/logout?return=#{CGI::escape(@return_url)}"
    end

    def self.get_user(code_or_login)
      User.new(get_request("/api/users/#{code_or_login}"))
    end

    def self.get_users(filters={})
      results = get_request("/api/users", filters)
      results.map { |r| User.new(r) }
    end

    protected

    def self.create_login_attempt
      params = { app_code: @application_code }

      if @return_url
        params[:return_url] = @return_url
      end

      LoginAttempt.new(post_request('/login_attempts', params))
    end

    def self.get_request(action, params={})
      response = RestClient.get("#{@endpoint}#{action}", {params: params}.merge(auth_headers))
      JSON.parse(response.body)
    end

    def self.post_request(action, params={})
      JSON.parse(RestClient.post("#{@endpoint}#{action}", params, auth_headers))
    end

    def self.auth_headers
      { authorization: "Token token=\"#{@access_token}\"" }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
auth-lh-0.0.8 lib/auth_lh/api.rb