Class: Archives::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/archives/base.rb

Overview

fundamental class for naver gem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil) ⇒ Base

Replace this API key with your own (see dev.naver.com/openapi/register)



10
11
12
13
14
# File 'lib/archives/base.rb', line 10

def initialize(key=nil)
	@key = key
	@host = 'http://search.archives.go.kr/openapi'
	@api = '/search.arc'
end

Instance Attribute Details

#doc_rootObject

attributes for accessing retrieved raw xml and parsed libxml root node



7
8
9
# File 'lib/archives/base.rb', line 7

def doc_root
  @doc_root
end

#raw_xmlObject

attributes for accessing retrieved raw xml and parsed libxml root node



7
8
9
# File 'lib/archives/base.rb', line 7

def raw_xml
  @raw_xml
end

Instance Method Details

#http_get(url) ⇒ Object

Does an HTTP GET on a given URL and returns the response body



36
37
38
# File 'lib/archives/base.rb', line 36

def http_get(url)
	Net::HTTP.get_response(URI.parse(URI.encode(url))).body.to_s
end

#request(query, params = {}) ⇒ Object

Takes a Archives API method name and set of parameters; returns an RSS object with the response



17
18
19
20
21
22
23
# File 'lib/archives/base.rb', line 17

def request(query, params={})
	response = http_get(request_url(query, params))
	parser, parser.string = LibXML::XML::Parser.new, response
	@raw_xml = parser.parse
	@doc_root = @raw_xml.root
	RSS.new(@doc_root)
end

#request_url(query, params = {}) ⇒ Object

Takes a Archives API method name and set of parameters; returns the correct URL for the REST API.



26
27
28
29
30
31
32
33
# File 'lib/archives/base.rb', line 26

def request_url(query, params={})
	url = "#{@host}#{@api}?key=#{@key}&query=#{query}"
	params.each do |key, value|
		key = ABBREVIATION[key] unless ABBREVIATION[key].nil?
		url += "&#{key}=" + CGI::escape(value)
	end unless params.nil?
	url
end