lib/semantics3.rb in semantics3-0.12 vs lib/semantics3.rb in semantics3-0.14

- old
+ new

@@ -5,21 +5,23 @@ # Author: Sivamani Varun (varun@semantics3.com) # Copyright 2013 Semantics3 Inc., see LICENSE require 'rubygems' require 'oauth' +require 'curb' require 'uri' require 'cgi' require 'json' module Semantics3 @auth={} class Base - def initialize(api_key,api_secret) + def initialize(api_key,api_secret,auth = "oauth") @api_key = api_key @api_secret = api_secret + @auth_method = auth raise Error.new('API Credentials Missing','You did not supply an api_key. Please sign up at https://semantics3.com/ to obtain your api_key.','api_key') if api_key == '' raise Error.new('API Credentials Missing','You did not supply an api_secret. Please sign up at https://semantics3.com/ to obtain your api_secret.','api_secret') if api_secret == '' @consumer = OAuth::Consumer.new(@api_key, @api_secret) @@ -31,38 +33,56 @@ #def oauth(response) #returns a value def _make_request(endpoint,method = "GET",params) base_url = 'https://api.semantics3.com/v1/' #+ endpoint + '?q=' + CGI.escape(params) + if @auth_method == "basic" + base_url = 'https://api.semantics3.com/test/v1/' #+ endpoint + '?q=' + CGI.escape(params) - if method == "GET" - request_data = CGI.escape(params) - encoded_url = base_url + endpoint + '?q=' + request_data - response = @auth.get(encoded_url) - JSON.parse response.body - elsif method == "DELETE" - url = base_url + endpoint - response = @auth.delete(url,params) - JSON.parse response.body - else - url = URI(base_url+endpoint) - request = Net::HTTP::Post.new url.request_uri,params - http = Net::HTTP.new url.host, url.port - http.use_ssl = true - http.verify_mode = OpenSSL::SSL::VERIFY_PEER - request.oauth! http, @consumer,@auth - http.start - response = http.request request - JSON.parse response.body + if method == "GET" + request_data = CGI.escape(params) + encoded_url = base_url + endpoint + '?q=' + request_data + http = Curl.get(encoded_url) do|http| + http.headers['api_key'] = @api_key + end + JSON.parse http.body_str + elsif method == "DELETE" + url = base_url + endpoint + response = @auth.delete(url,params) + JSON.parse response.body + elsif method == "POST" + url = base_url + endpoint + response = @auth.post(url,params.to_json,{'Content-Type' => 'application/json'}) + JSON.parse response.body + else + raise Error.new('Invalid Method','You have entered an invalid method. Use GET,DELETE or POST.','method') + end + else + if method == "GET" + request_data = CGI.escape(params) + encoded_url = base_url + endpoint + '?q=' + request_data + response = @auth.get(encoded_url) + JSON.parse response.body + elsif method == "DELETE" + url = base_url + endpoint + response = @auth.delete(url,params) + JSON.parse response.body + elsif method == "POST" + url = base_url + endpoint + response = @auth.post(url,params.to_json,{'Content-Type' => 'application/json'}) + JSON.parse response.body + else + raise Error.new('Invalid Method','You have entered an invalid method. Use GET,DELETE or POST.','method') + end end end end class Products < Base - def initialize api_key, api_secret + def initialize api_key, api_secret, auth = 'oauth' super clear() end MAX_LIMIT = 10 @@ -276,6 +296,6 @@ JSON.parse(foo).all? rescue JSON::ParserError false end end -end \ No newline at end of file +end