Class: ExpandUrl

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ ExpandUrl

Returns a new instance of ExpandUrl.



10
11
12
13
14
# File 'lib/expandurl.rb', line 10

def initialize(url)
  self.url = url
  self.user_agent = 'ExpandUrlGem/1.0'
  self.service = find_service
end

Instance Attribute Details

#serviceObject

Returns the value of attribute service.



8
9
10
# File 'lib/expandurl.rb', line 8

def service
  @service
end

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/expandurl.rb', line 8

def url
  @url
end

#user_agentObject

Returns the value of attribute user_agent.



8
9
10
# File 'lib/expandurl.rb', line 8

def user_agent
  @user_agent
end

Instance Method Details

#expandObject



25
26
27
28
29
30
31
32
33
# File 'lib/expandurl.rb', line 25

def expand
  if self.service
    [1,2].shuffle.each do |api|
      url = expand_by_api(api)
      return url if url
    end
  end
  return false 
end

#find_serviceObject



16
17
18
19
20
21
22
23
# File 'lib/expandurl.rb', line 16

def find_service
  host = Addressable::URI.parse(self.url).host
  services = YAML::load_file(File.join(File.dirname(File.expand_path(__FILE__)), 'services.yml'))
  services.each do |service|
    return host if service == host
  end
  return false
end