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.
-
.post(path, options = {}, &block) ⇒ HTTParty::Response
private
Fix bad XML from YM API by using custom parser.
-
.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
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 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/your_membership/base.rb', line 65 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.
16 17 18 19 20 21 22 23 24 |
# File 'lib/your_membership/base.rb', line 16 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 |
.post(path, options = {}, &block) ⇒ HTTParty::Response
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Fix bad XML from YM API by using custom parser.
31 32 33 34 |
# File 'lib/your_membership/base.rb', line 31 def self.post(path, = {}, &block) opt = .merge(parser: ::HTTParty::YMXMLParser) super(path, opt, &block) 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.
137 138 139 140 141 142 143 144 |
# File 'lib/your_membership/base.rb', line 137 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.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/your_membership/base.rb', line 113 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.
40 41 42 43 44 45 46 |
# File 'lib/your_membership/base.rb', line 40 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.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/your_membership/base.rb', line 52 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 |