Class: Naver::Base
- Inherits:
-
Object
- Object
- Naver::Base
- Defined in:
- lib/naver/base.rb
Overview
fundamental class for naver gem
Instance Attribute Summary collapse
-
#doc_root ⇒ Object
attributes for accessing retrieved raw xml and parsed libxml root node.
-
#raw_xml ⇒ Object
attributes for accessing retrieved raw xml and parsed libxml root node.
Instance Method Summary collapse
-
#http_get(url) ⇒ Object
Does an HTTP GET on a given URL and returns the response body.
-
#initialize(key = nil) ⇒ Base
constructor
Replace this API key with your own (see dev.naver.com/openapi/register).
- #method_missing(target, query, params = {}) ⇒ Object
-
#request(target, query, params = {}) ⇒ Object
Takes a Naver API method name and set of parameters; returns an libxml object with the response.
-
#request_url(target, query, params = {}) ⇒ Object
Takes a Naver API method name and set of parameters; returns the correct URL for the REST API.
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/naver/base.rb', line 10 def initialize(key=nil) @key = key @host = 'http://openapi.naver.com' @api = '/search' end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(target, query, params = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/naver/base.rb', line 16 def method_missing(target, query, params={}) raise NoQuery if query.empty? if METHOD_LIST.include?(target.to_s) request(target, query, params) if XML_LIST.include?(target.to_s) return Result.new(@doc_root, target.to_s) else return RSS.new(@doc_root) end else raise NoMethod end end |
Instance Attribute Details
#doc_root ⇒ Object
attributes for accessing retrieved raw xml and parsed libxml root node
7 8 9 |
# File 'lib/naver/base.rb', line 7 def doc_root @doc_root end |
#raw_xml ⇒ Object
attributes for accessing retrieved raw xml and parsed libxml root node
7 8 9 |
# File 'lib/naver/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
49 50 51 |
# File 'lib/naver/base.rb', line 49 def http_get(url) Net::HTTP.get_response(URI.parse(url)).body.to_s end |
#request(target, query, params = {}) ⇒ Object
Takes a Naver API method name and set of parameters; returns an libxml object with the response
31 32 33 34 35 36 |
# File 'lib/naver/base.rb', line 31 def request(target, query, params={}) response = http_get(request_url(target, query, params)) parser, parser.string = LibXML::XML::Parser.new, response @raw_xml = parser.parse @doc_root = @raw_xml.root end |
#request_url(target, query, params = {}) ⇒ Object
Takes a Naver API method name and set of parameters; returns the correct URL for the REST API.
39 40 41 42 43 44 45 46 |
# File 'lib/naver/base.rb', line 39 def request_url(target, query, params={}) url = "#{@host}#{@api}?key=#{@key}&target=#{target}&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 |