Class: Daum::Base
- Inherits:
-
Object
- Object
- Daum::Base
- Defined in:
- lib/daum/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 dna.daum.net/apis).
- #method_missing(target, query, params = {}) ⇒ Object
-
#request(target, query, params = {}) ⇒ Object
Takes a Daum API method name and set of parameters; returns an libxml object with the response.
-
#request_url(target, query, params = {}) ⇒ Object
Takes a Daum 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 dna.daum.net/apis)
10 11 12 13 |
# File 'lib/daum/base.rb', line 10 def initialize(key=nil) @key = key @host = 'http://apis.daum.net' 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 22 23 |
# File 'lib/daum/base.rb', line 15 def method_missing(target, query, params={}) raise NoQuery if query.empty? if METHOD_LIST.include?(target.to_s) request(target, query, params) return RSS.new(@doc_root, target) 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/daum/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/daum/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
60 61 62 |
# File 'lib/daum/base.rb', line 60 def http_get(url) Net::HTTP.get_response(URI.parse(URI.encode(url))).body.to_s end |
#request(target, query, params = {}) ⇒ Object
Takes a Daum API method name and set of parameters; returns an libxml object with the response
26 27 28 29 30 31 |
# File 'lib/daum/base.rb', line 26 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 Daum API method name and set of parameters; returns the correct URL for the REST API.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/daum/base.rb', line 34 def request_url(target, query, params={}) url = @host if SEARCH_LIST.include?(target.to_s) url += "/search/#{target}" elsif DIC_LIST.include?(target.to_s) url += "/search/dic/#{target}" elsif SHOPPING_LIST.include?(target.to_s) url += "/shopping/#{target}" elsif SUGGEST_LIST.include?(target.to_s) url += "/suggest/#{target}" end unless target.to_s == "detail" url += "?apikey=#{@key}&q=#{query}" else url += "?apikey=#{@key}&docid=#{query}" end params.each do |key, value| key = ABBREVIATION[key] unless ABBREVIATION[key].nil? url += "&#{key}=" + CGI::escape(value) end unless params.nil? url end |