Module: Shrinker

Defined in:
lib/shrinker.rb,
lib/shrinker/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.4"

Class Method Summary collapse

Class Method Details

.get_shrinker_result(long_url) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/shrinker.rb', line 23

def self.get_shrinker_result(long_url)
  Excon.post(ENV["SHRINKER_BASE_URL"],
                            path: "/api?key=#{ENV["SHRINKER_API_SECRET"]}",
                            body: JSON.generate("link" => long_url),
                            headers: { "Content-Type" => "application/json" }
                            )
end

.shorten_url(long_url) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/shrinker.rb', line 10

def self.shorten_url(long_url)
  uri?(long_url) #make sure long_url is url ArgumentError
  raise Error.new("Shrinker Base Url not set.") if ENV["SHRINKER_BASE_URL"].nil? # shrinker base url not set

  api_response = get_shrinker_result(long_url) 

  raise Error.new("Cannot Authenticate. Response Status: #{api_response.status}") unless api_response.status == 200 #env secret is correct  - cannot authenticate

  api_response.body
rescue Excon::Errors::SocketError
  raise Error.new('Shrinker not reachable') #shrinker reachable? - raise Shrinker::Error
end

.uri?(string) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
# File 'lib/shrinker.rb', line 31

def self.uri?(string)
  uri = URI.parse(string)
  %w( http https ).include?(uri.scheme)
rescue URI::BadURIError
  raise Error.new('ArgumentError: Bad URI')
rescue URI::InvalidURIError
  raise Error.new('ArgumentError: Invalid URI')
end