Class: Megalopolis::Novel

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Essentials

#param_serialize, #send_req

Methods inherited from MHash

#method_missing, #params

Constructor Details

#initialize(base_url, log, id) ⇒ Novel

Returns a new instance of Novel.



26
27
28
29
30
31
32
# File 'lib/megalopolis/scheme.rb', line 26

def initialize(base_url, log, id)
  @novel = fetch_novel(base_url, log, id)
  @base_url = base_url
  @log = log
  @id = id
  super(@novel)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Megalopolis::MHash

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#logObject (readonly)

Returns the value of attribute log.



23
24
25
# File 'lib/megalopolis/scheme.rb', line 23

def log
  @log
end

#novelObject (readonly)

Returns the value of attribute novel.



21
22
23
# File 'lib/megalopolis/scheme.rb', line 21

def novel
  @novel
end

Instance Method Details

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



50
51
52
53
54
55
56
57
# File 'lib/megalopolis/scheme.rb', line 50

def comment(text, params={})
  get_params = param_serialize(params)
  uri = URI.parse(@base_url)
  Net::HTTP.start(uri.host, uri.port) do |http|
    header = { "User-Agent" => USER_AGENT }
    response = http.post(uri.path, get_param, header)
  end
end

#fetch_novel(base_url, log, id) ⇒ Object



34
35
36
37
38
39
# File 'lib/megalopolis/scheme.rb', line 34

def fetch_novel(base_url, log, id)
  page = send_req(File.join(base_url, log.to_s, "#{id}.json"))
  novel = JSON.parse(page)
  novel["url"] = URI.join(base_url, "#{log}/#{id}").to_s
  return novel
end

#plainObject



59
60
61
# File 'lib/megalopolis/scheme.rb', line 59

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

#simple_rating(point) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/megalopolis/scheme.rb', line 41

def simple_rating(point)
  get_params = param_serialize({:point => point})
  uri = URI.parse(@base_url)
  Net::HTTP.start(uri.host, uri.port) do |http|
    header = { "User-Agent" => USER_AGENT }
    response = http.post(uri.path, get_param, header)
  end
end