Module: Shutl::Resource::RestClassMethods

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

Defined Under Namespace

Classes: RestCollection

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_user=(email) ⇒ Object



239
240
241
# File 'lib/shutl/resource/rest_class_methods.rb', line 239

def self.from_user= email
  Thread.current[:user_email] = email
end

Instance Method Details

#add_resource_id_to(args = {}) ⇒ Object



210
211
212
213
214
215
216
# File 'lib/shutl/resource/rest_class_methods.rb', line 210

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/shutl/resource/rest_class_methods.rb', line 113

def all(args = {})
  header_args, non_header_args = split_header_args args
  url_args, params = split_url_args non_header_args

  url = generate_collection_url url_args, params

  response = connection.get(url) do |req|
    req.headers = generate_request_header(header_options(header_args))
  end

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

  response_object = response.body[@resource_name.pluralize].map do |h|
    new_object(non_header_args.merge(h), response.body)
  end
  if order_collection?
    response_object.sort! do |a,b|
      str_a = a.send(@order_collection_by).to_s
      str_b = b.send(@order_collection_by).to_s
      str_a.casecmp(str_b)
    end
  end

  RestCollection.new(response_object, response.body['pagination'])
end

#base_uri(uri) ⇒ Object



5
6
7
# File 'lib/shutl/resource/rest_class_methods.rb', line 5

def base_uri(uri)
  @base_uri = uri
end

#collection_url(url) ⇒ Object



190
191
192
# File 'lib/shutl/resource/rest_class_methods.rb', line 190

def collection_url(url)
  @remote_collection_url = url
end

#connectionObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/shutl/resource/rest_class_methods.rb', line 13

def connection
  @connection ||= Faraday.new(url: target_url, proxy: proxy_url) do |faraday|
    faraday.request :url_encoded # form-encode POST params


    if Shutl::Resource.logger
      faraday.use :default_logger, logger: Shutl::Resource.logger
    end

    faraday.response :json
    faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
  end
end

#convert_new_id(attributes) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/shutl/resource/rest_class_methods.rb', line 202

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



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

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

  response = connection.post(url) do |req|
    req.headers = generate_request_header(header_options(options))
    req.body = { @resource_name => attributes }.to_json
  end

  check_fail response, "Create failed"

  body     = response.body || {}
  attributes = body[@resource_name] || {}

  new_object attributes, body
end

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



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/shutl/resource/rest_class_methods.rb', line 66

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

  perform_action(
    instance,
    :delete,
    {}.to_json,
    generate_request_header(header_options(options)),
    failure_message
  ).success?
end

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/shutl/resource/rest_class_methods.rb', line 27

def find(args = {}, params = {})
  if @singular_resource
    params = args
    url    = singular_member_url params
  elsif !args.kind_of?(Hash)
    id   = args
    args = { resource_id_name => id }
    url  = member_url args.dup, params
  else
    url = member_url args.dup, params
  end
  response     = connection.get(url) do |req|
    req.headers = generate_request_header(header_options(params))
  end

  check_fail response, "Failed to find #{name}! args: #{args}, params: #{params}"

  parent_attributes = response.body[@resource_name] || {}
  including_parent_attributes = parent_attributes.merge args
  new_object including_parent_attributes, response.body
end

#generate_collection_url(*args) ⇒ Object



234
235
236
# File 'lib/shutl/resource/rest_class_methods.rb', line 234

def generate_collection_url *args
  generate_url! remote_collection_url, *args
end

#member_url(*args) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/shutl/resource/rest_class_methods.rb', line 222

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



198
199
200
# File 'lib/shutl/resource/rest_class_methods.rb', line 198

def order_collection_by(field)
  @order_collection_by = field
end

#remote_collection_urlObject



182
183
184
# File 'lib/shutl/resource/rest_class_methods.rb', line 182

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

#remote_resource_urlObject



186
187
188
# File 'lib/shutl/resource/rest_class_methods.rb', line 186

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

#resource_id(variable_name) ⇒ Object



174
175
176
# File 'lib/shutl/resource/rest_class_methods.rb', line 174

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

#resource_id_nameObject



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

def resource_id_name
  instance_variable_get(:@resource_id).to_sym
end

#resource_name(name) ⇒ Object



170
171
172
# File 'lib/shutl/resource/rest_class_methods.rb', line 170

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

#resource_url(url) ⇒ Object



194
195
196
# File 'lib/shutl/resource/rest_class_methods.rb', line 194

def resource_url(url)
  @remote_resource_url = url
end

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



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/shutl/resource/rest_class_methods.rb', line 78

def save instance, options = {}
  attributes = instance.attributes rescue instance

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

  response = perform_action(instance,
                            options.fetch("method", :put),
                            body,
                            generate_request_header(header_options(options)),
                            "Save failed")

  response.success?
end

#set_proxy(uri) ⇒ Object



9
10
11
# File 'lib/shutl/resource/rest_class_methods.rb', line 9

def set_proxy(uri)
  @proxy_uri = uri
end

#singular_member_url(params) ⇒ Object



218
219
220
# File 'lib/shutl/resource/rest_class_methods.rb', line 218

def singular_member_url params
  generate_url! "/#{@resource_name}", {}, params
end

#singular_resourceObject



166
167
168
# File 'lib/shutl/resource/rest_class_methods.rb', line 166

def singular_resource
  @singular_resource = true
end

#split_hash(h) ⇒ Object



96
97
98
99
# File 'lib/shutl/resource/rest_class_methods.rb', line 96

def split_hash h
  partitions = h.partition { |key, value| yield key, value }
  [Hash[partitions.first].with_indifferent_access, Hash[partitions.last].with_indifferent_access]
end

#split_header_args(args) ⇒ Object



107
108
109
110
111
# File 'lib/shutl/resource/rest_class_methods.rb', line 107

def split_header_args args
  split_hash(args) do |key, value|
    %(auth headers from).include?(key.to_s)
  end
end

#split_url_args(args) ⇒ Object



101
102
103
104
105
# File 'lib/shutl/resource/rest_class_methods.rb', line 101

def split_url_args args
  split_hash(args) do |key, value|
    !remote_collection_url.index(":#{key}").nil?
  end
end

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



92
93
94
# File 'lib/shutl/resource/rest_class_methods.rb', line 92

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