lib/openpay/open_pay_resource.rb in openpay-1.0.3 vs lib/openpay/open_pay_resource.rb in openpay-1.0.4
- old
+ new
@@ -1,44 +1,40 @@
-
-
#This class is the abstract base class for other Openpay resources
#This class defines the basic rest verbs making use of the rest-api gem.
#Method aliases are created to provide friendly names.
class OpenPayResource
attr_accessor :api_hook
- def initialize(merchant_id,private_key,production=false)
- @merchant_id=merchant_id
- @private_key=private_key
- #assigns base url depending the requested env
- @base_url=OpenpayApi::base_url(production)
- @errors=false
+ def initialize(merchant_id, private_key, production=false)
+ @merchant_id=merchant_id
+ @private_key=private_key
+ #assigns base url depending the requested env
+ @base_url=OpenpayApi::base_url(production)
+ @errors=false
@production=production
@timeout=90
- #created resources should have a hook with the base class to keep control of created resources
+ #created resources should have a hook with the base class to keep control of created resources
@api_hook=nil
end
-
#returns the env set
- def env
+ def env
if @production
:production
else
:test
end
end
#errors on last call
def errors?
- @errors
+ @errors
end
-
- def list(args='')
- @base_url+ "#{@merchant_id}/"+ self.class.name.to_s.downcase+"/"+args
+ def list(search_params)
+ get(search_params.to_s)
end
def each
all.each do |line|
yield line
@@ -46,116 +42,117 @@
end
def delete_all
if env == :production
- raise OpenpayException.new('delete_all method cannot be used on production',false)
+ raise OpenpayException.new('delete_all method cannot be used on production', false)
end
each do |res|
self.delete(res['id'])
end
end
def get(args='')
- @errors=false
+ @errors = false
+ terminated = true
- LOG.debug("#{self.class.name.downcase}:")
- LOG.debug(" GET Resource URL:#{url(args)}")
+ if is_filter_string?(args)
+ terminated = false
+ end
+
+ LOG.debug("#{resource_name}:")
+ LOG.debug(" GET Resource URL:#{url(args, terminated)}")
res=RestClient::Request.new(
:method => :get,
- :url => url(args),
+ :url => url(args, terminated),
:user => @private_key,
:timeout => @timeout,
:headers => {:accept => :json,
:content_type => :json,
:user_agent => 'Openpay/v1 Ruby-API',
}
)
json_out=nil
begin
json_out=res.execute
- #exceptions
+ #exceptions
rescue Exception => e
@errors=true
#will raise the appropriate exception and return
OpenpayExceptionFactory::create(e)
end
JSON[json_out]
end
- def delete(args)
+ def delete(args)
- @errors=false
+ @errors=false
- LOG.debug("#{self.class.name.downcase}:")
- LOG.debug(" DELETE URL:#{url(args)}")
+ LOG.debug("#{self.class.name.downcase}:")
+ LOG.debug(" DELETE URL:#{url(args)}")
- res=''
- req=RestClient::Request.new(
- :method => :delete,
- :url => url(args),
- :user => @private_key,
- :timeout => @timeout,
- :headers => {:accept => :json,
- :content_type => :json,
- :user_agent => 'Openpay/v1 Ruby-API',
- }
- )
+ res=''
+ req=RestClient::Request.new(
+ :method => :delete,
+ :url => url(args),
+ :user => @private_key,
+ :timeout => @timeout,
+ :headers => {:accept => :json,
+ :content_type => :json,
+ :user_agent => 'Openpay/v1 Ruby-API',
+ }
+ )
- begin
+ begin
res=req.execute
- #exceptions
- rescue Exception => e
- @errors=true
- #will raise the appropriate exception and return
- OpenpayExceptionFactory::create(e)
+ #exceptions
+ rescue Exception => e
+ @errors=true
+ #will raise the appropriate exception and return
+ OpenpayExceptionFactory::create(e)
+ end
+ #returns a hash
+ JSON[res] if not res.empty?
end
- #returns a hash
- JSON[res] if not res.empty?
+ def post(message, args='')
- end
-
- def post(message,args='')
-
- return_hash=false
@errors=false
if message.is_a?(Hash)
return_hash=true
json= hash2json message
else
json=message
return_hash=false
end
- LOG.debug("#{self.class.name.downcase}:")
- LOG.debug " POST URL:#{url(args)}"
- #For security reasons we keep it hide
- #LOG.debug " json: #{json}"
+ # LOG.debug("#{self.class.name.downcase}:")
+ LOG.debug " POST URL:#{url(args)}"
+ LOG.debug " json: #{json}"
begin
- #request
- res= RestClient::Request.new(
+ #request
+ res= RestClient::Request.new(
:method => :post,
- :url => url(args) ,
+ :url => url(args),
:user => @private_key,
:timeout => @timeout,
:payload => json,
:headers => {:accept => :json,
:content_type => :json,
:user_agent => 'Openpay/v1 Ruby-API',
:json => json}
- ) .execute
+ ).execute
- #exceptions
+ #exceptions
rescue Exception => e
@errors=true
#will raise the appropriate exception and return
OpenpayExceptionFactory::create(e)
end
@@ -167,25 +164,22 @@
res
end
end
+ def put(message, args='')
-
- def put (message,args='')
-
return_hash=false
if message.is_a?(Hash)
return_hash=true
json= hash2json message
else
json=message
return_hash=false
end
-
LOG.info "PUT URL:#{url}"
#LOG.info " json: #{json}"
begin
res= RestClient::Request.new(
@@ -196,47 +190,53 @@
:payload => json,
:headers => {:accept => :json,
:content_type => :json,
:user_agent => 'Openpay/v1 Ruby-API',
:json => json}
- ) .execute
- rescue RestClient::BadRequest => e
+ ).execute
+ rescue RestClient::BadRequest => e
warn e.http_body
@errors=true
- return JSON.parse e.http_body
+ return JSON.parse e.http_body
end
-
if return_hash
- JSON.parse res
+ JSON.parse res
else
- res
+ res
end
end
-
#aliases for rest verbs
alias_method :all, :get
- alias_method :list, :get
alias_method :update, :put
- alias_method :create , :post
+ alias_method :create, :post
-
def hash2json(jash)
JSON.generate(jash)
end
def json2hash(json)
JSON[json]
end
+ private
- private
- def url(args='')
- @base_url+"#{@merchant_id}/"+ self.class.name.to_s.downcase+ '/'+args
+ def url(args = '', terminated = true)
+ termination = terminated ? '/' : ''
+ @base_url + "#{@merchant_id}/" + resource_name + termination + args
end
+ def resource_name
+ self.class.name.to_s.downcase
+ end
+ def is_filter_string?(args)
+ is_filter = false
+ if args =~ /^\?/
+ is_filter = true
+ end
+ is_filter
+ end
-
-end
\ No newline at end of file
+end