module CurateTumblr

Constants

CLIENT_STATUS_BAD_REQUEST
CLIENT_STATUS_NOT_AUTHORIZED
CLIENT_STATUS_NOT_FOUND
CLIENT_STATUS_OK
CLIENT_STATUS_RATE_LIMIT
COUNT_ALERT_IF_TOO_MUCH_BAD_REQUESTS
DISPLAY_SLEEP_AFTER
DISPLAY_SLEEP_BEFORE
DISPLAY_TUMBLR_DELETE
DISPLAY_TUMBLR_FOLLOW
DISPLAY_TUMBLR_GETPOSTS
DISPLAY_TUMBLR_POST
DISPLAY_TUMBLR_REBLOG
SANDBOX_POST_ID
STATE_DRAFT
STATE_PRIVATE
STATE_PUBLISHED
STATE_QUEUE

Public Class Methods

add_set_tofile_without_repeat( filename, set_tofile ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_file.rb, line 38
def self.add_set_tofile_without_repeat( filename, set_tofile )
  set_toadd = set_tofile.dup
  ::File.open( filename, "w" ) if !::File.exists?( filename )
  ar_file = get_ar_from_file( filename )
  ar_file.each { |line| format_tumblr_url!( line ) }
  set_toadd.merge( ar_file )
  ::File.open( filename, "w" ) { |file| file.puts( set_toadd.to_a )  }
  true
end
backup_file( filename, backupname="" ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_file.rb, line 31
def self.backup_file( filename, backupname="" )
  return false if !::File.exists?( filename )
  backupname = filename + ".save" if backupname.empty?
  ::File.open(backupname , "w+" ) { |file| file.puts get_ar_from_file( filename ) }
  true
end
checkFile( filename ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_file.rb, line 23
def self.checkFile( filename )
  raise "Filename is empty" if filename.empty?
  raise "File #{filename} doesn't exist" if !::File.exists?( filename )
  raise "#{filename} is not a true file" if !::File.file?( filename )
  raise "File #{filename} is not readable" if !::File.readable?( filename )
  true        
end
check_paths_ar_photos( ar_photos ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_validate.rb, line 33
def self.check_paths_ar_photos( ar_photos )
  ar_photos.each do |path|
    raise "path #{path} doesn't exist" if !File.exist?( path )
  end
end
copy_hash_key( hash_source, hash_target, key ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 103
def self.copy_hash_key( hash_source, hash_target, key )
  return false if !hash_source.has_key?( key )
  hash_target[key] = hash_source[key]
end
display_hash_multiple_posts( hash_posts, is_short=false ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 85
def self.display_hash_multiple_posts( hash_posts, is_short=false )
  raise "hash_posts #{hash_posts} is not valid" if !hash_multiple_posts_valid?( hash_posts )
  hash_posts['posts'].each { |hash_post| display_hash_post( hash_post, is_short ) }
end
display_hash_post( hash_post, is_short=false ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 74
def self.display_hash_post( hash_post, is_short=false )
raise "hash_post #{hash_post} is not valid" if !hash_post_valid?( hash_post )
title = "#{hash_post['type']} in #{hash_post['blog_name']} (#{hash_post['note_count']})"
title = title + " (source #{hash_post['source_url']})" if hash_post.has_key?( 'source_url' )
puts title
return if is_short
hash_post.each do |key, value|
  puts "> #{key} : #{value}"
end
end
follow( tumblr_name, directory, is_display_infos=true ) click to toggle source
# File lib/curate_tumblr/curator.rb, line 8
def self.follow( tumblr_name, directory, is_display_infos=true )
  Render::RenderLinks.render( Render::RenderFollow.new( tumblr_name, directory ), __method__, is_display_infos )
end
format_post_id( post_id ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_format.rb, line 84
def self.format_post_id( post_id )
  post_id.gsub('/', '').chomp.strip  
end
format_post_reblog_key( key ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_format.rb, line 88
def self.format_post_reblog_key( key )
  key.gsub('/', '').gsub('?', '').chomp.strip  
end
format_tumblr_url!( tumblr_url ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_format.rb, line 74
def self.format_tumblr_url!( tumblr_url )
  format_url!( tumblr_url )
  tumblr_url.gsub!("tumblr.com/", "tumblr.com")
  tumblr_url.gsub!("class=\"tumblr_blog\"", "")
  tumblr_url.gsub!("tumblr_blog href=", "")
  tumblr_url.gsub!("tumblr_blog", "")
  tumblr_url.gsub!("post", "")
  tumblr_url.gsub!("www.", "")
end
format_url!( url ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_format.rb, line 41
def self.format_url!( url )
  url.gsub!("target=\"_blank\"", "")
  url.gsub!("href=\"", "")
  url.gsub!("http://", "")
  url.gsub!("https://", "")
  url.gsub!("http%3A%2F%2F", "")
  url.gsub!("\"", "")
  url.gsub!('"', "")
  url.chomp!
  url.chomp!( " " )
  url.lstrip!
end
get_ar_from_file( filename ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_file.rb, line 18
def self.get_ar_from_file( filename )
  raise "filename is empty" if filename.nil? || filename.empty?
  ::File.open( filename, "r" ).readlines
end
get_filepath_errors_by_tumblr( tumblrname, path=PATH_LINKS ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_file.rb, line 6
def self.get_filepath_errors_by_tumblr( tumblrname, path=PATH_LINKS )
  path + tumblrname + "/" + tumblrname + "_errors"
end
get_filepath_tofollow_by_tumblr( tumblrname, path=PATH_LINKS ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_file.rb, line 10
def self.get_filepath_tofollow_by_tumblr( tumblrname, path=PATH_LINKS )
  path + tumblrname + "/" + tumblrname + "_tofollow"
end
get_format_ar_tumblr_urls( ar_urls ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_format.rb, line 64
def self.get_format_ar_tumblr_urls( ar_urls )
  raise "ar_urls is a #{ar_urls.class} instead of Array" if !ar_urls.is_a? Array
  ar_new_urls = []
  ar_urls.each do |url|        
    format_tumblr_url!( url )
    ar_new_urls << url
  end
  ar_new_urls
end
get_format_ar_tumblrs_urls( ar_tumblrs_urls ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_format.rb, line 3
def self.get_format_ar_tumblrs_urls( ar_tumblrs_urls )
  raise "ar_tumblrs_urls is a #{ar_tumblrs_urls.class} instead of Array" if !ar_tumblrs_urls.is_a? Array
  ar_new_tumblrs_urls = []
  ar_tumblrs_urls.each do |tumblr|
    format_tumblr_url!( tumblr )
    ar_new_tumblrs_urls << tumblr
  end
  ar_new_tumblrs_urls
end
get_format_ar_urls( ar_urls ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_format.rb, line 54
def self.get_format_ar_urls( ar_urls )
  raise "ar_urls is a #{ar_urls.class} instead of Array" if !ar_urls.is_a? Array
  ar_new_urls = []
  ar_urls.each do |url|        
    format_url!( url )
    ar_new_urls << url
  end
  ar_new_urls
end
get_format_tumblr_urls( tumblr_urls ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_format.rb, line 13
def self.get_format_tumblr_urls( tumblr_urls )
  if tumblr_urls.is_a? Set
    new_tumblr_urls = Set.new
  else
    new_tumblr_urls = []
  end
  tumblr_urls.each do |url|
    new_url = url.dup
    format_tumblr_url!( new_url )
    new_tumblr_urls << new_url
  end
  new_tumblr_urls
end
get_format_urls( urls ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_format.rb, line 27
def self.get_format_urls( urls )
  if urls.is_a? Set
    new_urls = Set.new
  else
    new_urls = []
  end
  urls.each do |url|
    new_url = url.dup
    format_url!( new_url )
    new_urls << new_url
  end
  new_urls
end
get_hash_url_from_post_url( post_url ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 14
def self.get_hash_url_from_post_url( post_url )
        return false if !Tumblr::ExtractLinks.tumblr_post_url?( post_url )
        { tumblr_url: Tumblr::ExtractLinks.get_tumblr_url( post_url ), post_id: Tumblr::ExtractLinks.get_post_id_from_post_url( post_url ) } 
end
get_hash_url_from_reblog_url( reblog_url ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 19
def self.get_hash_url_from_reblog_url( reblog_url )
        return false if !Tumblr::ExtractLinks.tumblr_reblog_url?( reblog_url )
        { tumblr_url: Tumblr::ExtractLinks.get_tumblr_from_reblog_url( reblog_url ), post_id: Tumblr::ExtractLinks.get_post_id_from_reblog_url( reblog_url ) } 
end
get_id_from_direct_post( hash_id ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 9
def self.get_id_from_direct_post( hash_id )
  return false if !hash_id_valid?( hash_id )
  hash_id['id']
end
get_link_url_from_hash_post( hash_post ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 28
def self.get_link_url_from_hash_post( hash_post )
  get_url_from_hash_post( "link_url", hash_post )
end
get_random_sleep( min, max ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 65
def self.get_random_sleep( min, max )
  raise "min is empty" if !min
  raise "max is empty" if !max
  raise "min (#{min} is >=  max (#{max})" if min >= max
  seconds = 1 + rand( max )
  seconds = min if seconds < min
  seconds
end
get_reblog_key_from_hash_post( hash_post ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 3
def self.get_reblog_key_from_hash_post( hash_post )
  return false if !hash_post_valid?( hash_post )
  return false if !hash_post.has_key?( 'reblog_key' )
  hash_post['reblog_key']
end
get_source_from_hash_post( hash_post ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 24
def self.get_source_from_hash_post( hash_post )
  get_url_from_hash_post( "source_url", hash_post )
end
get_summary_hash_post( hash_post ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 90
def self.get_summary_hash_post( hash_post )
  return hash_post if !hash_post_valid?( hash_post )
  summary = {}
  copy_hash_key( hash_post, summary, "blog_name")
  copy_hash_key( hash_post, summary, "id")
  copy_hash_key( hash_post, summary, "type")
  copy_hash_key( hash_post, summary, "post_url")
  copy_hash_key( hash_post, summary, "slug")
  copy_hash_key( hash_post, summary, "state")
  copy_hash_key( hash_post, summary, "reblog_key")
  summary
end
get_url_from_hash_post( url_key, hash_post ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 32
def self.get_url_from_hash_post( url_key, hash_post )
  return false if !hash_post_valid?( hash_post )
  return false if !hash_post.has_key?( url_key )
  #return false if !tumblr_url?( hash_post[url_key] )
  hash_post[url_key]
end
hash_id_valid?( hash_id ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_validate.rb, line 18
def self.hash_id_valid?( hash_id )
  return false if !hash_id.is_a? Hash
  return false if !hash_id.has_key?( 'id' )    
  return false if !post_id_valid?( hash_id['id'] )    
  true
end
hash_multiple_posts_valid?( hash_posts ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_validate.rb, line 2
def self.hash_multiple_posts_valid?( hash_posts )
  return false if hash_posts.nil? 
  return false if !hash_posts.is_a? Hash
  return false if !hash_posts.has_key?( 'posts' )
  return false if !hash_posts['posts'].is_a? Array
  true
end
hash_post_valid?( hash_post ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_validate.rb, line 10
def self.hash_post_valid?( hash_post )
  return false if !hash_post
  return false if !hash_post.is_a?( Hash )
  return false if !hash_post.has_key?( 'blog_name' )
  return false if !hash_post.has_key?( 'id' )
  true
end
hash_status?( hash_status, status ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 59
def self.hash_status?( hash_status, status )
  return true if !hash_status.has_key?( "status" )
  return true if hash_status["status"] == status
  false
end
hash_status_bad_request?( hash_status ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 51
def self.hash_status_bad_request?( hash_status )
  hash_status?( hash_status, CLIENT_STATUS_BAD_REQUEST )
end
hash_status_not_authorized?( hash_status ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 55
def self.hash_status_not_authorized?( hash_status )
  hash_status?( hash_status, CLIENT_STATUS_NOT_AUTHORIZED )
end
hash_status_not_found?( hash_status ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 43
def self.hash_status_not_found?( hash_status )
  hash_status?( hash_status, CLIENT_STATUS_NOT_FOUND )
end
hash_status_ok?( hash_status ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 39
def self.hash_status_ok?( hash_status )
  hash_status?( hash_status, CLIENT_STATUS_OK )
end
hash_status_rate_limit?( hash_status ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_client.rb, line 47
def self.hash_status_rate_limit?( hash_status )
  hash_status?( hash_status, CLIENT_STATUS_RATE_LIMIT )
end
hash_url_valid?( hash_url ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_validate.rb, line 25
def self.hash_url_valid?( hash_url )
  return false if !hash_url.is_a? Hash
  return false if !hash_url.has_key?( :tumblr_url )    
  return false if !hash_url.has_key?( :post_id )
  return false if !post_id_valid?( hash_url[:post_id] )    
  true    
end
post_id_valid?( post_id ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_validate.rb, line 39
def self.post_id_valid?( post_id )
  return false if post_id.nil?
  return false if !post_id
  return false if post_id.is_a?( String ) && post_id.empty?
  return false if post_id.is_a?( Integer ) && post_id.to_s.empty?
  true
end
reblog( tumblr_name, directory, is_display_infos=true ) click to toggle source
# File lib/curate_tumblr/curator.rb, line 4
def self.reblog( tumblr_name, directory, is_display_infos=true )
  Render::RenderLinks.render( Render::RenderReblog.new( tumblr_name, directory ), __method__, is_display_infos )
end
reblog_key_valid?( reblog_key ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_validate.rb, line 47
def self.reblog_key_valid?( reblog_key )
  return false if reblog_key.nil?
  return false if !reblog_key
  return false if !reblog_key.is_a? String
  return false if reblog_key.empty?
  true
end

Public Instance Methods

create_config_file( filename, hash_config ) click to toggle source
# File lib/curate_tumblr/utilities/utilities_file.rb, line 48
def create_config_file( filename, hash_config )
end