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



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_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



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.

Returns:

  • (HTTParty::Response)


31
32
33
34
# File 'lib/your_membership/base.rb', line 31

def self.post(path, options = {}, &block)
  opt = options.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.

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.



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.

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.



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.

Parameters:

  • response (Hash)

Returns:

  • (Boolean)

    true if no errors found.

Raises:

  • (HTTParty::ResponseError)

    if a communication error is found.



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.

Parameters:

  • response (Hash)

Returns:

  • (Boolean)

    false if no error is found

Raises:



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