Class: OpenPayResource

Inherits:
Object
  • Object
show all
Defined in:
lib/openpay/open_pay_resource.rb

Overview

This class is the abstract base class for other Openpay resources This class defines the basic rest verbs making use of the rest-api gem. Method aliases are created to provide friendly names.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(merchant_id, private_key, production = false, timeout = 90, country) ⇒ OpenPayResource

Returns a new instance of OpenPayResource.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/openpay/open_pay_resource.rb', line 8

def initialize(merchant_id, private_key, production = false, timeout = 90, country)
  @merchant_id = merchant_id
  @private_key = private_key
  #assigns base url depending the requested env
  @country = country
  @base_url = OpenpayApi::base_url(production, country)
  @errors = false
  @production = production
  @timeout = timeout
  #created resources should have a hook with the base class to keep control of created resources
  @api_hook = nil
end

Instance Attribute Details

#api_hookObject

Returns the value of attribute api_hook.



6
7
8
# File 'lib/openpay/open_pay_resource.rb', line 6

def api_hook
  @api_hook
end

Instance Method Details

#delete(args) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/openpay/open_pay_resource.rb', line 115

def delete(args)

  @errors = false

  strUrl = url(args)
  strUrlAux = delete_ending_slash(strUrl)

  LOG.debug("#{self.class.name.downcase}:")
  LOG.debug("    DELETE  URL:#{strUrlAux}")

  res = ''
  req = RestClient::Request.new(
    :method => :delete,
    :url => strUrlAux,
    :user => @private_key,
    :timeout => @timeout,
    :ssl_version => :TLSv1_2,
    :headers => { :accept => :json,
                  :content_type => :json,
                  :user_agent => 'Openpay/v1  Ruby-API',
    }
  )

  begin
    res = req.execute
    #exceptions
  rescue Exception => e
    @errors = true
    #will raise the appropriate exception and return
    OpenpayExceptionFactory::create(e)
  end
  #returns a hash
  JSON[res] if not res.empty?
end

#delete_allObject



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

def delete_all

  if env == :production
    raise OpenpayException.new('delete_all method cannot be used on production', false)
  end

  each do |res|
    self.delete(res['id'])
  end

end

#eachObject



39
40
41
42
43
# File 'lib/openpay/open_pay_resource.rb', line 39

def each
  all.each do |line|
    yield line
  end
end

#envObject

returns the env set



22
23
24
25
26
27
28
# File 'lib/openpay/open_pay_resource.rb', line 22

def env
  if @production
    :production
  else
    :test
  end
end

#errors?Boolean

errors on last call

Returns:

  • (Boolean)


31
32
33
# File 'lib/openpay/open_pay_resource.rb', line 31

def errors?
  @errors
end

#get(args = '') ⇒ Object Also known as: all



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
# File 'lib/openpay/open_pay_resource.rb', line 57

def get(args = '')
  @errors = false
  terminated = true
  if is_filter_string?(args)
    terminated = false
  end
  strUrl = url(args, terminated)
  strUrlAux = delete_ending_slash(strUrl)
  LOG.debug("#{resource_name}:")
  LOG.debug("   GET Resource URL:#{url(args, terminated)}")
  res = RestClient::Request.new(
    :method => :get,
    :url => strUrlAux,
    :user => @private_key,
    :timeout => @timeout,
    :ssl_version => :TLSv1_2,
    :headers => { :accept => :json,
                  :content_type => :json,
                  :user_agent => 'Openpay/v1  Ruby-API',
    }
  )
  json_out = nil
  begin
    json_out = res.execute
    #exceptions
  rescue Exception => e
    @errors = true
    #will raise the appropriate exception and return
    OpenpayExceptionFactory::create(e)
  end
  JSON[json_out]
end

#get_with_custom_url(url = '') ⇒ Object



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

def get_with_custom_url(url = '')
  @errors = false
  res = RestClient::Request.new(
    :method => :get,
    :url => url,
    :user => @private_key,
    :timeout => @timeout,
    :ssl_version => :TLSv1_2,
    :headers => { :accept => :json,
                  :content_type => :json,
                  :user_agent => 'Openpay/v1  Ruby-API',
    }
  )
  json_out = nil
  begin
    json_out = res.execute
    #exceptions
  rescue Exception => e
    @errors = true
    #will raise the appropriate exception and return
    OpenpayExceptionFactory::create(e)
  end
  JSON[json_out]
end

#hash2json(jash) ⇒ Object



250
251
252
# File 'lib/openpay/open_pay_resource.rb', line 250

def hash2json(jash)
  JSON.generate(jash)
end

#json2hash(json) ⇒ Object



254
255
256
# File 'lib/openpay/open_pay_resource.rb', line 254

def json2hash(json)
  JSON[json]
end

#list(search_params) ⇒ Object



35
36
37
# File 'lib/openpay/open_pay_resource.rb', line 35

def list(search_params)
  get(search_params.to_s)
end

#post(message, args = '') ⇒ Object Also known as: create



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/openpay/open_pay_resource.rb', line 150

def post(message, args = '')

  @errors = false

  if message.is_a?(Hash)
    return_hash = true
    json = hash2json message
  else
    json = message
    return_hash = false
  end

  strUrl = url(args)
  strUrlAux = delete_ending_slash(strUrl)

  # LOG.debug("#{self.class.name.downcase}:")
  LOG.debug "   POST URL:#{strUrlAux}"
  LOG.debug "   json: #{json}"

  begin
    #request
    res = RestClient::Request.new(
      :method => :post,
      :url => strUrlAux,
      :user => @private_key,
      :timeout => @timeout,
      :ssl_version => :TLSv1_2,
      :payload => json,
      :headers => { :accept => :json,
                    :content_type => :json,
                    :user_agent => 'Openpay/v1  Ruby-API',
                    :json => json }
    ).execute

    #exceptions
  rescue Exception => e
    @errors = true
    #will raise the appropriate exception and return
    OpenpayExceptionFactory::create(e)
  end

  #return
  if return_hash
    JSON.parse res
  else
    res
  end

end

#put(message, args = '') ⇒ Object Also known as: update



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/openpay/open_pay_resource.rb', line 200

def put(message, args = '')

  return_hash = false

  if message.is_a?(Hash)
    return_hash = true
    json = hash2json message
  else
    json = message
    return_hash = false
  end

  strUrl = url(args)
  strUrlAux = delete_ending_slash(strUrl)

  LOG.info "PUT URL:#{strUrlAux}"
  #LOG.info "   json: #{json}"

  begin
    res = RestClient::Request.new(
      :method => :put,
      :url => strUrlAux,
      :user => @private_key,
      :timeout => @timeout,
      :ssl_version => :TLSv1_2,
      :payload => json,
      :headers => { :accept => :json,
                    :content_type => :json,
                    :user_agent => 'Openpay/v1  Ruby-API',
                    :json => json }
    ).execute
  rescue RestClient::BadRequest => e
    warn e.http_body
    @errors = true
    return JSON.parse e.http_body
  end

  if return_hash
    JSON.parse res
  else
    res
  end

end