Module: Shutl::Resource::RestClassMethods
- Defined in:
- lib/shutl/resource/rest_class_methods.rb
Defined Under Namespace
Classes: RestCollection
Instance Method Summary collapse
- #add_resource_id_to(args = {}) ⇒ Object
- #all(args = {}) ⇒ Object
- #base_uri(uri) ⇒ Object
- #collection_url(url) ⇒ Object
- #connection ⇒ Object
- #convert_new_id(attributes) ⇒ Object
- #create(attributes = {}, options = {}) ⇒ Object
- #destroy(instance, options = {}) ⇒ Object
- #find(args = {}, params = {}) ⇒ Object
- #generate_collection_url(*args) ⇒ Object
- #headers ⇒ Object
- #member_url(*args) ⇒ Object
- #order_collection_by(field) ⇒ Object
- #remote_collection_url ⇒ Object
- #remote_resource_url ⇒ Object
- #resource_id(variable_name) ⇒ Object
- #resource_id_name ⇒ Object
- #resource_name(name) ⇒ Object
- #resource_url(url) ⇒ Object
- #save(instance, options = {}) ⇒ Object
- #singular_member_url(params = {}) ⇒ Object
- #singular_resource ⇒ Object
- #update(args, options = {}) ⇒ Object
Instance Method Details
#add_resource_id_to(args = {}) ⇒ Object
201 202 203 204 205 206 207 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 201 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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 102 def all(args = {}) = { 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 = connection.get(url) do |req| req.headers = headers_with_auth() end check_fail response, "Failed to find all #{name.downcase.pluralize}" response_object = response.body[@resource_name.pluralize].map do |h| new_object(args.merge(h), response) 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
3 4 5 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 3 def base_uri uri @base_uri = uri end |
#collection_url(url) ⇒ Object
181 182 183 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 181 def collection_url(url) @remote_collection_url = url end |
#connection ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 7 def connection @connection ||= Faraday.new(:url => @base_uri || Shutl::Resource.base_uri) do |faraday| faraday.request :url_encoded # form-encode POST params faraday.response :json # faraday.ssl[:ca_file] = ENV["SSL_CERT_FILE"] faraday.adapter Faraday.default_adapter # make requests with Net::HTTP if Shutl::Resource.logger faraday.use :extended_logging, logger: Shutl::Resource.logger end end end |
#convert_new_id(attributes) ⇒ Object
193 194 195 196 197 198 199 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 193 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
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 54 def create attributes = {}, = {} url = generate_collection_url attributes attributes.delete "response" response = connection.post(url) do |req| req.headers = headers_with_auth() req.body = { @resource_name => attributes }.to_json end check_fail response, "Create failed" parsed = response.body || {} attributes = parsed[@resource_name] || {} new_object attributes, response end |
#destroy(instance, options = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 71 def destroy instance, = {} = "Failed to destroy #{name.downcase.pluralize}" perform_action( instance, :delete, {}, headers_with_auth(), ).success? end |
#find(args = {}, params = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 30 def find(args = {}, params = {}) params = args if @singular_resource = { auth: params.delete(:auth), from: params.delete(:from) } if @singular_resource 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 = headers_with_auth() end check_fail response, "Failed to find #{name}! args: #{args}, params: #{params}" including_parent_attributes = response.body[@resource_name].merge args new_object including_parent_attributes, response end |
#generate_collection_url(*args) ⇒ Object
225 226 227 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 225 def generate_collection_url *args generate_url! remote_collection_url, *args end |
#headers ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 22 def headers { 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'User-Agent' => "Shutl Resource Gem v#{Shutl::Resource::VERSION}" } end |
#member_url(*args) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 213 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
189 190 191 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 189 def order_collection_by(field) @order_collection_by = field end |
#remote_collection_url ⇒ Object
173 174 175 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 173 def remote_collection_url @remote_collection_url ||= "/#{@resource_name.pluralize}" end |
#remote_resource_url ⇒ Object
177 178 179 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 177 def remote_resource_url @remote_resource_url ||= "#{remote_collection_url}/:#{resource_id_name}" end |
#resource_id(variable_name) ⇒ Object
165 166 167 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 165 def resource_id(variable_name) instance_variable_set :@resource_id, variable_name end |
#resource_id_name ⇒ Object
169 170 171 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 169 def resource_id_name instance_variable_get(:@resource_id).to_sym end |
#resource_name(name) ⇒ Object
161 162 163 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 161 def resource_name(name) instance_variable_set :@resource_name, name end |
#resource_url(url) ⇒ Object
185 186 187 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 185 def resource_url(url) @remote_resource_url = url end |
#save(instance, options = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 83 def save instance, = {} attributes = instance.attributes rescue instance body = { @resource_name => convert_new_id(attributes) }.to_json response = perform_action(instance, :put, body, headers_with_auth(), "Save failed") response.success? end |
#singular_member_url(params = {}) ⇒ Object
209 210 211 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 209 def singular_member_url params={} generate_url! "/#{@resource_name}", params end |
#singular_resource ⇒ Object
157 158 159 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 157 def singular_resource @singular_resource = true end |
#update(args, options = {}) ⇒ Object
97 98 99 |
# File 'lib/shutl/resource/rest_class_methods.rb', line 97 def update args, = {} save args, end |