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



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

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

.action_prefix=(action_prefix) ⇒ Object



84
85
86
# File 'lib/restful_resource/base.rb', line 84

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

.all(params = {}) ⇒ Object



74
75
76
# File 'lib/restful_resource/base.rb', line 74

def self.all(params = {})
  self.where(params)
end

.configure(base_url: nil, username: nil, password: nil, logger: nil, cache_store: nil, instrumentation: {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/restful_resource/base.rb', line 5

def self.configure(base_url: nil,
                   username: nil,
                   password: nil,
                   logger: nil,
                   cache_store: nil,
                   instrumentation: {})

  @base_url = URI.parse(base_url)

  @http = RestfulResource::HttpClient.new(username: username,
                                          password: password,
                                          logger: logger,
                                          cache_store: cache_store,
                                          instrumentation: instrumentation)
end

.delete(id, **params) ⇒ Object



47
48
49
50
51
# File 'lib/restful_resource/base.rb', line 47

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

.fetch_all!(conditions = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/restful_resource/base.rb', line 88

def self.fetch_all!(conditions={})
  Enumerator.new do |y|
    next_page = 1
    begin
      resources = self.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



25
26
27
28
29
30
# File 'lib/restful_resource/base.rb', line 25

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

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

.get(params = {}) ⇒ Object



40
41
42
43
44
45
# File 'lib/restful_resource/base.rb', line 40

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

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

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



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

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)

  self.new(parse_json(response.body))
end

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



53
54
55
56
57
58
59
60
61
# File 'lib/restful_resource/base.rb', line 53

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)
  self.new(parse_json(response.body))
end

.resource_path(url) ⇒ Object



21
22
23
# File 'lib/restful_resource/base.rb', line 21

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

.where(params = {}) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/restful_resource/base.rb', line 32

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

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