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_color(str) ⇒ String

Extracts a hex color code.

Examples:

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

Parameters:

  • href (String)

    Any string containing a hex color code.

Returns:

  • (String)

    The hex color code, downcased.



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

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

Extract the link_snippet from a path or link.

Parameters:

  • text (String)

    any 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)
  "http://tf2r.com/#{link_snippet}.html"
end

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

Examples:

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

Parameters:

  • link_snippet (String)

    the raffle link snippet.

Returns:

  • (String)

    the full raffle link.

See Also:



57
58
59
# File 'lib/tf2r/text_helpers.rb', line 57

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

Generates the TF2R link for a user given a Steam ID.

Examples:

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

Parameters:

  • steam_id (Integer)

    the user’s Steam ID.

Returns:

  • (String)

    the user link.



69
70
71
# File 'lib/tf2r/text_helpers.rb', line 69

def user_link(steam_id)
  "http://tf2r.com/user/#{steam_id}.html"
end