Class: ActiveWepay::Base
- Inherits:
-
Object
- Object
- ActiveWepay::Base
- Includes:
- ActiveModel::Model
- Defined in:
- lib/activewepay.rb
Direct Known Subclasses
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
-
#amount ⇒ Object
readonly
Returns the value of attribute amount.
-
#callback_uri ⇒ Object
readonly
Returns the value of attribute callback_uri.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#oauth_token ⇒ Object
Returns the value of attribute oauth_token.
-
#redirect_uri ⇒ Object
readonly
Returns the value of attribute redirect_uri.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#call(path, access_token = false, params = false) ⇒ Object
make a call to the WePay API.
-
#initialize(options) ⇒ Base
constructor
A new instance of Base.
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() @errors = ActiveModel::Errors.new(self) @options = [:oauth_token] ? @oauth_token = [:oauth_token] : false [:amount] ? @amount = [:amount] : false [:account_id] ? @account_id = [:account_id] : false [:redirect_uri] ? @redirect_uri = [:redirect_uri] : false [:callback_uri] ? @callback_uri = [:callback_uri] : false [:id] ? @id = [:id] : false [:name] ? @name = [: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
#amount ⇒ Object (readonly)
Returns the value of attribute amount.
15 16 17 |
# File 'lib/activewepay.rb', line 15 def amount @amount end |
#callback_uri ⇒ Object (readonly)
Returns the value of attribute callback_uri.
15 16 17 |
# File 'lib/activewepay.rb', line 15 def callback_uri @callback_uri end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
15 16 17 |
# File 'lib/activewepay.rb', line 15 def errors @errors end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
15 16 17 |
# File 'lib/activewepay.rb', line 15 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/activewepay.rb', line 15 def name @name end |
#oauth_token ⇒ Object
Returns the value of attribute oauth_token.
14 15 16 |
# File 'lib/activewepay.rb', line 14 def oauth_token @oauth_token end |
#redirect_uri ⇒ Object (readonly)
Returns the value of attribute redirect_uri.
15 16 17 |
# File 'lib/activewepay.rb', line 15 def redirect_uri @redirect_uri end |
#response ⇒ Object (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 |