Class: Site

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reddilicous) {|_self| ... } ⇒ Site

Returns a new instance of Site.

Yields:

  • (_self)

Yield Parameters:

  • _self (Site)

    the object that the method was called on



9
10
11
12
13
14
15
# File 'lib/site.rb', line 9

def initialize(reddilicous)
  @gourmand = reddilicous
  @dir = File.join(reddilicous.dir, name)
  @ids = Set.new
  Dir.mkdir(@dir) if !File.exists? @dir
  yield(self) if block_given?
end

Instance Attribute Details

#postsObject

Returns the value of attribute posts.



7
8
9
# File 'lib/site.rb', line 7

def posts
  @posts
end

Instance Method Details

#ask_for_credentialsObject



76
77
78
79
# File 'lib/site.rb', line 76

def ask_for_credentials
  puts "#{name} user name:"
  self.credentials = {"username" => STDIN.gets.strip}    
end

#balanceObject



30
31
32
33
# File 'lib/site.rb', line 30

def balance 
  @ids.merge(posts.map{|p| identifier(p)})
  posts.sort!{|x, y| date(y) <=> date(x)}
end

#credentialsObject



22
23
24
# File 'lib/site.rb', line 22

def credentials
  @credentials ||= if File.exists? credentials_file then JSON.parse(IO.read(credentials_file)) else {} end
end

#credentials=(credentials) ⇒ Object



17
18
19
20
# File 'lib/site.rb', line 17

def credentials=(credentials)
  @credentials = credentials
  File.open(credentials_file, "w"){|o| o.puts JSON.pretty_generate(@credentials)}
end

#date(post) ⇒ Object

Not required to return an actual date, only something which compares in the correct order to be read as such



50
51
52
# File 'lib/site.rb', line 50

def date(post)
  post["dt"]
end

#identifier(post) ⇒ Object



44
45
46
# File 'lib/site.rb', line 44

def identifier(post)
  post["url"]
end


58
59
60
# File 'lib/site.rb', line 58

def imported_links
  Delicious.get("/posts/all", :query=>{:tag=>["via:#{name}", Post::NEW_MARKER].join(' ')})['posts']['post'] || []
end

#save!Object



35
36
37
38
# File 'lib/site.rb', line 35

def save!
  balance
  File.open(posts_file, "w"){|o| o.puts JSON.pretty_generate(@posts)} if @posts
end

#to_post(data) ⇒ Object



40
41
42
# File 'lib/site.rb', line 40

def to_post(data)
  Post.new(data)
end

#to_sObject



54
55
56
# File 'lib/site.rb', line 54

def to_s
  name
end

#undo_import!Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/site.rb', line 62

def undo_import!
  posts = imported_links
  posts = [posts] if posts.is_a?(Hash) 
  puts "undo imports for #{self}" unless posts.empty?
  
  posts.each do |p|
    puts "deleting #{p['description']}"
    res = Delicious.delete('/posts/delete', :query=>{:url=>p['href']})
    if !res['result'] || res['result']['code'] != 'done'
      puts "delete failed: #{res.inspect}"
    end
  end
end