Module: StripeTester

Defined in:
lib/stripe_tester.rb,
lib/stripe_tester/version.rb

Constant Summary collapse

LATEST_STRIPE_VERSION =
"2015-10-16"
VERSION =
"0.5.0"

Class Method Summary collapse

Class Method Details

.basic_authentication_passwordObject



136
137
138
# File 'lib/stripe_tester.rb', line 136

def self.basic_authentication_password
  @webhook_password || self.webhook_url.password
end

.create_event(callback_type, attributes = {}, options = {method: :overwrite}) ⇒ Object

send the url the webhook event There are two options you can use. :method=>:overwrite, or :method=>:merge Each will use a different way of merging the new attributes.



14
15
16
17
18
# File 'lib/stripe_tester.rb', line 14

def self.create_event(callback_type, attributes={}, options={method: :overwrite})
  webhook_data = self.load_template(callback_type, attributes, options)

  post_to_url(webhook_data) if webhook_data
end

.load_template(callback_type, attributes = {}, options = {method: :overwrite}) ⇒ Object

load yaml with specified callback type



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

def self.load_template(callback_type, attributes={}, options={method: :overwrite})
  spec = Gem::Specification.find_by_name("stripe_tester")
  gem_root = spec.gem_dir
  version = StripeTester.stripe_version

  path = gem_root + "/stripe_webhooks/#{version}/#{callback_type}.yml"
  if File.exists?(path)
    template = indifferent(Psych.load_file(path))
    
    unless attributes.empty?
      if options[:method] == :merge
        template = merge_attributes(template, attributes)
      else
        template = overwrite_attributes(template, attributes)
      end
    end

    return template
  else
    raise "Webhook not found. Please use a correct webhook type or correct Stripe version"
  end
end

.merge_attributes(original_attributes, new_attributes = {}) ⇒ Object

deep merge original_attributes with new_attributes



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/stripe_tester.rb', line 45

def self.merge_attributes(original_attributes, new_attributes={})
  original_attributes = original_attributes.clone
  if new_attributes
    indifferent(new_attributes).each do |key, value|
      if value.is_a?(Hash) && original_attributes[key].is_a?(Hash)
        original_attributes[key] = self.merge_attributes original_attributes[key], value
      else
        original_attributes[key] = value
      end
    end
  end
  original_attributes
end

.overwrite_attributes(original_data, attributes = {}) ⇒ Object

replace multiple values for multiple keys in a hash



21
22
23
24
25
26
27
28
29
# File 'lib/stripe_tester.rb', line 21

def self.overwrite_attributes(original_data, attributes={})
  data = original_data.clone
  if attributes
    indifferent(attributes).each do |k,v|
      replace_value(data, k, v)
    end
  end
  data
end

.post_to_url(data = {}) ⇒ Object



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
# File 'lib/stripe_tester.rb', line 59

def self.post_to_url(data={})
  post_url = webhook_url

  if post_url
    # set up request
    req = Net::HTTP::Post.new(post_url.path)
    req.content_type = 'application/json'
    req.body = data.to_json
    req.basic_auth 'stripe', self.basic_authentication_password if !self.basic_authentication_password.nil?
    http_object = Net::HTTP.new(post_url.hostname, post_url.port)
    http_object.use_ssl = true if post_url.scheme == 'https'
    http_object.verify_mode = OpenSSL::SSL::VERIFY_NONE if (!verify_ssl? && http_object.use_ssl?)

    # send request
    res = http_object.start do |http|
      http.request(req)
    end

    case res
      when Net::HTTPSuccess, Net::HTTPRedirection
        true
      else
        res.value
    end
  else
    raise "Could not post to URL. Please set URL."
  end
end

.remove_urlObject



124
125
126
# File 'lib/stripe_tester.rb', line 124

def self.remove_url
  @url = nil
end

.replace_value(hash, key, new_value) ⇒ Object

replaces a value for a single key in a hash



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/stripe_tester.rb', line 32

def self.replace_value(hash, key, new_value)
  if hash.key?(key)
    hash[key] = new_value
  else
    hash.values.each do |value|
      value = value.first if value.is_a?(Array)
      replace_value(value, key, new_value) if value.is_a?(Hash)
    end
    hash
  end
end

.stripe_versionObject



144
145
146
# File 'lib/stripe_tester.rb', line 144

def self.stripe_version
  @version ? @version : LATEST_STRIPE_VERSION
end

.stripe_version=(version) ⇒ Object



140
141
142
# File 'lib/stripe_tester.rb', line 140

def self.stripe_version=(version)
  @version = version
end

.verify_ssl=(verify) ⇒ Object



148
149
150
# File 'lib/stripe_tester.rb', line 148

def self.verify_ssl=(verify)
  @verify_ssl = verify
end

.verify_ssl?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/stripe_tester.rb', line 152

def self.verify_ssl?
  !@verify_ssl.nil? ? @verify_ssl : true
end

.webhook_passwordObject



132
133
134
# File 'lib/stripe_tester.rb', line 132

def self.webhook_password
  @webhook_password
end

.webhook_password=(webhook_password) ⇒ Object



128
129
130
# File 'lib/stripe_tester.rb', line 128

def self.webhook_password=(webhook_password)
  @webhook_password =  webhook_password
end

.webhook_urlObject



120
121
122
# File 'lib/stripe_tester.rb', line 120

def self.webhook_url
  @url
end

.webhook_url=(url) ⇒ Object

save the url and a URI object



113
114
115
116
117
118
# File 'lib/stripe_tester.rb', line 113

def self.webhook_url=(url)
  if url =~ /^#{URI::regexp}$/
    temp_url = URI.parse(url)
    @url = temp_url
  end
end