Class: WhatIs::ThisIs::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/whatis/thisis/link.rb

Overview

Represents link to some entity that can be resolved to proper entity definition.

You should never create instances of this class directly, it occurs as variant links from Ambigous, and as ThisIs::languages links.

Examples:

# Ambigous variants link
a = WhatIs.this('Bela Crkva')
# => #<ThisIs::Ambigous Bela Crkva (6 options)>
a.variants[0]
# => #<ThisIs::Link Bela Crkva, Banat>
a.variants[0].resolve
# => #<ThisIs Bela Crkva, Banat [img] {44.897500,21.416944}>

# Languages link
paris = WhatIs.this('Paris', languages: :ru)
# => #<ThisIs Paris/Париж, [img] {48.856700,2.350800}>
paris.languages
# => {"ru"=>#<ThisIs::Link ru:Париж>}
paris.languages['ru'].resolve(categories: true)
# => #<ThisIs Париж, 10 categories [img] {48.833333,2.333333}>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, section: nil, owner: nil, language: nil, description: nil) ⇒ Link

Returns a new instance of Link.



38
39
40
41
42
43
44
# File 'lib/whatis/thisis/link.rb', line 38

def initialize(title, section: nil, owner: nil, language: nil, description: nil)
  @owner = owner
  @title = title
  @language = language&.to_s
  @section = section unless section == ''
  @description = description
end

Instance Attribute Details

#descriptionObject (readonly)



35
36
37
# File 'lib/whatis/thisis/link.rb', line 35

def description
  @description
end

#languageObject (readonly)



31
32
33
# File 'lib/whatis/thisis/link.rb', line 31

def language
  @language
end

#sectionObject (readonly)



35
36
37
# File 'lib/whatis/thisis/link.rb', line 35

def section
  @section
end

#titleString (readonly) Also known as: to_s

Returns:

  • (String)


29
30
31
# File 'lib/whatis/thisis/link.rb', line 29

def title
  @title
end

Instance Method Details

#==(other) ⇒ Object



70
71
72
# File 'lib/whatis/thisis/link.rb', line 70

def ==(other)
  other.is_a?(Link) && other.language == language && other.title == title
end

#inspectString

Returns:

  • (String)


47
48
49
# File 'lib/whatis/thisis/link.rb', line 47

def inspect
  "#<ThisIs::Link #{language&.append(':')}#{section&.append('/')}#{title}>"
end

#resolve(**options) ⇒ ThisIs, ThisIs::Ambigous

Resolves the link, fetching entity from Wikipedia API.

See WhatIs#this for options explanation.

Parameters:

  • options (Hash)

Options Hash (**options):

  • :languages (true, String, Symbol)
  • :categories (true, false)

Returns:



61
62
63
64
65
66
# File 'lib/whatis/thisis/link.rb', line 61

def resolve(**options)
  engine = @owner || language && WhatIs[language] or
    fail "Can't resolve #{inspect}"

  engine.this(title, **options)
end