Class: Megalith::Novel
- Inherits:
-
Object
- Object
- Megalith::Novel
- Includes:
- Essentials
- Defined in:
- lib/megalith/scheme.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#novel ⇒ Object
readonly
Returns the value of attribute novel.
Instance Method Summary collapse
- #comment(text, params = {}) ⇒ Object
- #fetch_novel(base_url, log, key) ⇒ Object
-
#initialize(base_url, log, key) ⇒ Novel
constructor
A new instance of Novel.
- #method_missing(action, *args) ⇒ Object
- #params ⇒ Object (also: #available_methods)
- #plain ⇒ Object
- #simple_rating(point) ⇒ Object
- #to_hash ⇒ Object
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_url ⇒ Object (readonly)
Returns the value of attribute base_url.
7 8 9 |
# File 'lib/megalith/scheme.rb', line 7 def base_url @base_url end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
9 10 11 |
# File 'lib/megalith/scheme.rb', line 9 def key @key end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
8 9 10 |
# File 'lib/megalith/scheme.rb', line 8 def log @log end |
#novel ⇒ Object (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) = 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"] = 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") = lines[0].split("<>") hash = lines[1] text = lines[2, lines.size].join comment_count, review_count = [4].split("/") comments = fetch_comments(base_url, key) novel = { :title => [0], :text => text, :aft => aft, :author => Author.new(:name => [1], :email => [2], :website => [3]), :tags => [12].split(/[\s ]/), :log => log, :key => key, :created_at => Time.at(key), :updated_at => Time.parse([7]), :review_count => review_count, :comment_count => comment_count, :point => [5], :rate => [6], :host => [8], :background_color => [9], :text_color => [10], :convert_newline => [11], :size => text.bytesize / 1024, :url => URI.join(base_url, "?mode=read&key=#{key}&log=#{log}").to_s, :comments => comments } return novel end |
#params ⇒ Object Also known as: available_methods
122 |
# File 'lib/megalith/scheme.rb', line 122 def params() @novel.keys.map{|k|k.to_sym} ; end |
#plain ⇒ Object
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 (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) = 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"] = req["User-Agent"] = USER_AGENT res = http.request(req, post_params) return res end |
#to_hash ⇒ Object
125 126 127 |
# File 'lib/megalith/scheme.rb', line 125 def to_hash @novel end |