Class: Megalith::Novel

Inherits:
Object
  • Object
show all
Includes:
Essentials
Defined in:
lib/megalith/scheme.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Essentials

#fetch_comments, #fetch_subjects, #param_serialize, #send_req

Constructor Details

#initialize(base_url, log, key) ⇒ Novel

Returns a new instance of Novel.



11
12
13
14
15
16
17
# File 'lib/megalith/scheme.rb', line 11

def initialize(base_url, log, key)
  relative_log = (log < 1) ? fetch_subjects(base_url).size : log
  @novel = fetch_novel(base_url, relative_log, key)
  @base_url = base_url
  @log = relative_log
  @key = key
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(action, *args) ⇒ Object



118
119
120
# File 'lib/megalith/scheme.rb', line 118

def method_missing(action, *args)
  return @novel[action.to_s.to_sym] rescue nil
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/megalith/scheme.rb', line 9

def key
  @key
end

#logObject (readonly)

Returns the value of attribute log.



8
9
10
# File 'lib/megalith/scheme.rb', line 8

def log
  @log
end

#novelObject (readonly)

Returns the value of attribute novel.



6
7
8
# File 'lib/megalith/scheme.rb', line 6

def novel
  @novel
end

Instance Method Details

#comment(text, params = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/megalith/scheme.rb', line 82

def comment(text, params={})
  # Get cookie
  get_params = param_serialize({
    :mode => :read,
    :key => @key,
    :log => @log
  })
  uri = URI.parse(@base_url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.path)
  req["User-Agent"] = USER_AGENT
  res = http.request(req, get_params)
  cookie = res["Set-Cookie"]

  # Post Comment
  post_uri_params = param_serialize({
    :mode => :update,
    :key => @key,
    :log => @log,
    :target => :res
  })
  params.each do |k, v|
    params[k] = v.to_s.tosjis
  end
  post_params = param_serialize({:body => text.tosjis}.update(params), false)
  req = Net::HTTP::Post.new(File.join(uri.path, post_uri_params))
  req["Cookie"] = cookie
  req["User-Agent"] = USER_AGENT
  res = http.request(req, post_params)
  return res
end

#fetch_novel(base_url, log, key) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/megalith/scheme.rb', line 19

def fetch_novel(base_url, log, key)
  novel_page = send_req(File.join(base_url, "dat", "#{key}.dat"))
  aft = send_req(File.join(base_url, "aft", "#{key}.aft.dat"))
  lines = novel_page.split("\n")
  meta = lines[0].split("<>")
  hash = lines[1]
  text = lines[2, lines.size].join
  comment_count, review_count = meta[4].split("/")
  comments = fetch_comments(base_url, key)
  novel = {
    :title => meta[0],
    :text => text,
    :aft => aft,
    :author => Author.new(:name => meta[1], :email => meta[2], :website => meta[3]),
    :tags => meta[12].split(/[\s ]/),
    :log => log,
    :key => key,
    :created_at => Time.at(key),
    :updated_at => Time.parse(meta[7]),
    :review_count => review_count,
    :comment_count => comment_count,
    :point => meta[5],
    :rate => meta[6],
    :host => meta[8],
    :background_color => meta[9],
    :text_color => meta[10],
    :convert_newline => meta[11],
    :size => text.bytesize / 1024,
    :url => URI.join(base_url, "?mode=read&key=#{key}&log=#{log}").to_s,
    :comments => comments
  }
  return novel
end

#paramsObject Also known as: available_methods



122
# File 'lib/megalith/scheme.rb', line 122

def params() @novel.keys.map{|k|k.to_sym} ; end

#plainObject



114
115
116
# File 'lib/megalith/scheme.rb', line 114

def plain
  return self.text.gsub(/(<br>|\r?\n)/, "")
end

#simple_rating(point) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/megalith/scheme.rb', line 53

def simple_rating(point)
  # Get cookie
  get_params = param_serialize({
    :mode => :read,
    :key => @key,
    :log => @log
  })
  uri = URI.parse(@base_url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Get.new(uri.path)
  req["User-Agent"] = USER_AGENT
  res = http.request(req, get_params)
  cookie = res["Set-Cookie"]

  # Post Comment
  post_uri_params = param_serialize({
    :mode => :update,
    :key => @key,
    :log => @log,
    :target => :res
  })
  post_params = param_serialize({:body => "#EMPTY#", :point => point}, false)
  req = Net::HTTP::Post.new(File.join(uri.path, post_uri_params))
  req["Cookie"] = cookie
  req["User-Agent"] = USER_AGENT
  res = http.request(req, post_params)
  return res
end

#to_hashObject



125
126
127
# File 'lib/megalith/scheme.rb', line 125

def to_hash
  @novel
end