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', field_to_return: 'Merchants', action_type: :get, default_params: {} }, purchase_card: { url: '/gcp/services/BuyatabWS.asmx/PurchaseCard', field_to_return: 'BATOrderNumber', action_type: :post, 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.
41 42 43 44 |
# File 'lib/buyatabwrapper/api.rb', line 41 def initialize self.result = Result.new self.config = parse_config_file end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
39 40 41 |
# File 'lib/buyatabwrapper/api.rb', line 39 def config @config end |
#result ⇒ Object
Returns the value of attribute result.
39 40 41 |
# File 'lib/buyatabwrapper/api.rb', line 39 def result @result end |
Instance Method Details
#send_request(type, params) ⇒ Object
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 |
# File 'lib/buyatabwrapper/api.rb', line 46 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 CALLS[type][:action_type] == :get self.result = response[CALLS[type][:field_to_return]] else result.response = response[CALLS[type][:field_to_return]] result.successful! end else result.response = response["Status"]['Error']['Message'] end result end |