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 ||= nil
  options.merge! @@config unless @@config.nil?
  self.uri = options[:uri] if options.include? :uri
  self.api_path = options[:api_path] if options.include? :api_path
  self.api_options = options[:api_options] if options.include? :api_options

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

  # errors
  raise "no uri given" if self.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

#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



53
54
55
# File 'lib/wiki/api/connect.rb', line 53

def config
  @@config ||= []
end

.config=(config = {}) ⇒ Object



50
51
52
# File 'lib/wiki/api/connect.rb', line 50

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"
    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



39
40
41
42
43
44
45
46
47
# File 'lib/wiki/api/connect.rb', line 39

def page page_name
  self.api_options[:page] = page_name
  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[:*]
end