Class: ActiveWepay::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/activewepay.rb

Direct Known Subclasses

Account, Checkout, Preapproval, Withdrawal

Constant Summary collapse

STAGE_API_ENDPOINT =
"https://stage.wepayapi.com/v2"
STAGE_UI_ENDPOINT =
"https://stage.wepay.com/v2"
PRODUCTION_API_ENDPOINT =
"https://wepayapi.com/v2"
PRODUCTION_UI_ENDPOINT =
"https://www.wepay.com/v2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.



25
26
27
28
29
30
31
32
33
34
35
36
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



79
80
81
82
83
84
85
86
87
# 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

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



15
16
17
# File 'lib/activewepay.rb', line 15

def amount
  @amount
end

#callback_uriObject (readonly)

Returns the value of attribute callback_uri.



15
16
17
# File 'lib/activewepay.rb', line 15

def callback_uri
  @callback_uri
end

#errorsObject (readonly)

Returns the value of attribute errors.



15
16
17
# File 'lib/activewepay.rb', line 15

def errors
  @errors
end

#idObject (readonly)

Returns the value of attribute id.



15
16
17
# File 'lib/activewepay.rb', line 15

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/activewepay.rb', line 15

def name
  @name
end

#oauth_tokenObject

Returns the value of attribute oauth_token.



14
15
16
# File 'lib/activewepay.rb', line 14

def oauth_token
  @oauth_token
end

#redirect_uriObject (readonly)

Returns the value of attribute redirect_uri.



15
16
17
# File 'lib/activewepay.rb', line 15

def redirect_uri
  @redirect_uri
end

#responseObject (readonly)

Returns the value of attribute response.



15
16
17
# File 'lib/activewepay.rb', line 15

def response
  @response
end

Instance Method Details

#call(path, access_token = false, params = false) ⇒ Object

make a call to the WePay API



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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