Class: Naver::Base
- Inherits:
-
Object
- Object
- Naver::Base
- Defined in:
- lib/naver/base.rb
Instance Attribute Summary collapse
-
#doc ⇒ Object
Returns the value of attribute doc.
-
#raw_xml ⇒ Object
Returns the value of attribute raw_xml.
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)
9 10 11 12 13 |
# File 'lib/naver/base.rb', line 9 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
15 16 17 18 19 20 21 |
# File 'lib/naver/base.rb', line 15 def method_missing(target, query, params={}) if METHOD_LIST.include?(target.to_s) request(target, query, params) else raise NoMethod end end |
Instance Attribute Details
#doc ⇒ Object
Returns the value of attribute doc.
6 7 8 |
# File 'lib/naver/base.rb', line 6 def doc @doc end |
#raw_xml ⇒ Object
Returns the value of attribute raw_xml.
6 7 8 |
# File 'lib/naver/base.rb', line 6 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
42 43 44 |
# File 'lib/naver/base.rb', line 42 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
24 25 26 27 28 29 |
# File 'lib/naver/base.rb', line 24 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 = @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.
32 33 34 35 36 37 38 39 |
# File 'lib/naver/base.rb', line 32 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 |