Class: SL::BitlySearch
- Inherits:
-
Object
- Object
- SL::BitlySearch
- Defined in:
- lib/searchlink/searches/shorteners/bitly.rb
Overview
Bit.ly link shortening
Class Method Summary collapse
Class Method Details
.search(_, search_terms, link_text) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/searchlink/searches/shorteners/bitly.rb', line 31 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) unless url SL.add_error("Result is not a valid URL", "URL error") return [false, title, link_text] end format_response(url, link, link_text) end |
.settings ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/searchlink/searches/shorteners/bitly.rb', line 7 def settings { trigger: "b(l|itly)", searches: [ ["bl", "bit.ly Shorten"], ["bitly", "bit.ly shorten"] ], config: [ { key: "bitly_access_token", value: "", required: true, description: "Generate an access token at https://app.bitly.com/settings/api/" }, { description: "Bit.ly domain (optional).", key: "bitly_domain", value: "bit.ly", required: false } ] } end |
.shorten(url) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/searchlink/searches/shorteners/bitly.rb', line 48 def shorten(url) return false unless bitly_config? domain = SL.config.key?("bitly_domain") ? SL.config["bitly_domain"] : "bit.ly" headers = { "Content-Type" => "application/json", "Authorization" => "Bearer #{SL.config['bitly_access_token']}" } data_obj = { "long_url" => url, "domain" => domain } data = Curl::Json.new("https://api-ssl.bitly.com/v4/shorten", data: data_obj.to_json, headers: headers, symbolize_names: true) return false unless data.json.key?(:link) link = data.json[:link] return false unless SL::URL.valid_link?(link) link end |