Module: TF2R::TextHelpers

Included in:
Raffle, Scraper
Defined in:
lib/tf2r/text_helpers.rb

Overview

This module provides common methods for manipulating text throughout this gem. Common operations include messing with Steam ID 64s, hex color codes, and link to resources on TF2R.

Instance Method Summary collapse

Instance Method Details

#extract_colour(str) ⇒ String

Extracts a hex colour code.

Examples:

extract_colour('color:#70B01B;') #=> '70b01b'

Parameters:

  • href (String)

    Any string containing a hex colour code.

Returns:

  • (String)

    The hex colour code, downcased.



13
14
15
# File 'lib/tf2r/text_helpers.rb', line 13

def extract_colour(str)
  /#(\w+)\s*;/.match(str)[1].downcase
end

Extract the link_snippet from a path or link.

Parameters:

  • any (String)

    raffle link or path.

Returns:

  • (String)

    the raffle’s link snippet.



21
22
23
# File 'lib/tf2r/text_helpers.rb', line 21

def extract_link_snippet(text)
  /\/(k.+)\.html/.match(text)[1]
end

#extract_steam_id(href) ⇒ Fixnum

Extracts a SteamID64 from a TF2R user link.

Examples:

extract_steam_id('http://tf2r.com/user/76561198061719848.html')
  #=> 76561198061719848

Parameters:

  • href (String)

    The full user profile link.

Returns:

  • (Fixnum)

    The Steam ID.



33
34
35
# File 'lib/tf2r/text_helpers.rb', line 33

def extract_steam_id(href)
  /http:\/\/tf2r.com\/user\/(\d+)\.html/.match(href)[1].to_i
end

Generates the TF2R link for a raffle given a link snippet.

Examples:

raffle_link('kabc123') => 'http://tf2r.com/kabc123.html'

Parameters:

  • link_snippet (String)

    the raffle link snippet.

Returns:

  • (String)

    the raffle link.



44
45
46
# File 'lib/tf2r/text_helpers.rb', line 44

def raffle_link(link_snippet)
  link = "http://tf2r.com/#{link_snippet}.html"
end

Examples:

raffle_link_full('kabc123') => 'http://tf2r.com/kabc123.html?full'

See Also:



52
53
54
# File 'lib/tf2r/text_helpers.rb', line 52

def raffle_link_full(link_snippet)
  "#{raffle_link(link_snippet)}?full"
end