Class: E621::Post

Inherits:
Container show all
Defined in:
lib/post.rb

Instance Method Summary collapse

Methods inherited from Container

#method_missing

Constructor Details

#initialize(post) ⇒ Post

Returns a new instance of Post.



24
25
26
# File 'lib/post.rb', line 24

def initialize(post)
  set_variables(post)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class E621::Container

Instance Method Details

#add_to_set(id) ⇒ Object



66
67
68
69
# File 'lib/post.rb', line 66

def add_to_set(id)
  api = API.new("set")
  api.post("add_post",{"post_id"=>@id,"set_id"=>id})
end

#download(where) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/post.rb', line 38

def download(where)
  host,port = @file_url.sub(%r{^.+?//},"").sub(%r{/.+$},""),@file_url.match(/^https/) ? 443 : 80
  http = HTTP.new(host,port)
  if !File.exist?(where) then
    body = ""
    while Digest::MD5.hexdigest(body) != @md5 do
      body = http.get(@file_url.sub(/^.+?\.net/,""),"")["body"]
    end
    File.open(where,"w") do |f|
      f.print body
    end
  else
    md5 = String.new
    File.open(where,"r") do |f|
      md5 = Digest::MD5.hexdigest(f.read)
    end
    if md5 != @md5 then
      body = ""
      while Digest::MD5.hexdigest(body) != @md5 do
        body = http.get(@file_url.sub(/^.+?\.net/,""),"")["body"]
      end
      File.open(where,"w") do |f|
        f.print body
      end
    end
  end
end

#download_data ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/post.rb', line 28

def download_data
  host,port = @file_url.sub(%r{^.+?//},"").sub(%r{/.+$},""),@file_url.match(/^https/) ? 443 : 80
  http = HTTP.new(host,port)
  body = String.new
  while Digest::MD5.hexdigest(body) != @md5 do
    body = http.get(@file_url.sub(/^.+?\.net/,""),"")["body"]
  end
  return body
end

#favorite ⇒ Object



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

def favorite
  api = API.new("favorite")
  api.post("create",{"id"=>@id})
end

#keys ⇒ Object



92
93
94
# File 'lib/post.rb', line 92

def keys
  return instance_variables.map{|i|i.to_s.sub("@","")}
end

#remove_from_set(id) ⇒ Object



71
72
73
74
# File 'lib/post.rb', line 71

def remove_from_set(id)
  api = API.new("set")
  api.post("remove_post",{"post_id"=>@id,"set_id"=>id})
end

#to_json(b = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/post.rb', line 96

def to_json(b=nil)
  json_hash = Hash.new
  instance_variables.each do |i|
    v = instance_variable_get(i)
    v = v.is_a?(Time) ? v.to_i : v
    json_hash.store(i.to_s.sub("@",""),v)
  end
  return json_hash.to_json
end

#unfavorite ⇒ Object



81
82
83
84
# File 'lib/post.rb', line 81

def unfavorite
  api = API.new("favorite")
  api.post("destroy",{"id"=>@id})
end

#vote(dir) ⇒ Object



86
87
88
89
90
# File 'lib/post.rb', line 86

def vote(dir)
  dir = if dir >= 0 then 1 elsif dir < 0 then -1 end
  api = API.new("post")
  api.post("vote",{"id"=>@id,"score"=>dir})
end