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) ⇒ OpenPayResource

Returns a new instance of OpenPayResource.



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

def initialize(merchant_id, private_key, production=false,timeout=90)
  @merchant_id=merchant_id
  @private_key=private_key
  #assigns base url depending the requested env
  @base_url=OpenpayApi::base_url(production)
  @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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/openpay/open_pay_resource.rb', line 95

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



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

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



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

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

#envObject

returns the env set



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

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

#errors?Boolean

errors on last call

Returns:

  • (Boolean)


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

def errors?
  @errors
end

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



56
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
89
90
91
92
93
# File 'lib/openpay/open_pay_resource.rb', line 56

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

#hash2json(jash) ⇒ Object



232
233
234
# File 'lib/openpay/open_pay_resource.rb', line 232

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

#json2hash(json) ⇒ Object



236
237
238
# File 'lib/openpay/open_pay_resource.rb', line 236

def json2hash(json)
  JSON[json]
end

#list(search_params) ⇒ Object



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

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

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
# File 'lib/openpay/open_pay_resource.rb', line 131

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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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
# File 'lib/openpay/open_pay_resource.rb', line 182

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