Sha256: e0c4fdb3e7e8e950aed60f940cbc7639f321bf229c398671c5813732d3362e0e

Contents?: true

Size: 1015 Bytes

Versions: 1

Compression:

Stored size: 1015 Bytes

Contents

require File.join(File.dirname(__FILE__), '..', 'sapo.rb')
require 'rexml/document'
require 'net/https'

module SAPO
  class ID
    attr_accessor :email, :password, :primary, :token
    
    def initialize(email, password)
      self.email = email
      self.password = password
      
      uri = URI.parse("https://services.sapo.pt/STS/GetPrimaryId?ESBUsername=#{self.email}&ESBPassword=#{self.password}")
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
      http.ca_file = File.join(File.dirname(__FILE__), "cacert.pem")
      http.start {
        http.request_get(uri.path+"?"+uri.query) {|res|
            output = REXML::Document.new(res.body)
            begin
              self.primary = output.root.elements['PrimaryId'].text
              self.token = output.root.elements['ESBToken'].text
            rescue
              raise "Wrong credentials"
              return nil
            end
          }
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tpinto-sapo-rb-0.0.3 lib/sapo/id.rb