diff --git a/lib/zooz.rb b/lib/zooz.rb index b027e81..1ca7fa7 100644 --- a/lib/zooz.rb +++ b/lib/zooz.rb @@ -1,3 +1,4 @@ # Require all requests require 'zooz/request/open' require 'zooz/request/verify' +require 'zooz/request/commit' diff --git a/lib/zooz/request.rb b/lib/zooz/request.rb index 097df84..0a751f5 100644 --- a/lib/zooz/request.rb +++ b/lib/zooz/request.rb @@ -3,7 +3,7 @@ # The ZooZ Request class formats and sends requests to the ZooZ API. module Zooz class Request - attr_accessor :sandbox, :unique_id, :app_key, :response_type, :cmd + attr_accessor :sandbox, :developer_id, :unique_id, :app_key, :response_type, :cmd attr_reader :errors, :params def initialize @@ -11,6 +11,8 @@ def initialize @errors = [] @response_type = 'NVP' @sandbox = false + @headers = {} + @url = '' end # Set a request parameter. @@ -23,6 +25,10 @@ def get_param @params[name] end + def set_header name, value + @headers[name]= value + end + # Whether the request will be sent to sandbox. def is_sandbox? @sandbox == true @@ -30,21 +36,38 @@ def is_sandbox? # Get the URL of the API, based on whether in sandbox mode or not. def url - (is_sandbox? ? 'https://sandbox.zooz.co' : 'https://app.zooz.com') + - '/mobile/SecuredWebServlet' + if @response_type.eql?('NVP') + @url = (is_sandbox? ? 'https://sandbox.zooz.co' : 'https://app.zooz.com') + + '/mobile/SecuredWebServlet' + else + @url = (is_sandbox? ? 'https://sandbox.zooz.co' : 'https://app.zooz.com') + + '/mobile/ExtendedServerAPI' + end + @url end # Send a request to the server, returns a Zooz::Response object or false. # If returning false, the @errors attribute is populated. def request + url1 = url return false unless valid? http_response = HTTParty.post(url, :format => :plain, - :query => @params.merge({ :cmd => @cmd }), - :headers => { - 'ZooZ-Unique-ID' => @unique_id, - 'ZooZ-App-Key' => @app_key, - 'ZooZ-Response-Type' => @response_type, - }) + :query => @params.merge({ :cmd => @cmd }), + :headers => { + 'ZooZ-Unique-ID' => @unique_id, + 'ZooZ-App-Key' => @app_key, + 'ZooZ-Response-Type' => @response_type, + }) if @response_type.eql?('NVP') + + + + http_response = HTTParty.post(url, :format => :json, + :body => @params.merge({ :cmd => @cmd }), + :headers => { + 'ZooZDeveloperId' => @developer_id, + 'ZooZServerAPIKey' => CGI::escape(@app_key) + }) if @response_type.eql?('JSON') + response = Response.new response.request = self response.http_response = http_response @@ -58,11 +81,12 @@ def request # Whether the request object is valid for requesting. def valid? @errors = [] - @errors << 'unique_id is required' if @unique_id.nil? + @errors << 'unique_id is required' if @unique_id.nil? && @response_type.eql?('NVP') + @errors << 'developer_id is required' if @developer_id.nil? && @response_type.eql?('JSON') @errors << 'app_key is required' if @app_key.nil? @errors << 'cmd is required' if @cmd.nil? @errors << 'response_type is required' if @response_type.nil? @errors.empty? end end -end \ No newline at end of file +end diff --git a/lib/zooz/request/commit.rb b/lib/zooz/request/commit.rb new file mode 100644 index 0000000..b4110c9 --- /dev/null +++ b/lib/zooz/request/commit.rb @@ -0,0 +1,42 @@ +require 'zooz/request' +require 'zooz/response/commit' +require 'active_support/core_ext/module/delegation' + +module Zooz + class Request + class Commit + attr_accessor :transaction_id, :requestor + attr_reader :errors + delegate :sandbox, :sandbox=, :unique_id, :unique_id=, :app_key, + :app_key=, :response_type, :response_type=, :cmd, :cmd=, :is_sandbox?, :developer_id, :developer_id=, + :to => :requestor + + def initialize + @errors = [] + @requestor = Request.new + @requestor.cmd = 'commitTransaction' + end + + def request + return false unless valid? + @requestor.set_param('ver', '1.0.6') + @requestor.set_param('transactionID', @transaction_id) + open_response = Response::Commit.new + open_response.request = self + open_response.response = @requestor.request + unless open_response.response + @errors += @requestor.errors + return false + end + open_response + end + + def valid? + @errors = [] + @errors << 'transaction_id is required' if @transaction_id.nil? + @errors += @requestor.errors unless @requestor.valid? + @errors.empty? + end + end + end +end diff --git a/lib/zooz/request/verify.rb b/lib/zooz/request/verify.rb index 7f4bfa3..dae03e3 100644 --- a/lib/zooz/request/verify.rb +++ b/lib/zooz/request/verify.rb @@ -5,7 +5,7 @@ module Zooz class Request class Verify - attr_accessor :trx_id + attr_accessor :trx_id, :requestor attr_reader :errors delegate :sandbox, :sandbox=, :unique_id, :unique_id=, :app_key, :app_key=, :response_type, :response_type=, :cmd, :cmd=, :is_sandbox?, diff --git a/lib/zooz/response.rb b/lib/zooz/response.rb index 7859c78..3ad76b3 100644 --- a/lib/zooz/response.rb +++ b/lib/zooz/response.rb @@ -5,10 +5,10 @@ # and reports on success and errors. module Zooz class Response - attr_accessor :http_response, :request + attr_accessor :http_response, :request, :json_response attr_reader :errors delegate :body, :code, :message, :headers, :to => :http_response, - :prefix => :http + :prefix => :http delegate :is_sandbox?, :unique_id, :app_key, :to => :request def initialize @@ -23,12 +23,24 @@ def get_parsed_singular(offset) # Get a parsed response as an object from the response text. def parsed_response - CGI::parse(http_body.strip) + CGI::parse(http_body.strip) unless http_body.nil? + end + + def parsed_json_response + begin + @json_response = JSON.parse(http_body) + rescue + @json_response = nil + end end # Get the ZooZ status code, 0 for success. def status_code - get_parsed_singular('statusCode') + if @json_response.blank? + get_parsed_singular('statusCode') + else + @json_response['ResponseStatus'].to_s + end end # Get the ZooZ error message, returned when status_code is not 0. @@ -38,6 +50,7 @@ def error_message # Whether the request was successful, populates the @errors array on error. def success? + parsed_json_response @errors = [] unless http_code.to_s[0,1] == '2' @errors << "HTTP status #{http_code}: #{http_message}" diff --git a/lib/zooz/response/commit.rb b/lib/zooz/response/commit.rb new file mode 100644 index 0000000..16dff8f --- /dev/null +++ b/lib/zooz/response/commit.rb @@ -0,0 +1,15 @@ +require 'zooz/response' +require 'active_support/core_ext/module/delegation' + +module Zooz + class Response + class Commit + attr_accessor :request, :response + delegate :success?, :errors, :parsed_json_response, :get_parsed_singular, + :to => :response + delegate :is_sandbox?, :unique_id, :app_key, :transaction_id, + :to => :request + end + end +end + diff --git a/zooz.gemspec b/zooz.gemspec index 2a65fc4..ac7d00e 100644 --- a/zooz.gemspec +++ b/zooz.gemspec @@ -1,14 +1,15 @@ Gem::Specification.new do |s| s.name = 'zooz' - s.version = '1.0.2' - s.date = '2012-08-23' + s.version = '1.0.11' + s.date = '2013-10-16' s.summary = "ZooZ" s.description = "A ZooZ API library for Ruby" - s.authors = ["Michael Alexander"] + s.authors = ["Michael Alexander", "Francisco Soto", "Lostmyname"] s.email = 'beefsack@gmail.com' s.files = Dir["{lib}/**/*.rb", "bin/*", "LICENSE", "*.md"] s.require_path = 'lib' s.homepage = 'https://github.com/Miniand/zooz-ruby' - s.add_dependency('activesupport', '~> 3.2.0') + s.add_dependency('activesupport', '~> 4') s.add_dependency('httparty', '~> 0.8.0') + s.add_dependency('json') end