Class: Wikiwhat::Media

Inherits:
Results show all
Defined in:
lib/wikiwhat/parse.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Results

#content_split, #pull_from_hash

Constructor Details

#initialize(api_return, prop) ⇒ Media

Returns a new instance of Media.


157
158
159
160
161
162
# File 'lib/wikiwhat/parse.rb', line 157

def initialize(api_return, prop)
  @request = self.pull_from_hash(api_return, prop)
  if @request.class == Array
    @request = self.pull_from_hash(@request[0], "*")
  end
end

Instance Attribute Details

#api_returnObject (readonly)

Returns the value of attribute api_return.


156
157
158
# File 'lib/wikiwhat/parse.rb', line 156

def api_return
  @api_return
end

Instance Method Details

#list_imagesObject

Find all the media items on a page.

Return a hash containing an array of urls and an array of titles.


167
168
169
170
171
172
173
174
175
176
177
# File 'lib/wikiwhat/parse.rb', line 167

def list_images
  # Call API for initial list of images
  isolated_list = @request
  # Parse JSON object for list of image titles
  image_array = []
  isolated_list.collect do |key, value|
    image_array << {"title" => value["title"], "url" => value["fullurl"] }
  end

  return image_array
end

Find the image from the sidebar, if one exists

Return the url of the thumbnail image as a String.


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/wikiwhat/parse.rb', line 182

def sidebar_image_thumbnail(options={})
  # Check to see if a sidebar image exists
  if self.content_split(0)[/(image).*?(\.\w\w(g|G|f|F))/]
    # Grab the sidebar image title
    image_name = self.content_split(0)[/(image).*?(\.\w\w(g|G|f|F))/]
    # Remove the 'image = ' part of the string
    image_name = image_name.split("=")[1].strip
    # Call Wikipedia for image url
    get_url = Wikiwhat::Call.call_api(('File:'+ image_name), 
      :prop => "imageinfo", 
      :iiprop => true, 
      :iiurlwidth => options[:width], 
      :iiurlheight => options[:height] )
    # Pull url from hash
    img_name_2 = pull_from_hash(get_url, "pages")
    img_array = pull_from_hash(img_name_2, "imageinfo")
    img_array[0]["thumburl"]
  else
    # If no sidebar image exists, raise error.
    raise Wikiwhat::WikiwhatError.new("Sorry, it looks like there is no sidebar image
      on this page.")
  end
end