Class: Wiki::Api::Connect
- Inherits:
-
Object
- Object
- Wiki::Api::Connect
- Defined in:
- lib/wiki/api/connect.rb
Instance Attribute Summary collapse
-
#api_options ⇒ Object
Returns the value of attribute api_options.
-
#api_path ⇒ Object
Returns the value of attribute api_path.
-
#file ⇒ Object
Returns the value of attribute file.
-
#html ⇒ Object
Returns the value of attribute html.
-
#http ⇒ Object
Returns the value of attribute http.
-
#parsed ⇒ Object
Returns the value of attribute parsed.
-
#request ⇒ Object
Returns the value of attribute request.
-
#response ⇒ Object
Returns the value of attribute response.
-
#uri ⇒ Object
Returns the value of attribute uri.
Class Method Summary collapse
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(options = {}) ⇒ Connect
constructor
A new instance of Connect.
- #page(page_name) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Connect
Returns a new instance of Connect.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/wiki/api/connect.rb', line 12 def initialize(={}) @@config ||= nil .merge! @@config unless @@config.nil? self.uri = [:uri] if .include? :uri self.file = [:file] if .include? :file self.api_path = [:api_path] if .include? :api_path self. = [:api_options] if .include? :api_options # defaults self.api_path ||= "/w/api.php" self. ||= {action: "parse", format: "json", page: ""} # errors raise "no uri given" if self.uri.nil? end |
Instance Attribute Details
#api_options ⇒ Object
Returns the value of attribute api_options.
10 11 12 |
# File 'lib/wiki/api/connect.rb', line 10 def @api_options end |
#api_path ⇒ Object
Returns the value of attribute api_path.
10 11 12 |
# File 'lib/wiki/api/connect.rb', line 10 def api_path @api_path end |
#file ⇒ Object
Returns the value of attribute file.
10 11 12 |
# File 'lib/wiki/api/connect.rb', line 10 def file @file end |
#html ⇒ Object
Returns the value of attribute html.
10 11 12 |
# File 'lib/wiki/api/connect.rb', line 10 def html @html end |
#http ⇒ Object
Returns the value of attribute http.
10 11 12 |
# File 'lib/wiki/api/connect.rb', line 10 def http @http end |
#parsed ⇒ Object
Returns the value of attribute parsed.
10 11 12 |
# File 'lib/wiki/api/connect.rb', line 10 def parsed @parsed end |
#request ⇒ Object
Returns the value of attribute request.
10 11 12 |
# File 'lib/wiki/api/connect.rb', line 10 def request @request end |
#response ⇒ Object
Returns the value of attribute response.
10 11 12 |
# File 'lib/wiki/api/connect.rb', line 10 def response @response end |
#uri ⇒ Object
Returns the value of attribute uri.
10 11 12 |
# File 'lib/wiki/api/connect.rb', line 10 def uri @uri end |
Class Method Details
.config ⇒ Object
67 68 69 |
# File 'lib/wiki/api/connect.rb', line 67 def config @@config ||= [] end |
.config=(config = {}) ⇒ Object
64 65 66 |
# File 'lib/wiki/api/connect.rb', line 64 def config=(config = {}) @@config = config end |
Instance Method Details
#connect ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/wiki/api/connect.rb', line 28 def connect uri = URI("#{self.uri}#{self.api_path}") uri.query = URI.encode_www_form self. self.http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == "https" self.http.use_ssl = true #self.http.verify_mode = OpenSSL::SSL::VERIFY_NONE end self.request = Net::HTTP::Get.new(uri.request_uri) self.response = self.http.request(request) end |
#page(page_name) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/wiki/api/connect.rb', line 40 def page page_name self.[:page] = page_name # parse page by uri if !self.uri.nil? && self.file.nil? self.connect response = self.response json = JSON.parse response.body, {symbolize_names: true} raise json[:error][:code] unless valid? json, response self.html = json[:parse][:text] self.parsed = Nokogiri::HTML self.html[:*] # parse page by file elsif !self.file.nil? f = File.open(self.file) # self.parsed = Nokogiri::HTML self.html[:*] self.parsed = Nokogiri::HTML(f) f.close # invalid config, raise exception else raise "no :uri or :file config found!" end self.parsed end |