Class: SL::SocialSearch

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

Class Method Summary collapse

Class Method Details

.search(search_type, search_terms, link_text = "") ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/searchlink/searches/social.rb', line 19

def search(search_type, search_terms, link_text = "")
  type = case search_type
    when /^@t/ # twitter-ify username
      unless search_terms.strip =~ /^@?[0-9a-z_$]+$/i
        return [false, "#{search_terms} is not a valid Twitter handle", link_text]
      end

      "t"
    when /^@fb?/ # fb-ify username
      return [false, "#{search_terms} is not a valid Facebook username", link_text] unless search_terms.strip =~ /^@?[0-9a-z_]+$/i

      "f"
    when /^@i/ # intagramify username
      return [false, "#{search_terms} is not a valid Instagram username", link_text] unless search_terms.strip =~ /^@?[0-9a-z_]+$/i

      "i"
    when /^@l/ # linked-inify username
      unless search_terms.strip =~ /^@?[0-9a-z_]+$/i
        return [false, "#{search_terms} is not a valid LinkedIn username", link_text]
      end

      "l"
    when /^@m/ # mastodonify username
      return [false, "#{search_terms} is not a valid Mastodon username", link_text] unless search_terms.strip =~ /^@?[0-9a-z_]+@[0-9a-z_.]+$/i

      "m"
    else
      "t"
    end

  url, title = social_handle(type, search_terms)
  link_text = title if link_text == ""
  [url, title, link_text]
end

.settingsObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/searchlink/searches/social.rb', line 6

def settings
  {
    trigger: "@[tfilm]",
    searches: [
      ["@t", "Twitter Handle"],
      ["@f", "Facebook Handle"],
      ["@i", "Instagram Handle"],
      ["@l", "LinkedIn Handle"],
      ["@m", "Mastodon Handle"],
    ],
  }
end

.social_handle(type, term) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/searchlink/searches/social.rb', line 64

def social_handle(type, term)
  handle = term.sub(/^@/, "").strip

  case type
  when /^t/i
    url = "https://twitter.com/#{handle}"
    title = template_social(handle, url, "Twitter")
  when /^f/i
    url = "https://www.facebook.com/#{handle}"
    title = template_social(handle, url, "Facebook")
  when /^l/i
    url = "https://www.linkedin.com/in/#{handle}/"
    title = template_social(handle, url, "LinkedIn")
  when /^i/i
    url = "https://www.instagram.com/#{handle}/"
    title = template_social(handle, url, "Instagram")
  when /^m/i
    parts = handle.split(/@/)
    return [false, term] unless parts.count == 2

    url = "https://#{parts[1]}/@#{parts[0]}"
    title = template_social(handle, url, "Mastodon")
  else
    [false, term]
  end

  [url, title]
end

.template_social(user, url, service) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/searchlink/searches/social.rb', line 54

def template_social(user, url, service)
  template = SL.config["social_template"].dup

  template.sub!(/%user%/, user)
  template.sub!(/%service%/, service)
  template.sub!(/%url%/, url.sub(%r{^https?://(www\.)?}, "").sub(%r{/$}, ""))

  template
end