Module: Hipku

Defined in:
lib/hipku.rb,
lib/hipku/version.rb,
lib/hipku/dictionary.rb

Defined Under Namespace

Modules: Dictionary

Constant Summary collapse

IPV4_OCTET_COUNT =
4
IPV6_OCTET_COUNT =
8
IPV6_PADDING_OCTECT =
'0'.freeze
IPV4_DIVISOR =
16
IPV6_DIVISOR =
256
DIVISORS =
{
  ipv4: IPV4_DIVISOR,
  ipv6: IPV6_DIVISOR,
}.freeze
PLACEHOLDER_OCTET =
'OCTET'
NEW_LINE =
"\n".freeze
PERIOD =
'.'.freeze
SPACE =
' '.freeze
FILLER_WORDS =
%w[in the and].freeze
NON_WORDS =
[NEW_LINE, PERIOD, SPACE].freeze
IPV4_HAIKU_STRUCTURE =
[
  Dictionary::ANIMAL_ADJECTIVES,
  Dictionary::ANIMAL_COLORS,
  Dictionary::ANIMAL_NOUNS,
  Dictionary::ANIMAL_VERBS,
  Dictionary::NATURE_ADJECTIVES,
  Dictionary::NATURE_NOUNS,
  Dictionary::PLANT_NOUNS,
  Dictionary::PLANT_VERBS,
].freeze
IPV6_HAIKU_STRUCTURE =
[
  Dictionary::ADJECTIVES,
  Dictionary::NOUNS,
  Dictionary::ADJECTIVES,
  Dictionary::NOUNS,
  Dictionary::VERBS,
  Dictionary::ADJECTIVES,
  Dictionary::ADJECTIVES,
  Dictionary::ADJECTIVES,
  Dictionary::ADJECTIVES,
  Dictionary::ADJECTIVES,
  Dictionary::NOUNS,
  Dictionary::ADJECTIVES,
  Dictionary::NOUNS,
  Dictionary::VERBS,
  Dictionary::ADJECTIVES,
  Dictionary::NOUNS,
].freeze
HAIKU_STRUCTURE =
{
  ipv4: IPV4_HAIKU_STRUCTURE,
  ipv6: IPV6_HAIKU_STRUCTURE,
}.freeze
IPV4_SCHEMA =
[
  'The',
  PLACEHOLDER_OCTET,
  PLACEHOLDER_OCTET,
  PLACEHOLDER_OCTET,
  NEW_LINE,
  PLACEHOLDER_OCTET,
  'in the',
  PLACEHOLDER_OCTET,
  PLACEHOLDER_OCTET,
  PERIOD,
  NEW_LINE,
  PLACEHOLDER_OCTET,
  PLACEHOLDER_OCTET,
  PERIOD,
  NEW_LINE,
].freeze
IPV6_SCHEMA =
[
  PLACEHOLDER_OCTET,
  PLACEHOLDER_OCTET,
  'and',
  PLACEHOLDER_OCTET,
  PLACEHOLDER_OCTET,
  NEW_LINE,
  PLACEHOLDER_OCTET,
  PLACEHOLDER_OCTET,
  PLACEHOLDER_OCTET,
  PLACEHOLDER_OCTET,
  PLACEHOLDER_OCTET,
  PLACEHOLDER_OCTET,
  PLACEHOLDER_OCTET,
  PERIOD,
  NEW_LINE,
  PLACEHOLDER_OCTET,
  PLACEHOLDER_OCTET,
  PLACEHOLDER_OCTET,
  PLACEHOLDER_OCTET,
  PLACEHOLDER_OCTET,
  PERIOD,
  NEW_LINE,
].freeze
SCHEMA =
{
  ipv4: IPV4_SCHEMA,
  ipv6: IPV6_SCHEMA,
}.freeze
VERSION =
'1.1.2'

Class Method Summary collapse

Class Method Details

.decode(haiku) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/hipku.rb', line 126

def decode(haiku)
  words = to_word_array(haiku)
  @version = haiku_version(words)
  factors = to_factors(words)

  if @version == :ipv6
    octets = factors
    # convert from decimal to hexidecimal octets
    octets.map! {|octet| base_convert(octet, 10, 16)}
    octets.map! {|octet| octet.size < 2 ? octet.prepend('0') : octet}
    ip = join_ipv6(factors)
  elsif @version == :ipv4
    octets = to_octets(factors)
    ip = join_ipv4(octets)
  end

  ip
end

.encode(ip) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/hipku.rb', line 108

def encode(ip)
  @version = ip_version(ip)
  ip = ip.gsub(/[[:space:]]/, '')
  if @version == :ipv6
    octets = split_ipv6(ip)
    # convert from hexidecimal to decimal octets
    octets.map! {|octet| base_convert(octet, 16, 10)}
  elsif @version == :ipv4
    octets = split_ipv4(ip)
  end

  factored_octets = factor_octets(octets, DIVISORS[@version])

  words = encode_words(factored_octets, HAIKU_STRUCTURE[@version])

  write_haiku(words)
end