Class: Tweetwine::UrlShortener

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ UrlShortener

Returns a new instance of UrlShortener.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tweetwine/url_shortener.rb', line 5

def initialize(options)
  raise 'UrlShortener should be disabled' if options[:disable]
  require 'nokogiri'
  @method = (options[:method] || :get).to_sym
  unless [:get, :post].include? @method
    raise CommandLineError, "Unsupported HTTP request method for URL shortening: #{@method}"
  end
  @service_url    = require_option options, :service_url
  @url_param_name = require_option options, :url_param_name
  @xpath_selector = require_option options, :xpath_selector
  @extra_params   = options[:extra_params] || {}
  if @method == :get
    tmp = []
    @extra_params.each_pair { |k, v| tmp << "#{k}=#{v}" }
    @extra_params = tmp
  end
end

Instance Method Details

#shorten(url) ⇒ Object



23
24
25
26
27
# File 'lib/tweetwine/url_shortener.rb', line 23

def shorten(url)
  response = CLI.http.send(@method, *get_service_url_and_params(url))
  doc = Nokogiri::HTML(response)
  doc.xpath(@xpath_selector).first.to_s
end