Class: Openpix::RubySdk::Resources::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/openpix/ruby_sdk/resources/resource.rb

Overview

Base class for resources from the API

Direct Known Subclasses

Charge, Customer, Payment, Refund, Subscription, Webhook

Instance Method Summary collapse

Constructor Details

#initialize(http_client) ⇒ Resource

Returns a new instance of Resource.



41
42
43
# File 'lib/openpix/ruby_sdk/resources/resource.rb', line 41

def initialize(http_client)
  @http_client = http_client
end

Instance Method Details

#create_attributesObject



65
66
67
# File 'lib/openpix/ruby_sdk/resources/resource.rb', line 65

def create_attributes
  raise NotImplementedError.new(method: __method__)
end

#destroy(id:, extra_headers: {}) ⇒ Object



189
190
191
192
193
194
195
196
197
# File 'lib/openpix/ruby_sdk/resources/resource.rb', line 189

def destroy(id:, extra_headers: {})
  response = delete_request(url: encoded_url(id), extra_headers: extra_headers)

  Openpix::RubySdk::ApiResponse.new(
    status: response.status,
    body: response.body,
    single_resource: to_single_resource
  )
end

#destroy!(id:, extra_headers: {}) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/openpix/ruby_sdk/resources/resource.rb', line 199

def destroy!(id:, extra_headers: {})
  response = delete_request(url: encoded_url(id), extra_headers: extra_headers)
  api_response = Openpix::RubySdk::ApiResponse.new(
    status: response.status,
    body: response.body,
    single_resource: to_single_resource
  )

  if response.status != 200
    raise(
      RequestError,
      "Error while deleting #{to_url} of id = #{id}, API response: #{api_response.error_response}, status: #{api_response.status}"
    )
  end

  api_response
end

#fetch(skip: nil, limit: nil, extra_headers: {}, params: {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/openpix/ruby_sdk/resources/resource.rb', line 111

def fetch(skip: nil, limit: nil, extra_headers: {}, params: {})
  set_pagination(skip, limit)

  response = get_request(extra_headers: extra_headers, params: @pagination_params.merge(params))
  api_response = Openpix::RubySdk::ApiResponse.new(
    status: response.status,
    body: response.body,
    collection_resource: to_collection_resource
  )

  @fetched = api_response.status == 200

  set_pagination_meta(api_response.pagination_meta) if @fetched
  @last_fetched_params = params if @fetched && !params.empty?

  api_response
end

#fetch!(skip: nil, limit: nil, extra_headers: {}, 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/openpix/ruby_sdk/resources/resource.rb', line 129

def fetch!(skip: nil, limit: nil, extra_headers: {}, params: {})
  set_pagination(skip, limit)

  response = get_request(extra_headers: extra_headers, params: @pagination_params.merge(params))
  api_response = Openpix::RubySdk::ApiResponse.new(
    status: response.status,
    body: response.body,
    collection_resource: to_collection_resource
  )

  if response.status != 200
    raise(
      RequestError,
      "Error while fetching, API response: #{api_response.error_response}, status: #{api_response.status}"
    )
  end

  @fetched = true
  @last_fetched_params = params unless params.empty?
  set_pagination_meta(api_response.pagination_meta)

  api_response
end

#fetch_next_page!(extra_headers: {}) ⇒ Object



153
154
155
# File 'lib/openpix/ruby_sdk/resources/resource.rb', line 153

def fetch_next_page!(extra_headers: {})
  fetch_page!(:next, extra_headers)
end

#fetch_previous_page!(extra_headers: {}) ⇒ Object



157
158
159
# File 'lib/openpix/ruby_sdk/resources/resource.rb', line 157

def fetch_previous_page!(extra_headers: {})
  fetch_page!(:previous, extra_headers)
end

#find(id:, extra_headers: {}) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/openpix/ruby_sdk/resources/resource.rb', line 161

def find(id:, extra_headers: {})
  response = get_request(url: encoded_url(id), extra_headers: extra_headers)

  Openpix::RubySdk::ApiResponse.new(
    status: response.status,
    body: response.body,
    single_resource: to_single_resource
  )
end

#find!(id:, extra_headers: {}) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/openpix/ruby_sdk/resources/resource.rb', line 171

def find!(id:, extra_headers: {})
  response = get_request(url: encoded_url(id), extra_headers: extra_headers)
  api_response = Openpix::RubySdk::ApiResponse.new(
    status: response.status,
    body: response.body,
    single_resource: to_single_resource
  )

  if response.status != 200
    raise(
      RequestError,
      "Error while getting #{to_single_resource} of id = #{id}, API response: #{api_response.error_response}, status: #{api_response.status}"
    )
  end

  api_response
end

#init_body(base_attrs: [], params: {}, rest: {}) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/openpix/ruby_sdk/resources/resource.rb', line 45

def init_body(base_attrs: [], params: {}, rest: {})
  params = params.with_indifferent_access
  base_attrs.each { |attr| instance_variable_set("@#{attr}", params[attr]) }
  @rest = rest

  self
end

#save(extra_headers: {}, return_existing: false) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/openpix/ruby_sdk/resources/resource.rb', line 83

def save(extra_headers: {}, return_existing: false)
  response = post_request(extra_headers, return_existing)

  Openpix::RubySdk::ApiResponse.new(
    status: response.status,
    body: response.body,
    single_resource: to_single_resource
  )
end

#save!(extra_headers: {}, return_existing: false) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/openpix/ruby_sdk/resources/resource.rb', line 93

def save!(extra_headers: {}, return_existing: false)
  response = post_request(extra_headers, return_existing)
  api_response = Openpix::RubySdk::ApiResponse.new(
    status: response.status,
    body: response.body,
    single_resource: to_single_resource
  )

  if response.status != 200
    raise(
      RequestError,
      "Error while saving, API response: #{api_response.error_response}, status: #{api_response.status}"
    )
  end

  api_response
end

#to_bodyObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/openpix/ruby_sdk/resources/resource.rb', line 69

def to_body
  body = {}

  create_attributes.each do |attr|
    body[attr] = send(attr)
  end

  compacted_body = Openpix::RubySdk::ApiBodyFormatter.remove_empty_values(body)

  return compacted_body unless @rest

  compacted_body.merge(@rest)
end

#to_collection_resourceObject



61
62
63
# File 'lib/openpix/ruby_sdk/resources/resource.rb', line 61

def to_collection_resource
  to_url.pluralize
end

#to_single_resourceObject



57
58
59
# File 'lib/openpix/ruby_sdk/resources/resource.rb', line 57

def to_single_resource
  to_url
end

#to_urlObject



53
54
55
# File 'lib/openpix/ruby_sdk/resources/resource.rb', line 53

def to_url
  raise NotImplementedError.new(method: __method__)
end