lib/openamplify.rb in openamplify-0.1.1 vs lib/openamplify.rb in openamplify-0.2.0

- old
+ new

@@ -4,20 +4,21 @@ module OpenAmplify API_URL = "http://portaltnx20.openamplify.com/AmplifyWeb_v20/AmplifyThis" class Client - def initialize(options) - @options = { :base_url => API_URL } + def initialize(options={}) + @options = { :base_url => API_URL, :method => :get } @options.merge!(OpenAmplify.symbolize_keys(options)) end def analyze_text(text) - Response.new(:base_url => @options[:base_url], :query => query.merge(:inputText => text)) + Response.new(:base_url => @options[:base_url], :query => query.merge(:inputText => text), + :method => @options[:method]) end - %w(api_key analysis base_url).each do |attr| + %w(api_key analysis base_url method).each do |attr| class_eval <<-EOS def #{attr} @options[:#{attr}] end @@ -47,65 +48,91 @@ def request_url @request_url ||= compose_url(@options[:base_url], @options[:query]) end - def response - @response ||= get_response - end - - # Make this class behave like a Hash - # TODO: Add more methods - def each response.each do |k, v| yield(k, v) end end - - ['has_key?', '==', '[]', 'fetch', 'empty?', 'include?', 'inspect', - 'key?', 'keys', 'length', 'member?', 'merge', 'merge!' - ].each do |method| - class_eval <<-EOS - def #{method}(*args) - response.send('#{method}', *args) - end - EOS + + def method_missing(name, *args, &block) + response.send(name, *args, &block) end - ## Hash methods end here - + # Support the different formats. Note this would entail another request # to openamplify %w(xml json rdf csv oas signals pretty).each do |format| class_eval <<-EOS def to_#{format} - get('#{format}') + fetch_as_format(:#{format}) end EOS end - private + def top_topics + items = response && response['Topics']['TopTopics'] + end + def proper_nouns + items = response && response['Topics']['ProperNouns'] + + return items if items.is_a?(Array) + + # I'm not sure if this is the default behavior if + # only a single item is found, or if it is a bug + # TODO: check other helpers as well + if items.is_a?(Hash) + return [ items['TopicResult'] ] + end + end + + def locations + response && response['Topics']['Locations'] + end + + def domains + response && response['Topics']['Domains'] + end + + private def compose_url(path, params) path + '?' + URI.escape(params.collect{ |k, v| "#{k}=#{v}" }.join('&')) end + + def response + @response ||= fetch_response + end - def get_response - response = get('json') + def fetch_response + response = fetch_as_format(:json) result = JSON.parse(response) - + if analysis = @options[:query][:analysis] name = analysis.sub(/./){ |s| s.upcase } result["ns1:#{name}Response"]["#{name}Return"] else result['ns1:AmplifyResponse']['AmplifyReturn'] end end - def get(format) - params = @options[:query] - url = compose_url(@options[:base_url], params.merge(:outputFormat => format)) + def fetch_as_format(format) + fetch(@options[:base_url], @options[:query].merge(:outputFormat => format), @options[:method]) + end + + def fetch(path, params, method) + self.send(method, path, params) + end + + def get(path, params) + url = compose_url(path, params) Net::HTTP.get_response(URI.parse(url)).body + end + + def post(path, params) + uri = URI::parse(path) + Net::HTTP.post_form(uri, params).body end end # OpenAmplify::Response private