Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added rails4 support #5

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/zooz.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Require all requests
require 'zooz/request/open'
require 'zooz/request/verify'
require 'zooz/request/commit'
46 changes: 35 additions & 11 deletions lib/zooz/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
# 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
@params = {}
@errors = []
@response_type = 'NVP'
@sandbox = false
@headers = {}
@url = ''
end

# Set a request parameter.
Expand All @@ -23,28 +25,49 @@ 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
end

# 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
Expand All @@ -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
end
42 changes: 42 additions & 0 deletions lib/zooz/request/commit.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/zooz/request/verify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand Down
21 changes: 17 additions & 4 deletions lib/zooz/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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}"
Expand Down
15 changes: 15 additions & 0 deletions lib/zooz/response/commit.rb
Original file line number Diff line number Diff line change
@@ -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

9 changes: 5 additions & 4 deletions zooz.gemspec
Original file line number Diff line number Diff line change
@@ -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 = '[email protected]'
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