ruby-on-railsmongoidfeedjira

How to check the extension type then save in the proper location


I have model with pictures and videos

How to check the enclosure_type then save in the proper location?

feed_entry.rb

class FeedEntry
  include Mongoid::Document

  field :enclosure_url, type: String
  field :enclosure_type, type: String
  field :video, type: String
  mount_uploader :picture, PictureUploader
end

For example:

feed = Feedjira::Feed.fetch_and_parse 'http://example.com/feed.xml'

feed.entries.first.enclosure_url # => "http://example.com/d260eb5c"
feed.entries.first.enclosure_type # => "image/jpeg"

feed.entries.last.enclosure_url # => 'https://youtu.be/AnXR-MKhg64'
feed.entries.last.enclosure_type # => "video/x-ms-asf"

Solution

  • before_save  :save_uri
    
    def media_type
        if self.enclosure_type =~ /image\//
          self.remote_picture_url = self.enclosure_url
        elsif self.enclosure_type =~ /video\//
          self.video = self.enclosure_url
        else
          self.video = self.enclosure_url
        end
      end