Class: SL::TinyurlSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/searchlink/searches/shorteners/tinyurl.rb

Overview

is.gd link shortening

Class Method Summary collapse

Class Method Details

.format_response(link, original_url, link_text) ⇒ Object



65
66
67
68
# File 'lib/searchlink/searches/shorteners/tinyurl.rb', line 65

def format_response(link, original_url, link_text)
  rtitle = SL::URL.title(original_url)
  [link, rtitle, link_text == "" && !SL.titleize ? rtitle : link_text]
end

.search(_, search_terms, link_text) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/searchlink/searches/shorteners/tinyurl.rb', line 24

def search(_, search_terms, link_text)
  if SL::URL.url?(search_terms)
    link = search_terms
  else
    link, title, link_text = SL.ddg(search_terms, link_text)
  end

  url = shorten(link)
  title = SL::URL.title(link) if title.nil? || title.empty?
  link_text = title if (link_text.nil? || link_text.empty?) && !SL.titleize
  format_response(url, link, link_text)
end

.settingsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/searchlink/searches/shorteners/tinyurl.rb', line 7

def settings
  {
    trigger: "tiny",
    searches: [
      ["tiny", "TinyURL Shorten"]
    ],
    config: [
      {
        key: "tinyurl_access_token",
        value: "",
        required: true,
        description: "Generate a tinyurl API key at https://tinyurl.ph/developers (login required)"
      }
    ]
  }
end

.shorten(url) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/searchlink/searches/shorteners/tinyurl.rb', line 37

def shorten(url)
  return false unless tinyurl_config?

  headers = {
    "Content-Type" => "application/json",
    "Authorization" => "Bearer #{SL.config['tinyurl_access_token']}"
  }
  data_obj = {
    "url" => url
  }
  data = Curl::Json.new("https://tinyurl.ph/api/url/add", data: data_obj.to_json, headers: headers,
                                                          symbolize_names: true)

  if data.json[:error].positive?
    SL.add_error("Error creating tinyurl", data.json[:error])
    return false
  end

  data.json[:shorturl]
end

.tinyurl_config?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
# File 'lib/searchlink/searches/shorteners/tinyurl.rb', line 58

def tinyurl_config?
  return true if SL.config["tinyurl_access_token"] && !SL.config["tinyurl_access_token"].empty?

  SL.add_error("TinyURL not configured", "Missing access token")
  false
end