Class: Payrix::Resource::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/payrix/resource/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, attrs) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
# File 'lib/payrix/resource/base.rb', line 6

def initialize(params, attrs)
  @attrs = attrs
  @request_options = Payrix::Http::RequestParams.new

  set(params)
end

Instance Attribute Details

#resource_nameObject (readonly)

Returns the value of attribute resource_name.



4
5
6
# File 'lib/payrix/resource/base.rb', line 4

def resource_name
  @resource_name
end

Instance Method Details

#create(params = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/payrix/resource/base.rb', line 113

def create(params = {})
  set(params)
  
  headers = build_headers
  headers['Content-Type'] = "application/json"
  method = 'post'
  url = Payrix.configuration.url
  endpoint = "#{@resource_name}"
  data = to_json

  body, status = Payrix::Http::Request.instance.send_http(method, url, endpoint, data, headers)
  @response = Payrix::Http::Response.new(body, status, self.class)

  validate_response
end

#delete(params = {}) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/payrix/resource/base.rb', line 153

def delete(params = {})
  set(params)

  if !id 
    if Payrix.configuration.exception_enabled
      raise Payrix::Exceptions::InvalidRequest.new('ID is required for this delete')
    else
      return false
    end
  end

  headers = build_headers
  headers['Content-Type'] = "application/json"
  method = 'delete'
  url = Payrix.configuration.url
  endpoint = "#{@resource_name}/#{id}"
  data = {}

  body, status = Payrix::Http::Request.instance.send_http(method, url, endpoint, data, headers)
  @response = Payrix::Http::Response.new(body, status, self.class)

  validate_response
end

#detailsObject



68
69
70
# File 'lib/payrix/resource/base.rb', line 68

def details
  @response.nil? ? [] : @response.details
end

#has_errors?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/payrix/resource/base.rb', line 60

def has_errors?
  @response.nil? ? false : @response.has_errors?
end

#has_more?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/payrix/resource/base.rb', line 76

def has_more?
  @response.nil? ? true : @response.has_more?
end

#request_optionsObject



13
14
15
# File 'lib/payrix/resource/base.rb', line 13

def request_options
  @request_options
end

#request_options=(params = {}) ⇒ Object



17
18
19
20
21
22
# File 'lib/payrix/resource/base.rb', line 17

def request_options=(params = {})
  @request_options.sort = params['sort'] unless params['sort'].nil?
  @request_options.expand = params['expand'] unless params['expand'].nil?
  @request_options.totals = params['totals'] unless params['totals'].nil?
  @request_options.page = params['page'] unless params['page'].nil?
end

#responseObject



64
65
66
# File 'lib/payrix/resource/base.rb', line 64

def response
  @response.nil? ? [] : @response.response
end

#retrieve(params = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/payrix/resource/base.rb', line 80

def retrieve(params = {})
  set(params)

  headers = build_headers

  search = build_search(to_json)
  search += "&#{request_options.sort}" if request_options
  headers['SEARCH'] = search

  headers['Content-Type'] = "application/json"
  query_params = []
  if request_options
    query_params << request_options.expand if request_options.expand
    query_params << request_options.page if request_options.page
    if request_options.totals
      headers['TOTALS'] = request_options.totals
    end
  end

  method = 'get'
  url = Payrix.configuration.url
  endpoint = "#{@resource_name}?#{query_params.join('&')}"
  data = {}

  body, status = Payrix::Http::Request.instance.send_http(method, url, endpoint, data, headers)
  @response = Payrix::Http::Response.new(body, status, self.class)

  success = validate_response
  request_options.go_next_page if success

  success
end

#set(params) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/payrix/resource/base.rb', line 24

def set(params)
  if params.is_a?(Hash) && !params.empty?
     params.each do |k, v|
       if @attrs.include? k
         public_send("#{k}=", v) if respond_to? "#{k}="
       else
         self.class.send(:attr_accessor, k)
         self.instance_variable_set("@#{k}", v);
       end
     end
   end
end

#statusObject



56
57
58
# File 'lib/payrix/resource/base.rb', line 56

def status
  @response.nil? ? false : @response.status
end

#to_json(nested = false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/payrix/resource/base.rb', line 37

def to_json(nested = false)
  excludes = ['request_options', 'resource_name', 'response', 'attrs']

  instance_variables.inject({}) do |hash, var|
    key = var.to_s.delete('@')
    val = instance_variable_get(var)

    if !excludes.include? key
      if val.is_a? Base
        hash[key] = val.to_json(true) if nested
      else
        hash[key] = val
      end
    end

    hash
  end
end

#totalsObject



72
73
74
# File 'lib/payrix/resource/base.rb', line 72

def totals
  @response.nil? ? [] : @response.totals
end

#update(params = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/payrix/resource/base.rb', line 129

def update(params = {})
  set(params)

  if !id 
    if Payrix.configuration.exception_enabled
      raise Payrix::Exceptions::InvalidRequest.new('ID is required for this action')
    else
      return false
    end
  end
  
  headers = build_headers
  headers['Content-Type'] = "application/json"
  method = 'put'
  url = Payrix.configuration.url
  endpoint = "#{@resource_name}/#{id}"
  data = to_json

  body, status = Payrix::Http::Request.instance.send_http(method, url, endpoint, data, headers)
  @response = Payrix::Http::Response.new(body, status, self.class)

  validate_response
end