Class: Buyatabwrapper::API
- Inherits:
-
Object
- Object
- Buyatabwrapper::API
- Includes:
- Resultable
- Defined in:
- lib/buyatabwrapper/api.rb
Constant Summary collapse
- CALLS =
{ get_merchants: { url: '/gcp/services/BuyatabWS.asmx/GetMerchantList', default_params: {} }, purchase_card: { url: '/gcp/services/BuyatabWS.asmx/PurchaseCard', default_params: { cardstyle_id: '0', msg: '', quantity: '1', recipient_name: '', recipient_email: '', sender_name: '', sender_email: '', handle_delivery: 'true', customer_ref_id: '', opt_in: 'true' } } }
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#result ⇒ Object
Returns the value of attribute result.
Instance Method Summary collapse
-
#initialize ⇒ API
constructor
A new instance of API.
- #send_request(type, params) ⇒ Object
Constructor Details
#initialize ⇒ API
Returns a new instance of API.
37 38 39 40 |
# File 'lib/buyatabwrapper/api.rb', line 37 def initialize self.result = Result.new self.config = parse_config_file end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
35 36 37 |
# File 'lib/buyatabwrapper/api.rb', line 35 def config @config end |
#result ⇒ Object
Returns the value of attribute result.
35 36 37 |
# File 'lib/buyatabwrapper/api.rb', line 35 def result @result end |
Instance Method Details
#send_request(type, params) ⇒ Object
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 70 71 72 |
# File 'lib/buyatabwrapper/api.rb', line 42 def send_request(type, params) return result unless config request = Net::HTTP::Post.new(CALLS[type][:url], initheader = {'Content-Type' => 'application/json'}) request.body = default_params(type).merge(params).to_json http = Net::HTTP.new(config.host, 443).tap do |http| http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end response = http.request(request) response = JSON.parse(response.body)["d"] if response["Status"]["Success"] if type == :get_merchants self.result = response['Merchants'] else result.response = OpenStruct.new( id: response['BATOrderNumber'], url: response['Links'].first, card_number: response['CardNumbers'].first, pin_number: response['CardPins'].first ) result.successful! end else result.response = response["Status"]['Error']['Message'] end result end |