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



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

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
65
66
67
68
69
# File 'lib/post.rb', line 38

def download(where)
  if @file_url then
    host,port = @file_url.sub(%r{^.+?//},"").sub(%r{/.+$},""),@file_url.match(/^https/) ? 443 : 80
  else
    host,port = "static1.e621.net",443
    @file_url = "/data/#{@md5[0,2]}/#{@md5[2,2]}/#@md5.#@file_ext"
  end
  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_dataObject



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

#favoriteObject



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

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

#keysObject



97
98
99
# File 'lib/post.rb', line 97

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

#remove_from_set(id) ⇒ Object



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

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



101
102
103
104
105
106
107
108
109
# File 'lib/post.rb', line 101

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

#unfavoriteObject



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

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

#vote(dir) ⇒ Object



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

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