Module: MarketingCloudSDK::Rest

Includes:
Targeting
Included in:
Client
Defined in:
lib/marketingcloudsdk/rest.rb

Instance Attribute Summary

Attributes included from Targeting

#access_token, #endpoint, #soap_endpoint

Instance Method Summary collapse

Methods included from Targeting

#cache_file, #get_soap_endpoint_from_file, #set_soap_endpoint_to_file

Methods included from HTTPRequest

#generate_uri, #request

Instance Method Details

#complete_url(url, url_properties) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/marketingcloudsdk/rest.rb', line 66

def complete_url url, url_properties
  normalize_keys(url_properties)
  url = url % url_properties if url_properties
  url.end_with?('/') ? url.chop : url
rescue KeyError => ex
  raise "#{ex.message} to complete #{url}"
end

#get_url_properties(url, properties) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/marketingcloudsdk/rest.rb', line 55

def get_url_properties url, properties
  url_property_names = url.scan(/(%{(.+?)})/).collect{|frmt, name| name}
  url_properties = {}
  properties.keys.each do |k|
    if url_property_names.include? k
      url_properties[k] = properties.delete(k)
    end
  end
  url_properties
end

#normalize_keys(obj) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/marketingcloudsdk/rest.rb', line 46

def normalize_keys obj
  if obj and obj.is_a? Hash
    obj.keys.each do |k|
      obj[(k.to_sym rescue k) || k] = obj.delete(k)
    end
  end
  obj
end

#parse_properties(url, properties) ⇒ Object



74
75
76
77
78
# File 'lib/marketingcloudsdk/rest.rb', line 74

def parse_properties url, properties
  url_properties = get_url_properties url, properties
  url = complete_url url, url_properties
  [url, properties]
end

#rest_clientObject



42
43
44
# File 'lib/marketingcloudsdk/rest.rb', line 42

def rest_client
  self
end

#rest_delete(url, properties = {}) ⇒ Object



85
86
87
88
# File 'lib/marketingcloudsdk/rest.rb', line 85

def rest_delete url, properties={}
  url, properties = parse_properties url, properties
  rest_request :delete, url
end

#rest_get(url, properties = {}) ⇒ Object



80
81
82
83
# File 'lib/marketingcloudsdk/rest.rb', line 80

def rest_get url, properties={}
  url, properties = parse_properties url, properties
  rest_request :get, url, {'params' => properties}
end

#rest_patch(url, properties = {}) ⇒ Object



90
91
92
93
94
# File 'lib/marketingcloudsdk/rest.rb', line 90

def rest_patch url, properties={}
  url, payload = parse_properties url, properties
  rest_request :patch, url, {'data' => payload,
    'content_type' => 'application/json'}
end

#rest_post(url, properties = {}) ⇒ Object



96
97
98
99
100
# File 'lib/marketingcloudsdk/rest.rb', line 96

def rest_post url, properties={}
  url, payload = parse_properties url, properties
  rest_request :post, url, {'data' => payload,
    'content_type' => 'application/json'}
end