Class: GoiuShorten::GoiuShortenApi

Inherits:
Object
  • Object
show all
Defined in:
lib/goiu_shorten/goiu_shorten_api.rb

Instance Method Summary collapse

Constructor Details

#initializeGoiuShortenApi

Returns a new instance of GoiuShortenApi.



12
13
14
15
16
# File 'lib/goiu_shorten/goiu_shorten_api.rb', line 12

def initialize
  @client = Savon::Client.new do
    wsdl.document = 'https://go.iu.edu/GoIUWebService.asmx?WSDL'
  end
end

Instance Method Details

#expand(*short_url) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/goiu_shorten/goiu_shorten_api.rb', line 18

def expand(*short_url)
  if short_url.nil? then raise ArgumentError end
  api_response = @client.request :decode do
    soap.body = {'tinyURL' => short_url}
  end
  result = api_response.to_hash[:decode_response][:decode_result]
  result
end

#shorten(long_url, passcode) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/goiu_shorten/goiu_shorten_api.rb', line 27

def shorten(long_url, passcode)
  unless (long_url =~ /^[a-z]{2,5}:\/\//)
    raise ArgumentError
    exit
  end
  unless passcode =~ /^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/
    raise ArgumentError, "You must supply a passcode to shorten URLs."
    exit
  end
  api_response = @client.request :encode do
    soap.body = {'passCode' => passcode, 'longURL' => long_url}
  end
  result = api_response.to_hash[:encode_response][:encode_result]
  # This tests for an error response about a bad, expired, invalid passcode
  if (result =~ /^\*\*ERR\*\*\sPasscode/) 
    raise ArgumentError, "Not a valid passcode." 
  end
  result
end