Class: Braintree::CustomerRecommendationsPayload

Inherits:
Object
  • Object
show all
Includes:
BaseModule
Defined in:
lib/braintree/graphql/types/customer_recommendations_payload.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseModule

included

Methods included from BaseModule::Methods

#copy_instance_variables_from_object, #return_object_or_raise, #set_instance_variables_from_hash, #singleton_class

Constructor Details

#initialize(attributes) ⇒ CustomerRecommendationsPayload

Returns a new instance of CustomerRecommendationsPayload.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/braintree/graphql/types/customer_recommendations_payload.rb', line 13

def initialize(attributes)
  @attrs = [:is_in_paypal_network, :recommendations]

  if attributes.key?(:response)
    response = attributes[:response]
    # Constructor for response map
    begin
      @is_in_paypal_network = _get_value(response, "generateCustomerRecommendations.isInPayPalNetwork")
      @recommendations = _extract_recommendations(response)
    rescue => e
      puts e.backtrace.join("\n")
      raise
    end
  else
    @is_in_paypal_network = attributes[:is_in_paypal_network]
    @recommendations = attributes[:recommendations]
  end
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



9
10
11
# File 'lib/braintree/graphql/types/customer_recommendations_payload.rb', line 9

def attrs
  @attrs
end

#is_in_paypal_networkObject (readonly)

Returns the value of attribute is_in_paypal_network.



10
11
12
# File 'lib/braintree/graphql/types/customer_recommendations_payload.rb', line 10

def is_in_paypal_network
  @is_in_paypal_network
end

#recommendationsObject (readonly)

Returns the value of attribute recommendations.



11
12
13
# File 'lib/braintree/graphql/types/customer_recommendations_payload.rb', line 11

def recommendations
  @recommendations
end

Class Method Details

._new(*args) ⇒ Object



120
121
122
# File 'lib/braintree/graphql/types/customer_recommendations_payload.rb', line 120

def self._new(*args)
  self.new(*args)
end

Instance Method Details

#_extract_recommendations(response) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
72
73
# File 'lib/braintree/graphql/types/customer_recommendations_payload.rb', line 32

def _extract_recommendations(response)
  begin
    payment_recommendations = _get_value(response, "generateCustomerRecommendations.paymentRecommendations")

    payment_options_list = []
    payment_recommendations_list = []

    payment_recommendations.each_with_index do |recommendation, _i|
      recommended_priority = _get_value(recommendation, "recommendedPriority")
      payment_option_string = _get_value(recommendation, "paymentOption")

      begin
        payment_option = payment_option_string

        payment_option_obj = Braintree::PaymentOptions._new({
          paymentOption: payment_option,
          recommendedPriority: recommended_priority
        })

        payment_recommendation_obj = Braintree::PaymentRecommendations._new({
          paymentOption: payment_option,
          recommendedPriority: recommended_priority
        })

        payment_options_list << payment_option_obj
        payment_recommendations_list << payment_recommendation_obj
      rescue => e
        puts e.backtrace.join("\n")
        raise
      end
    end

    customer_recommendations = CustomerRecommendations._new({
      payment_recommendations: payment_recommendations_list
    })

    return customer_recommendations
  rescue => e
    puts e.backtrace.join("\n")
    raise ServerError.new("Error extracting recommendations: #{e.message}")
  end
end

#_get_value(response, key) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/braintree/graphql/types/customer_recommendations_payload.rb', line 75

def _get_value(response, key)
  current_map = response
  key_parts = key.split(".")

  # Navigate through nested dictionaries for all but last key
  (0...key_parts.length - 1).each do |i|
    sub_key = key_parts[i]
    current_map = _pop_value(current_map, sub_key)
  end

  # Get the final value
  last_key = key_parts[-1]
  value = _pop_value(current_map, last_key)
  return value
end

#_pop_value(response, key) ⇒ Object

Raises:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/braintree/graphql/types/customer_recommendations_payload.rb', line 91

def _pop_value(response, key)

# Try as string first
if response.key?(key)
    return response[key]
end

# Then try as symbol
symkey = key.to_sym
if response.key?(symkey)
    return response[symkey]
end

# Finally try as string with string keys
if response.key?(key.to_s)
    return response[key.to_s]
end

raise ServerError.new("Couldn't parse response")
end

#inspectObject



111
112
113
114
# File 'lib/braintree/graphql/types/customer_recommendations_payload.rb', line 111

def inspect
  inspected_attributes = @attrs.map { |attr| "#{attr}:#{send(attr).inspect}" }
  "#<#{self.class} #{inspected_attributes.join(" ")}>"
end