Skip to content

Instantly share code, notes, and snippets.

@Thermatix
Created June 17, 2016 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thermatix/d8d40439b89fa160dbcf4acaeaa8b5e6 to your computer and use it in GitHub Desktop.
Save Thermatix/d8d40439b89fa160dbcf4acaeaa8b5e6 to your computer and use it in GitHub Desktop.
class NptArticle < Sequel::Model
clear_all
set_schema {
primary_key :id
integer :npt_site_id, :null => false
text :title_submitted, :null => false, :default => ''
varchar :url_submitted, :null => false, :default => '', :size => 1024
varchar :url_indexed, :null => false, :default => '', :size => 1024
integer :date_submitted, :null => false, :default => 0
integer :date_discovered, :null => false, :default => 0
text :title_headline, :null => false, :default => ''
text :title_html, :null => false, :default => ''
varchar :id_html, :null => false, :default => ''
varchar :date_html, :null => false, :default => ''
text :description_html, :null => false, :default => ''
varchar :channel_html, :null => false, :default => ''
varchar :subchannel_html, :null => false, :default => ''
varchar :author_html, :null => false, :default => ''
integer :comments_html, :null => false, :default => 0 # currently also stores api comments. Need to be renamed to 'comments' for general comment storage.
integer :http_status_code, :null => false, :default => 0
varchar :url_canonical, :null => false, :default => '', :size => 1024
varchar :url_redirect, :null => false, :default => '', :size => 1024
text :title_indexed, :null => false, :default => ''
text :description_indexed, :null => false, :default => ''
integer :date_indexed, :null => false, :default => 0
varchar :google_indexed, :null => false, :default => "?"
varchar :fb_shares, :null => false, :default => "?"
varchar :fb_likes, :null => false, :default => "?"
varchar :fb_comments, :null => false, :default => "?"
varchar :fb_commentsbox, :null => false, :default => "?"
varchar :fb_total, :null => false, :default => "?"
varchar :fb_clicks, :null => false, :default => "?"
varchar :id_opengraph, :null => false, :default => ''
varchar :tweets, :null => false, :default => "?"
integer :ga_entrances, :null => false, :default => 0
integer :ga_unique_visitors, :null => false, :default => 0
integer :ga_exit_rate, :null => false, :default => 0
integer :ga_page_views, :null => false, :default => 0
integer :ga_mobile_page_views, :null => false, :default => 0
varchar :ga_time_on_page, :null => false, :default => "-"
# boolean :ga_visitor_success, :null => false, :default => false
# boolean :ga_entrances_success, :null => false, :default => false
varchar :latest_google_news_error_detail, :null => false, :default => ""
integer :npt_author_id, :null => false, :default => 0
boolean :found_in_analytics, :null => false, :default => false
text :social_data, :null => false, :default => ""
varchar :img_count, :null => false, :default => "?"
varchar :video_count, :null => false, :default => "?"
varchar :content_type, :null => false, :default => "?"
varchar :embed_count, :null => false, :default => "?"
varchar :word_count, :null => false, :default => "?"
varchar :int_link_count, :null => false, :default => "?"
varchar :ext_link_count, :null => false, :default => "?"
varchar :tweets_per_hour, :null => false, :default => "?"
varchar :likes_per_hour, :null => false, :default => "?"
integer :last_twitter_check, :null => false, :default => 0
integer :last_fb_check, :null => false, :default => 0
integer :omni_page_views, :null => false, :default => 0
integer :omni_mobile_views, :null => false, :default => 0
float :omni_bounce_rate, :null => false, :default => 0
float :omni_time_on_page, :null => false, :default => 0
integer :omni_entries, :null => false, :default => 0
integer :omni_bounces, :null => false, :default => 0
integer :omni_unique_visitors, :null => false, :default => 0
boolean :found_in_omniture, :null => false, :default => false
# boolean :omni_entries_success, :null => false, :default => false
# boolean :omni_visitor_success, :null => false, :default => false
varchar :gnews_content_category, :null => false, :default => ""
varchar :gnews_image, :null => false, :default => ""
integer :success, :null => false, :default => 0
varchar :og_image, :null => false, :default => ""
varchar :google_plus_one, :null => false, :default => "?"
varchar :pinterest, :null => false, :default => "?"
varchar :linked_in, :null => false, :default => "?"
index :url_submitted
index :url_indexed
index :npt_site_id
index :date_discovered
index :gnews_content_category
index :latest_google_news_error_detail
#index :title_html
index :channel_html
index :subchannel_html
index :author_html
#index :http_status_code
}
create_table unless table_exists?
validates {
uniqueness_of :url_submitted, :url_indexed
# uniqueness_of [:npt_site_id, :url_submitted], :message => "There is such record."
# uniqueness_of [:npt_site_id, :url_indexed], :message => "There is such record."
}
def validate
super
filter = id ? " AND id <> #{id}" : ""
# errors.add(:url_submitted, "Duplicate!") if NptArticle.filter("CONCAT(url_submitted,url_indexed) = '#{url_submitted.to_s+url_indexed.to_s}'#{filter}").first
errors.add(:url_submitted, 'url_indexed and url_submitted cannot be both blank') if url_submitted.blank? && url_indexed.blank?
errors.add(:url_indexed, "Duplicate!") if url_submitted.blank? && NptArticle.filter("url_submitted = '#{url_indexed.to_s}'#{filter}").first
errors.add(:url_submitted, "Duplicate!") if url_indexed.blank? && NptArticle.filter("url_indexed = '#{url_submitted.to_s}'#{filter}").first
end
def get_social_data
self[:social_data]
end
def total_social
@total_social ||= self[:comments_html].to_i + self[:fb_total].to_i + self[:google_plus_one].to_i + self[:pinterest].to_i + self[:linked_in].to_i
end
def main_title
if !title_submitted.blank?
title_submitted
elsif !title_indexed.blank?
title_indexed
elsif !title_headline.blank?
title_headline
else
title_html
end
end
def has_google_image?
gnews_image == "Article Image" || gnews_image == "Competitor Image"
end
def success_in_words
if success == 2
"Outstanding"
elsif success == 1
"Successful"
elsif success == 0
"Unsuccessful"
else
"Not checked"
end
end
def main_url
if !url_indexed.blank?
url_indexed
elsif !url_canonical.blank?
url_canonical
elsif !url_submitted.blank?
url_submitted
else
url_redirect
end
end
def time_to_index
if !url_submitted.blank? && date_submitted != 0
time = date_indexed - date_discovered#Time.at(date_submitted).strftime("%T") == "00:00:00" ? date_indexed - date_discovered : date_indexed - date_submitted
return time <= 0 ? 0 : time.to_f/60
else
return 0
end
end
def urls_redirecting_from
return NptArticle.filter(:url_redirect => URI.parse(main_url).request_uri).to_a
end
def urls_redirecting_to
if !url_redirect.blank?
article = NptArticle.select.filter("url_submitted like '%#{url_redirect}%' or url_indexed like '%#{url_redirect}%'").to_a.first
return article ? article : "not_in_db"
else
return false
end
end
before_destroy {
self.npt_articles_npt_channels.each {|r| r.destroy}
self.npt_articles_npt_social_profiles.each {|r| r.destroy}
}
# before_save {
# self[:domain] = self[:domain].to_s.gsub(/[\r\n]+/, "\n").gsub(/^https?:\/\/|\/$/, "").gsub(/\/\S*\s*/, "")
# }
belongs_to :npt_site
belongs_to :npt_author
many_to_many :npt_channels
has_many :npt_articles_npt_channels
has_and_belongs_to_many(:npt_channels,
:join_table => :npt_articles_npt_channels,
:class => 'NptChannel',
:left_key => :npt_article_id,
:right_key => :npt_channel_id)
many_to_many_by_ids :npt_channels, 'NptArticlesNptChannel'
many_to_many :npt_social_profile
has_many :npt_articles_npt_social_profiles
has_and_belongs_to_many(:npt_social_profiles,
:join_table => :npt_articles_npt_social_profiles,
:class => 'NptSocialProfile',
:left_key => :npt_article_id,
:right_key => :npt_social_profile_id)
many_to_many_by_ids :npt_social_profiles, 'NptArticlesNptSocialProfile'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment