Class: YourMembership::Base
- Inherits:
-
Object
- Object
- YourMembership::Base
- Includes:
- HTTParty
- Defined in:
- lib/your_membership/base.rb
Overview
Base Class inherited by all Your Membership SDK Classes.
Direct Known Subclasses
Convert, Events, Feeds, Member, Members, People, Sa::Auth, Sa::Certifications, Sa::Commerce, Sa::Events, Sa::Export, Sa::Groups, Sa::Member, Sa::Members, Sa::NonMembers, Sa::People, Session
Constant Summary collapse
- @@genericCallID =
Call IDs are usually tied to sessions, this is a unique call id for use whenever a session is not needed.
nil
Class Method Summary collapse
-
.build_XML_request(callMethod, session = nil, params = {}) ⇒ Object
Creates an XML string to send to the API.
-
.new_call_id ⇒ Integer
Auto Increments ad returns the genericCallID as required by the YourMembership.com API.
-
.response_to_array(response_body, keys = [], key_for_array) ⇒ Array
Converts the desired portion of the XML response to a single dimension array.
-
.response_to_array_of_hashes(response_body, keys = []) ⇒ Array
This is a helper method to always return an array (potentially empty) of responses (Hashes) for methods that can have multiple results.
-
.response_valid?(response) ⇒ Boolean
A Guard Method that returns true if the response from the API can be processed and raises an exception if not.
-
.response_ym_error?(response) ⇒ Boolean
Checks for error codes in the API response and raises an exception if an error is found.
Class Method Details
.build_XML_request(callMethod, session = nil, params = {}) ⇒ Object
THIS SHOULD BE MARKED PRIVATE and refactored to DRY up the calls.
Creates an XML string to send to the API
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/your_membership/base.rb', line 54 def self.build_XML_request(callMethod, session = nil, params = {}) # rubocop:disable Style/MethodLength, Style/MethodName builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml| # Root Node Is always <YourMembership> xml.YourMembership do # API Version xml.Version_ YourMembership.config[:version] # API Key: For System Administrative tasks it is the private key and # passcode, for all others it is the public key if callMethod.downcase.start_with?('sa.') xml.ApiKey_ YourMembership.config[:privateKey] xml.SaPasscode_ YourMembership.config[:saPasscode] else xml.ApiKey_ YourMembership.config[:publicKey] end # Pass Session ID and Session Call ID unless there is no session, then # send call id from class if session if session.is_a? YourMembership::Session xml.SessionID_ session.session_id else xml.SessionID_ session end xml.CallID_ session.call_id else xml.CallID_ new_call_id end xml.Call(:Method => callMethod) do params.each do |key, value| xml_process(key, value, xml) end end end end builder.to_xml end |
.new_call_id ⇒ Integer
Returns Auto Increments ad returns the genericCallID as required by the YourMembership.com API.
14 15 16 17 18 19 20 21 22 |
# File 'lib/your_membership/base.rb', line 14 def self.new_call_id if @@genericCallID.nil? # We start with a very high number to avoid conflicts when initiating a new session. @@genericCallID = 10_000 else @@genericCallID += 1 end @@genericCallID end |
.response_to_array(response_body, keys = [], key_for_array) ⇒ Array
Converts the desired portion of the XML response to a single dimension array. This is useful when you don’t have a need for key, value pairs and want a clean array of values to work with.
126 127 128 129 130 131 132 133 |
# File 'lib/your_membership/base.rb', line 126 def self.response_to_array(response_body, keys = [], key_for_array) return_array = [] response_hash_array = response_to_array_of_hashes(response_body, keys) response_hash_array.each do |item| return_array.push item[key_for_array] end return_array end |
.response_to_array_of_hashes(response_body, keys = []) ⇒ Array
This is a helper method to always return an array (potentially empty) of responses (Hashes) for methods that can have multiple results. The default behavior of the API is to return a nil if no records are found, a hash if one record is found and an array if multiple records are found.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/your_membership/base.rb', line 102 def self.response_to_array_of_hashes(response_body, keys = []) return_array = [] if response_body # http://stackoverflow.com/questions/13259181/what-is-the-most-ruby-ish-way-of-accessing-nested-hash-values-at-arbitrary-depth response_body_items = keys.reduce(response_body) { |h, key| h[key] } if response_body_items.class == Array response_body_items.each do |response_item| return_array.push response_item end else return_array.push response_body_items end end return_array end |
.response_valid?(response) ⇒ Boolean
A Guard Method that returns true if the response from the API can be processed and raises an exception if not.
29 30 31 32 33 34 35 |
# File 'lib/your_membership/base.rb', line 29 def self.response_valid?(response) if response.success? !response_ym_error?(response) else raise HTTParty::ResponseError.new(response), 'Connection to YourMembership API failed.' end end |
.response_ym_error?(response) ⇒ Boolean
Checks for error codes in the API response and raises an exception if an error is found.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/your_membership/base.rb', line 41 def self.response_ym_error?(response) if response['YourMembership_Response']['ErrCode'] != '0' raise YourMembership::Error.new( response['YourMembership_Response']['ErrCode'], response['YourMembership_Response']['ErrDesc'] ) else return false end end |