Class: WikipediaWrapper::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/wikipedia_wrapper/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(term, redirect: true) ⇒ Page

Returns a new instance of Page.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wikipedia_wrapper/page.rb', line 11

def initialize(term, redirect: true)

  @term = term
  @redirect = redirect
  @images = nil
  @img_width = WikipediaWrapper.config.img_width
  @img_height = WikipediaWrapper.config.img_height

  # FIXME: Deal with continuation

  # FIXME: deal with redirect false!
  load_page

end

Instance Attribute Details

#extractObject (readonly)

Returns the value of attribute extract.



9
10
11
# File 'lib/wikipedia_wrapper/page.rb', line 9

def extract
  @extract
end

#revision_timeObject (readonly)

Returns the value of attribute revision_time.



9
10
11
# File 'lib/wikipedia_wrapper/page.rb', line 9

def revision_time
  @revision_time
end

#titleObject (readonly)

Returns the value of attribute title.



9
10
11
# File 'lib/wikipedia_wrapper/page.rb', line 9

def title
  @title
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/wikipedia_wrapper/page.rb', line 9

def url
  @url
end

Instance Method Details

#categoriesObject



30
31
32
# File 'lib/wikipedia_wrapper/page.rb', line 30

def categories
  # FIXME: Implement!
end

#images(width: nil, height: nil) ⇒ Array<WikipediaWrapper::WikiImage>

Note:

Only one of width and height can be used at the same time. If both are defined, only width is used.

Retrieve image info for all given image filenames, except for the images in the whitelist See http://www.mediawiki.org/wiki/API:Imageinfo

Parameters:

  • width (Integer) (defaults to: nil)

    optional width of the smaller image (in px)

  • height (Integer) (defaults to: nil)

    optional height of the smaller image (in px)

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
92
93
94
95
96
97
98
99
100
101
# File 'lib/wikipedia_wrapper/page.rb', line 41

def images(width: nil, height: nil)

  # if we haven't retrieved any images or the width or height have changed, re-fetch
  if @images.nil? || (!width.nil? && @img_width != width) || (!width.nil? && @img_width != width)

    unless width.nil?
      @img_width = width
    end

    unless height.nil?
      @img_height = height
    end

    @images = []

    # deal with the case that a page has no images
    if @raw['images'].nil?
      return @images
    end

    filenames = @raw['images'].map {|img_info| img_info['title']}.compact

    if filenames.empty? # there are no filenames, return an empty array
      return @images
    end

    # exclude whitelisted filenames
    filenames = filenames.map { |f| WikipediaWrapper.config.image_allowed?(f) ? f : nil }.compact

    query_parameters = {
      'titles': filenames.join('|'),
      'redirects': '',
      'prop': 'imageinfo',
      'iiprop': 'url|size|mime',
    }

    if (!@img_width.nil?)
      query_parameters[:iiurlwidth] = @img_width.to_s
    elsif (!@img_height.nil?)
      query_parameters[:iiurlheight] = @img_height.to_s
    end

    raw_results = WikipediaWrapper.fetch(query_parameters)

    # check if the proper format is there
    if raw_results.key?('query') && raw_results['query'].key?('pages')
      raw_results['query']['pages'].each do |k, main_info|
        begin
          wi = WikiImage.new(main_info)
          @images.push(wi)
        rescue WikipediaWrapper::FormatError => e
          puts e.message
        end
      end
    end

  end

  return @images

end

#to_sObject



26
27
28
# File 'lib/wikipedia_wrapper/page.rb', line 26

def to_s
  "Wikipedia Page: #{@title} (#{@url}), revid: #{@revision_id}, revtime: #{@revision_time}"
end