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
- #parse_from_file(file) ⇒ Object
- #parse_from_uri(response) ⇒ 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 |
# File 'lib/wiki/api/connect.rb', line 12 def initialize( = {}) @@config ||= {} self.uri = [:uri] || @@config[:uri] self.file = [:file] || @@config[:file] self.api_path = [:api_path] || @@config[:api_path] self. = [:api_options] || @@config[:api_options] # defaults self.api_path ||= '/w/api.php' self. ||= { action: 'parse', format: 'json', page: '' } # errors raise('no uri given') if 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
78 79 80 |
# File 'lib/wiki/api/connect.rb', line 78 def config @@config ||= [] end |
.config=(config = {}) ⇒ Object
74 75 76 |
# File 'lib/wiki/api/connect.rb', line 74 def config=(config = {}) @@config = config end |
Instance Method Details
#connect ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/wiki/api/connect.rb', line 27 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' http.use_ssl = true # self.http.verify_mode = OpenSSL::SSL::VERIFY_NONE end self.request = Net::HTTP::Get.new(uri.request_uri) self.response = http.request(request) end |
#page(page_name) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/wiki/api/connect.rb', line 39 def page(page_name) self.[:page] = page_name # parse page by uri if !uri.nil? && file.nil? self.parsed = parse_from_uri(response) # parse page by file elsif !file.nil? self.parsed = parse_from_file(file) # invalid config, raise exception else raise('no :uri or :file config found!') end parsed end |
#parse_from_file(file) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/wiki/api/connect.rb', line 66 def parse_from_file(file) f = File.open(file) ret = Nokogiri::HTML(f) f.close ret end |
#parse_from_uri(response) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/wiki/api/connect.rb', line 54 def parse_from_uri(response) connect # rubocop:disable Lint/ShadowedArgument response = self.response # rubocop:enable Lint/ShadowedArgument 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(html[:*]) end |