Class: YourMembership::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/your_membership/base.rb

Overview

Base Class inherited by all Your Membership SDK Classes.

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

Class Method Details

.build_XML_request(callMethod, session = nil, params = {}) ⇒ Object

TODO:

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_idInteger

Returns Auto Increments ad returns the genericCallID as required by the YourMembership.com API.

Returns:

  • (Integer)

    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.

Parameters:

  • response_body (Hash)

    This is the nodeset that returns nil if an empty data set is returned.

  • keys (Array) (defaults to: [])

    This is a list of keys, in order that are nested inside the response body, these keys will be traversed in order before retrieving the data associated with the key_for_array

  • key_for_array (String)

    this is the key that represents the list of items you want to turn into an array.

Returns:

  • (Array)

    A single dimension array of values.



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.

Parameters:

  • response_body (Hash)

    This is the nodeset that returns nil if an empty data set is returned.

  • keys (Array) (defaults to: [])

    This is a list of keys, in order that are nested inside the response body, these keys will be traversed in order before retrieving the data associated with the last key in the array

Returns:

  • (Array)

    A single dimension array of hashes.



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.

Parameters:

  • response (Hash)

Returns:

  • (Boolean)

    true if no errors found.

Raises:

  • (HTTParty::ResponseError)

    if a communication error is found.



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.

Parameters:

  • response (Hash)

Returns:

  • (Boolean)

    false if no error is found

Raises:



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