Class: Wiki::Api::Connect

Inherits:
Object
  • Object
show all
Defined in:
lib/wiki/api/connect.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
  @@config ||= {}
  self.uri = options[:uri] || @@config[:uri]
  self.file = options[:file] || @@config[:file]
  self.api_path = options[:api_path] || @@config[:api_path]
  self.api_options = options[:api_options] || @@config[:api_options]

  # defaults
  self.api_path ||= '/w/api.php'
  self.api_options ||= { action: 'parse', format: 'json', page: '' }

  # errors
  raise('no uri given') if uri.nil?
end

Instance Attribute Details

#api_optionsObject

Returns the value of attribute api_options.



10
11
12
# File 'lib/wiki/api/connect.rb', line 10

def api_options
  @api_options
end

#api_pathObject

Returns the value of attribute api_path.



10
11
12
# File 'lib/wiki/api/connect.rb', line 10

def api_path
  @api_path
end

#fileObject

Returns the value of attribute file.



10
11
12
# File 'lib/wiki/api/connect.rb', line 10

def file
  @file
end

#htmlObject

Returns the value of attribute html.



10
11
12
# File 'lib/wiki/api/connect.rb', line 10

def html
  @html
end

#httpObject

Returns the value of attribute http.



10
11
12
# File 'lib/wiki/api/connect.rb', line 10

def http
  @http
end

#parsedObject

Returns the value of attribute parsed.



10
11
12
# File 'lib/wiki/api/connect.rb', line 10

def parsed
  @parsed
end

#requestObject

Returns the value of attribute request.



10
11
12
# File 'lib/wiki/api/connect.rb', line 10

def request
  @request
end

#responseObject

Returns the value of attribute response.



10
11
12
# File 'lib/wiki/api/connect.rb', line 10

def response
  @response
end

#uriObject

Returns the value of attribute uri.



10
11
12
# File 'lib/wiki/api/connect.rb', line 10

def uri
  @uri
end

Class Method Details

.configObject



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

#connectObject



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.api_options)
  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.api_options[: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