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

- old
+ new

@@ -5,20 +5,21 @@ module OpenAmplify API_URL = "http://portaltnx20.openamplify.com/AmplifyWeb_v20/AmplifyThis" class Client def initialize(options={}) - @options = { :base_url => API_URL, :method => :get } + @options = { :api_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), + validate + Response.new(:api_url => @options[:api_url], :query => query.merge(:inputText => text), :method => @options[:method]) end - %w(api_key analysis base_url method).each do |attr| + %w(api_key analysis api_url method).each do |attr| class_eval <<-EOS def #{attr} @options[:#{attr}] end @@ -45,12 +46,16 @@ def initialize(options) @options = options end def request_url - @request_url ||= compose_url(@options[:base_url], @options[:query]) + @request_url ||= compose_url(@options[:api_url], @options[:query]) end + + def reload + response + end def each response.each do |k, v| yield(k, v) end @@ -94,10 +99,11 @@ 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 @@ -115,25 +121,30 @@ result['ns1:AmplifyResponse']['AmplifyReturn'] end end def fetch_as_format(format) - fetch(@options[:base_url], @options[:query].merge(:outputFormat => format), @options[:method]) + fetch(@options[:api_url], @options[:query].merge(:outputFormat => format), @options[:method]) end def fetch(path, params, method) + raise OpenAmplify::NotSupported unless [:get, :post].include?(method.to_sym) self.send(method, path, params) end def get(path, params) - url = compose_url(path, params) - Net::HTTP.get_response(URI.parse(url)).body + uri = URI.parse(compose_url(path, params)) + response = Net::HTTP.get_response(uri) + self.class.validate(response) + response.body end def post(path, params) - uri = URI::parse(path) - Net::HTTP.post_form(uri, params).body + uri = URI::parse(path) + response = Net::HTTP.post_form(uri, params) + self.class.validate(response) + response.body end end # OpenAmplify::Response private @@ -144,5 +155,7 @@ options end end end # module OpenAmplify + +require 'openamplify/validations'