Module: Shortener::Short::ClassMethods

Included in:
Shortener, Shortener::Short
Defined in:
lib/shortener/short.rb

Instance Method Summary collapse

Instance Method Details

#delete(short, conf = nil) ⇒ Object

delete a short



42
43
44
45
# File 'lib/shortener/short.rb', line 42

def delete(short, conf = nil)
  response = request(:post, :delete, conf, {id: short})
  Short.new(response.body, conf)
end

#fetch(short, conf = nil) ⇒ Object

get data for a short, including full url.



24
25
26
27
# File 'lib/shortener/short.rb', line 24

def fetch(short, conf = nil)
  response = request(:get, :fetch, conf, short)
  Short.new(response.body, conf)
end

#index(start = 0, stop = nil, conf = nil) ⇒ Object

fetch data on multiple shorts



34
35
36
37
38
39
# File 'lib/shortener/short.rb', line 34

def index(start = 0, stop = nil, conf = nil)
  response = request(:get, :index, conf)
  data = JSON.parse(response.body)
  shorts = data.map {|sh| Short.new(sh, conf)}
  shorts
end

#request(type, end_point, conf = nil, args = nil) ⇒ Object

build a request based on configurations

Raises:



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/shortener/short.rb', line 48

def request(type, end_point, conf = nil, args = nil)
  config = conf || Shortener::Configuration.current
  response = case type
  when :post
    Net::HTTP.post_form(config.uri_for(end_point), args)
  when :get
    Net::HTTP.get_response(config.uri_for(end_point, args))
  end
  raise NetworkException.new(response.body) if response.kind_of?(Net::HTTPClientError)
  response
end

#shorten(url, conf = nil) ⇒ Object

set a shortened url



13
14
15
16
17
18
19
20
21
# File 'lib/shortener/short.rb', line 13

def shorten(url, conf = nil)
  opts = {"shortener[url]" => "#{url}"}
  response = request(:post, :add, conf, opts)
  if response.is_a?(Net::HTTPOK)
    return Short.new(response.body, conf)
  else
    raise "OH SHIT! #{response}"
  end
end

#upload(file) ⇒ Object

post a file to the configured s3 bucket and set a short.



30
31
# File 'lib/shortener/short.rb', line 30

def upload(file)
end