Class: RestfulResource::Base

Inherits:
OpenObject show all
Extended by:
Associations
Defined in:
lib/restful_resource/base.rb

Class Method Summary collapse

Methods included from Associations

has_many, has_one

Methods inherited from OpenObject

#==, #as_json, #eql?, #equal?, #hash, #initialize, #method_missing, #respond_to?

Constructor Details

This class inherits a constructor from RestfulResource::OpenObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RestfulResource::OpenObject

Class Method Details

.action(action_name) ⇒ Object



99
100
101
102
103
# File 'lib/restful_resource/base.rb', line 99

def self.action(action_name)
  clone = self.clone
  clone.action_prefix = action_name
  clone
end

.action_prefix=(action_prefix) ⇒ Object



105
106
107
# File 'lib/restful_resource/base.rb', line 105

def self.action_prefix=(action_prefix)
  @action_prefix = action_prefix.to_s
end

.all(**params) ⇒ Object



95
96
97
# File 'lib/restful_resource/base.rb', line 95

def self.all(**params)
  where(**params)
end

.base_urlObject



126
127
128
129
130
131
132
# File 'lib/restful_resource/base.rb', line 126

def self.base_url
  result = @base_url
  result = superclass.base_url if result.nil? && superclass.respond_to?(:base_url)
  raise 'Base url missing' if result.nil?

  result
end

.collection_url(**params) ⇒ Object



134
135
136
137
# File 'lib/restful_resource/base.rb', line 134

def self.collection_url(**params)
  url = merge_url_paths(base_url, @resource_path, @action_prefix)
  replace_parameters(url, **params)
end

.configure(base_url: nil, username: nil, password: nil, auth_token: nil, logger: nil, cache_store: nil, instrumentation: {}, timeout: nil, open_timeout: nil, faraday_config: nil, faraday_options: {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/restful_resource/base.rb', line 5

def self.configure(base_url: nil,
  username: nil,
  password: nil,
  auth_token: nil,
  logger: nil,
  cache_store: nil,
  instrumentation: {},
  timeout: nil,
  open_timeout: nil,
  faraday_config: nil,
  faraday_options: {})

  @base_url = URI.parse(base_url)

  @http = RestfulResource::HttpClient.new(username: username,
                                          password: password,
                                          auth_token: auth_token,
                                          logger: logger,
                                          cache_store: cache_store,
                                          timeout: timeout,
                                          open_timeout: open_timeout,
                                          instrumentation: instrumentation,
                                          faraday_config: faraday_config,
                                          faraday_options: faraday_options
                                         )
end

.delete(id, **params) ⇒ Object



58
59
60
61
62
# File 'lib/restful_resource/base.rb', line 58

def self.delete(id, **params)
  params_without_options, options = format_params(**params)
  response = http.delete(member_url(id, **params_without_options), **options)
  new(parse_json(response.body))
end

.fetch_all!(conditions = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/restful_resource/base.rb', line 109

def self.fetch_all!(conditions = {})
  Enumerator.new do |y|
    next_page = 1
    begin
      resources = where(conditions.merge(page: next_page))
      resources.each do |resource|
        y << resource
      end
      next_page = resources.next_page
    end while !next_page.nil?
  end
end

.find(id, **params) ⇒ Object



36
37
38
39
40
41
# File 'lib/restful_resource/base.rb', line 36

def self.find(id, **params)
  params_without_options, options = format_params(**params)

  response = http.get(member_url(id, **params_without_options), **options)
  new(parse_json(response.body))
end

.format_params(**params) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/restful_resource/base.rb', line 139

def self.format_params(**params)
  headers = params.delete(:headers) || {}

  headers[:cache_control] = 'no-cache' if params.delete(:no_cache)
  open_timeout = params.delete(:open_timeout)
  timeout = params.delete(:timeout)

  [params, { headers: headers, open_timeout: open_timeout, timeout: timeout }]
end

.get(**params) ⇒ Object



51
52
53
54
55
56
# File 'lib/restful_resource/base.rb', line 51

def self.get(**params)
  params_without_options, options = format_params(**params)

  response = http.get(collection_url(**params_without_options), **options)
  new(parse_json(response.body))
end

.httpObject



122
123
124
# File 'lib/restful_resource/base.rb', line 122

def self.http
  @http || superclass.http
end

.member_url(id, **params) ⇒ Object



153
154
155
156
157
158
# File 'lib/restful_resource/base.rb', line 153

def self.member_url(id, **params)
  raise ResourceIdMissingError if id.blank?

  url = merge_url_paths(base_url, @resource_path, CGI.escape(id.to_s), @action_prefix)
  replace_parameters(url, **params)
end

.merge_url_paths(uri, *paths) ⇒ Object



149
150
151
# File 'lib/restful_resource/base.rb', line 149

def self.merge_url_paths(uri, *paths)
  uri.merge(paths.compact.join('/')).to_s
end

.new_collection(json) ⇒ Object



160
161
162
163
164
# File 'lib/restful_resource/base.rb', line 160

def self.new_collection(json)
  json.map do |element|
    new(element)
  end
end

.paginate_response(response) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/restful_resource/base.rb', line 192

def self.paginate_response(response)
  links_header =  response.headers[:links]
  links = LinkHeader.parse(links_header)

  prev_url = links.find_link(%w[rel prev]).try(:href)
  next_url = links.find_link(%w[rel next]).try(:href)

  array = parse_json(response.body).map { |attributes| new(attributes) }
  PaginatedArray.new(array, previous_page_url: prev_url, next_page_url: next_url, total_count: response.headers[:x_total_count])
end

.parse_json(json) ⇒ Object



166
167
168
169
170
# File 'lib/restful_resource/base.rb', line 166

def self.parse_json(json)
  return nil if json.strip.empty?

  ActiveSupport::JSON.decode(json)
end

.patch(id, data: {}, headers: {}, **params) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/restful_resource/base.rb', line 64

def self.patch(id, data: {}, headers: {}, **params)
  params_without_options, options = format_params(**params)
  options.delete(:headers)

  url = member_url(id, **params_without_options)

  response = http.patch(url, data: data, headers: headers, **options)
  new(parse_json(response.body))
end

.post(data: {}, headers: {}, **params) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/restful_resource/base.rb', line 84

def self.post(data: {}, headers: {}, **params)
  params_without_options, options = format_params(**params)
  options.delete(:headers)

  url = collection_url(**params_without_options)

  response = http.post(url, data: data, headers: headers, **options)

  new(parse_json(response.body))
end

.put(id, data: {}, headers: {}, **params) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/restful_resource/base.rb', line 74

def self.put(id, data: {}, headers: {}, **params)
  params_without_options, options = format_params(**params)
  options.delete(:headers)

  url = member_url(id, **params_without_options)

  response = http.put(url, data: data, headers: headers, **options)
  new(parse_json(response.body))
end

.replace_parameters(url, **params) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/restful_resource/base.rb', line 172

def self.replace_parameters(url, **params)
  missing_params = []
  params = params.with_indifferent_access

  url_params = url.scan(%r{:([A-Za-z][^/]*)}).flatten
  url_params.each do |key|
    value = params.delete(key)
    if value.nil?
      missing_params << key
    else
      url = url.gsub(':' + key, CGI.escape(value.to_s))
    end
  end

  raise ParameterMissingError, missing_params if missing_params.any?

  url += "?#{params.to_query}" unless params.empty?
  url
end

.resource_path(url) ⇒ Object



32
33
34
# File 'lib/restful_resource/base.rb', line 32

def self.resource_path(url)
  @resource_path = url
end

.where(**params) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/restful_resource/base.rb', line 43

def self.where(**params)
  params_without_options, options = format_params(**params)

  url = collection_url(**params_without_options)
  response = http.get(url, **options)
  paginate_response(response)
end