class OpenPayResource

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.

Attributes

api_hook[RW]

Public Class Methods

new(merchant_id, private_key, production=false,timeout=90) click to toggle source
# File lib/openpay/open_pay_resource.rb, line 8
def initialize(merchant_id, private_key, production=false,timeout=90)
  @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=timeout
  #created resources should have a hook with the base class to keep control of created resources
  @api_hook=nil
end

Public Instance Methods

all(args='')

aliases for rest verbs

Alias for: get
create(message, args='')
Alias for: post
delete(args) click to toggle source
# File lib/openpay/open_pay_resource.rb, line 95
def delete(args)

  @errors=false

  
  strUrl = url(args)
  strUrlAux = delete_ending_slash(strUrl)

  LOG.debug("#{self.class.name.downcase}:")
  LOG.debug("    DELETE  URL:#{strUrlAux}")

  res=''
  req=RestClient::Request.new(
      :method => :delete,
      :url => strUrlAux,
      :user => @private_key,
      :timeout => @timeout,
      :ssl_version => :TLSv1_2,
      :headers => {:accept => :json,
                   :content_type => :json,
                   :user_agent => 'Openpay/v1  Ruby-API',
      }
  )

  begin
    res=req.execute
      #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
delete_all() click to toggle source
# File lib/openpay/open_pay_resource.rb, line 44
def delete_all

  if env == :production
    raise OpenpayException.new('delete_all method cannot be used on production', false)
  end

  each do |res|
    self.delete(res['id'])
  end

end
each() { |line| ... } click to toggle source
# File lib/openpay/open_pay_resource.rb, line 38
def each
  all.each do |line|
    yield line
  end
end
env() click to toggle source

returns the env set

# File lib/openpay/open_pay_resource.rb, line 21
def env
  if @production
    :production
  else
    :test
  end
end
errors?() click to toggle source

errors on last call

# File lib/openpay/open_pay_resource.rb, line 30
def errors?
  @errors
end
get(args='') click to toggle source
# File lib/openpay/open_pay_resource.rb, line 56
def get(args='')

  @errors = false
  terminated = true

  if is_filter_string?(args)
    terminated = false
  end

  strUrl = url(args, terminated)
  strUrlAux = delete_ending_slash(strUrl)

  LOG.debug("#{resource_name}:")
  LOG.debug("   GET Resource URL:#{url(args, terminated)}")
  res=RestClient::Request.new(
      :method => :get,
      :url => strUrlAux,
      :user => @private_key,
      :timeout => @timeout,
      :ssl_version => :TLSv1_2,
      :headers => {:accept => :json,
                   :content_type => :json,
                   :user_agent => 'Openpay/v1  Ruby-API',
      }
  )
  json_out=nil
  begin
    json_out=res.execute
      #exceptions
  rescue Exception => e
    @errors=true
    #will raise the appropriate exception and return
    OpenpayExceptionFactory::create(e)
  end

  JSON[json_out]

end
Also aliased as: all
hash2json(jash) click to toggle source
# File lib/openpay/open_pay_resource.rb, line 232
def hash2json(jash)
  JSON.generate(jash)
end
json2hash(json) click to toggle source
# File lib/openpay/open_pay_resource.rb, line 236
def json2hash(json)
  JSON[json]
end
list(search_params) click to toggle source
# File lib/openpay/open_pay_resource.rb, line 34
def list(search_params)
  get(search_params.to_s)
end
post(message, args='') click to toggle source
# File lib/openpay/open_pay_resource.rb, line 131
def post(message, args='')

  @errors=false

  if message.is_a?(Hash)
    return_hash=true
    json= hash2json message
  else
    json=message
    return_hash=false
  end

  
  strUrl = url(args)
  strUrlAux = delete_ending_slash(strUrl)

  # LOG.debug("#{self.class.name.downcase}:")
   LOG.debug "   POST URL:#{strUrlAux}"
   LOG.debug "   json: #{json}"

  begin
    #request
    res= RestClient::Request.new(
        :method => :post,
        :url => strUrlAux,
        :user => @private_key,
        :timeout => @timeout,
        :ssl_version => :TLSv1_2,
        :payload => json,
        :headers => {:accept => :json,
                     :content_type => :json,
                     :user_agent => 'Openpay/v1  Ruby-API',
                     :json => json}
    ).execute

      #exceptions
  rescue Exception => e
    @errors=true
    #will raise the appropriate exception and return
    OpenpayExceptionFactory::create(e)
  end

  #return
  if return_hash
    JSON.parse res
  else
    res
  end

end
Also aliased as: create
put(message, args='') click to toggle source
# File lib/openpay/open_pay_resource.rb, line 182
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

  strUrl = url(args)
  strUrlAux = delete_ending_slash(strUrl)

  LOG.info "PUT URL:#{strUrlAux}"
  #LOG.info "   json: #{json}"

  begin 
    res= RestClient::Request.new(
        :method => :put,
        :url => strUrlAux,
        :user => @private_key,
        :timeout => @timeout,
        :ssl_version => :TLSv1_2,
        :payload => json,
        :headers => {:accept => :json,
                     :content_type => :json,
                     :user_agent => 'Openpay/v1  Ruby-API',
                     :json => json}
    ).execute
  rescue RestClient::BadRequest => e
    warn e.http_body
    @errors=true
    return JSON.parse e.http_body
  end

  if return_hash
    JSON.parse res
  else
    res
  end

end
Also aliased as: update
update(message, args='')
Alias for: put

Private Instance Methods

delete_ending_slash(url) click to toggle source
# File lib/openpay/open_pay_resource.rb, line 254
def delete_ending_slash(url)
  slash = '/'
  strUrl = url
  strUrlAux = url
  ending = strUrl.to_s[-1,1]
  if ending == slash
    strUrlAux = strUrl.to_s[0,strUrl.length - 1]
  end
  strUrlAux
end
is_filter_string?(args) click to toggle source
# File lib/openpay/open_pay_resource.rb, line 265
def is_filter_string?(args)
  is_filter = false
  if args =~ /^\?/
     is_filter = true
  end
  is_filter
end
resource_name() click to toggle source
# File lib/openpay/open_pay_resource.rb, line 250
def resource_name
  self.class.name.to_s.downcase
end
url(args = '', terminated = true) click to toggle source
# File lib/openpay/open_pay_resource.rb, line 242
def url(args = '', terminated = true)
  if args.nil? || args.empty?
    terminated = false
  end
  termination = terminated ? '/' : ''
  @base_url + "#{@merchant_id}/" + resource_name + termination + args
end