class ActiveWepay::Base

Constants

PRODUCTION_API_ENDPOINT
PRODUCTION_UI_ENDPOINT
STAGE_API_ENDPOINT
STAGE_UI_ENDPOINT

Attributes

amount[R]
callback_uri[R]
errors[R]
id[R]
name[R]
oauth_token[RW]
redirect_uri[R]
response[R]

Public Class Methods

new(options) click to toggle source
# File lib/activewepay.rb, line 25
def initialize(options)
  @errors = ActiveModel::Errors.new(self)
  @options = options

  options[:oauth_token]  ? @oauth_token = options[:oauth_token] : false
  options[:amount]       ? @amount = options[:amount] : false
  options[:account_id]   ? @account_id = options[:account_id] : false
  options[:redirect_uri] ? @redirect_uri = options[:redirect_uri] : false
  options[:callback_uri] ? @callback_uri = options[:callback_uri] : false
  options[:id]           ? @id = options[:id] : false
  options[:name]         ? @name = options[:name] : false
end

Public Instance Methods

call(path, access_token = false, params = false) click to toggle source

make a call to the WePay API

# File lib/activewepay.rb, line 39
def call(path, access_token = false, params = false)
  if Rails.env == 'development' || Rails.env == 'test'
    api_endpoint = STAGE_API_ENDPOINT
    ui_endpoint = STAGE_UI_ENDPOINT
  else
    api_endpoint = PRODUCTION_API_ENDPOINT
    ui_endpoint = PRODUCTION_UI_ENDPOINT
  end
     
  # get the url
  url = URI.parse(api_endpoint + path)
  # construct the call data and access token
  call = Net::HTTP::Post.new(url.path, initheader = {'Content-Type' =>'application/json', 'User-Agent' => 'WePay Ruby SDK'})
  if params
    call.body = params.to_json
  end

  if access_token
    call.add_field('Authorization: Bearer', access_token)
  end

  # create the request object
  request = Net::HTTP.new(url.host, url.port)
  request.use_ssl = true
  # make the call
  response = request.start {|http| http.request(call) }
  # returns JSON response as ruby hash
  @response = JSON.parse!(response.body, :symbolize_names => true)
  
  self
end

Private Instance Methods

method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/activewepay.rb, line 79
def method_missing(method_name, *args, &block)
  if @response and @response.keys.include? method_name.to_sym
    @response[method_name.to_sym]
  elsif @options.keys.include? method_name.to_sym
    @options[method_name.to_sym]
  else
    super
  end
end
validate_response() click to toggle source
# File lib/activewepay.rb, line 73
def validate_response
  if @response && @response[:error]
    @errors.add(@response[:error].to_sym, @response[:error_description])
  end
end