Module: Shutl::Resource::RestClassMethods

Defined in:
lib/shutl/resource/rest_class_methods.rb

Defined Under Namespace

Classes: RestCollection

Instance Method Summary collapse

Instance Method Details

#add_resource_id_to(args = {}) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/shutl/resource/rest_class_methods.rb', line 159

def add_resource_id_to args={}
  args = args.dup.with_indifferent_access
  unless args.has_key? "id"
    args.merge!({ "id" => args[resource_id_name] })
  end
  args
end

#all(args = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/shutl/resource/rest_class_methods.rb', line 69

def all(args = {})
  auth_options = { auth: args.delete(:auth), from: args.delete(:from) }
  partition    = args.partition { |key, value| !remote_collection_url.index(":#{key}").nil? }

  url_args = partition.first.inject({}) { |h, pair| h[pair.first] = pair.last; h }
  params   = partition.last.inject({}) { |h, pair| h[pair.first] = pair.last; h }

  url      = generate_collection_url url_args, params
  response = get url, headers_with_auth(auth_options)

  check_fail response, "Failed to find all #{name.downcase.pluralize}"

  response_object = response.parsed_response[@resource_name.pluralize].map do |h|
    new_object(args.merge(h), response)
  end
  if order_collection?
    response_object.sort! do |a,b|
      a.send(@order_collection_by) <=> b.send(@order_collection_by)
    end
  end
  RestCollection.new(response_object, response.parsed_response['pagination'])
end

#collection_url(url) ⇒ Object



139
140
141
# File 'lib/shutl/resource/rest_class_methods.rb', line 139

def collection_url(url)
  @remote_collection_url = url
end

#convert_new_id(attributes) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/shutl/resource/rest_class_methods.rb', line 151

def convert_new_id attributes
  if attributes[:new_id]
    attributes = attributes.clone.tap { |h| h[:id] = h[:new_id]; h.delete(:new_id) }
  end

  attributes
end

#create(attributes = {}, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/shutl/resource/rest_class_methods.rb', line 22

def create attributes = {}, options = {}
  url = generate_collection_url attributes
  attributes.delete "response"

  response = post(
    url,
    { body: { @resource_name => attributes }.to_json }.merge(headers_with_auth(options))
  )

  check_fail response, "Create failed"

  parsed     = response.parsed_response || {}
  attributes = parsed[@resource_name] || {}

  new_object attributes, response
end

#destroy(instance, options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/shutl/resource/rest_class_methods.rb', line 39

def destroy instance, options = {}
  message = "Failed to destroy #{name.downcase.pluralize}"

  perform_action(
    instance,
    :delete,
    headers_with_auth(options),
    message
  ).success?
end

#find(args, params = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/shutl/resource/rest_class_methods.rb', line 3

def find(args, params = {})
  unless args.kind_of?(Hash)
    id   = args
    args = { resource_id_name => id }
  end
  auth_options = { auth: params.delete(:auth), from: params.delete(:from) }
  url          = member_url args.dup, params
  response     = get url, headers_with_auth(auth_options)


  check_fail response, "Failed to find #{name} with the id #{id}"

  parsed = response.parsed_response

  including_parent_attributes = parsed[@resource_name].merge args

  new_object including_parent_attributes, response
end

#generate_collection_url(*args) ⇒ Object



178
179
180
# File 'lib/shutl/resource/rest_class_methods.rb', line 178

def generate_collection_url *args
  generate_url! remote_collection_url, *args
end

#member_url(*args) ⇒ Object



167
168
169
170
171
172
173
174
175
176
# File 'lib/shutl/resource/rest_class_methods.rb', line 167

def member_url *args
  attributes = args.first.with_indifferent_access
  unless attributes[resource_id_name] ||= attributes[:id]
    raise ArgumentError, "Missing resource id with name: `#{resource_id_name}' for #{self}"
  end

  args[0] = attributes

  generate_url! remote_resource_url, *(args.dup)
end

#order_collection_by(field) ⇒ Object



147
148
149
# File 'lib/shutl/resource/rest_class_methods.rb', line 147

def order_collection_by(field)
  @order_collection_by = field
end

#remote_collection_urlObject



131
132
133
# File 'lib/shutl/resource/rest_class_methods.rb', line 131

def remote_collection_url
  @remote_collection_url ||= "/#{@resource_name.pluralize}"
end

#remote_resource_urlObject



135
136
137
# File 'lib/shutl/resource/rest_class_methods.rb', line 135

def remote_resource_url
  @remote_resource_url ||= "#{remote_collection_url}/:#{resource_id_name}"
end

#resource_id(variable_name) ⇒ Object



123
124
125
# File 'lib/shutl/resource/rest_class_methods.rb', line 123

def resource_id(variable_name)
  instance_variable_set :@resource_id, variable_name
end

#resource_id_nameObject



127
128
129
# File 'lib/shutl/resource/rest_class_methods.rb', line 127

def resource_id_name
  instance_variable_get(:@resource_id).to_sym
end

#resource_name(name) ⇒ Object



119
120
121
# File 'lib/shutl/resource/rest_class_methods.rb', line 119

def resource_name(name)
  instance_variable_set :@resource_name, name
end

#resource_url(url) ⇒ Object



143
144
145
# File 'lib/shutl/resource/rest_class_methods.rb', line 143

def resource_url(url)
  @remote_resource_url = url
end

#save(instance, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/shutl/resource/rest_class_methods.rb', line 50

def save instance, options = {}
  #TODO: this is sometimes a hash and sometimes a Rest - need to rethink this
  attributes = instance.attributes rescue instance

  payload = {
    body: { @resource_name => convert_new_id(attributes) }.to_json
  }

  payload_with_headers = payload.merge(headers_with_auth options)
  response             = perform_action(instance, :put, payload, "Save failed")

  response.success?
end

#update(args, options = {}) ⇒ Object



64
65
66
# File 'lib/shutl/resource/rest_class_methods.rb', line 64

def update args, options = {}
  save args, options
end