Class: Arrest::HttpSource
- Inherits:
-
Object
- Object
- Arrest::HttpSource
- Defined in:
- lib/arrest/http_source.rb
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
Instance Method Summary collapse
- #add_headers(context, headers) ⇒ Object
- #connection ⇒ Object
- #delete(context, rest_resource) ⇒ Object
- #delete_all(context, resource_path) ⇒ Object
- #get_many(context, sub, filter = {}) ⇒ Object
- #get_many_other_ids(context, path) ⇒ Object
- #get_one(context, sub, filter = {}) ⇒ Object
- #hash_to_query(hash) ⇒ Object
-
#initialize(base) ⇒ HttpSource
constructor
A new instance of HttpSource.
- #insert_nulls!(rest_resource, hash) ⇒ Object
- #internal_put(rest_resource, location, body) ⇒ Object
- #post(context, rest_resource) ⇒ Object
- #put(context, rest_resource) ⇒ Object
- #put_sub_resource(rest_resource, sub_url, ids) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(base) ⇒ HttpSource
Returns a new instance of HttpSource.
10 11 12 |
# File 'lib/arrest/http_source.rb', line 10 def initialize base @base = base end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
8 9 10 |
# File 'lib/arrest/http_source.rb', line 8 def base @base end |
Instance Method Details
#add_headers(context, headers) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/arrest/http_source.rb', line 18 def add_headers(context,headers) decorator = context.header_decorator if context decorator ||= Arrest::Source.header_decorator hds = decorator.headers hds.each_pair do |k,v| headers[k.to_s] = v.to_s end hds end |
#connection ⇒ Object
166 167 168 169 170 171 172 173 |
# File 'lib/arrest/http_source.rb', line 166 def connection conn = Faraday.new(:url => @base) do |builder| builder.request :url_encoded builder.request :json builder.adapter :net_http builder.use Faraday::Response::Logger, Arrest::logger end end |
#delete(context, rest_resource) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/arrest/http_source.rb', line 73 def delete(context, rest_resource) raise "To delete an object it must have an id" unless rest_resource.respond_to?(:id) && rest_resource.id != nil headers = nil response = self.connection().delete do |req| req.url(rest_resource.resource_location) headers = add_headers(context, req.headers) end rql = RequestLog.new(:delete, rest_resource.resource_location, nil, headers) rsl = ResponseLog.new(response.env[:status], response.body) Arrest::Source.call_logger.log(rql, rsl) if response.env[:status] != 200 handle_errors(rest_resource, response.body, response.env[:status]) end response.env[:status] == 200 end |
#delete_all(context, resource_path) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/arrest/http_source.rb', line 60 def delete_all(context, resource_path) headers = nil response = self.connection().delete do |req| req.url(resource_path) headers = add_headers(context, req.headers) end rql = RequestLog.new(:delete, "#{resource_path}", nil, headers) rsl = ResponseLog.new(response.env[:status], response.body) Arrest::Source.call_logger.log(rql, rsl) response.env[:status] == 200 end |
#get_many(context, sub, filter = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/arrest/http_source.rb', line 47 def get_many(context, sub, filter={}) headers = nil response = self.connection().get do |req| req.url(sub, filter) headers = add_headers(context, req.headers) end rql = RequestLog.new(:get, "#{sub}#{hash_to_query filter}", nil, headers) rsl = ResponseLog.new(response.env[:status], response.body) Arrest::Source.call_logger.log(rql, rsl) response.body end |
#get_many_other_ids(context, path) ⇒ Object
43 44 45 |
# File 'lib/arrest/http_source.rb', line 43 def get_many_other_ids(context, path) get_one(context, path) end |
#get_one(context, sub, filter = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/arrest/http_source.rb', line 28 def get_one(context, sub, filter={}) headers = nil response = self.connection().get do |req| req.url(sub, filter) headers = add_headers(context, req.headers) end rql = RequestLog.new(:get, "#{sub}#{hash_to_query filter}", nil, headers) rsl = ResponseLog.new(response.env[:status], response.body) Arrest::Source.call_logger.log(rql, rsl) if response.env[:status] != 200 raise Errors::DocumentNotFoundError end response.body end |
#hash_to_query(hash) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/arrest/http_source.rb', line 175 def hash_to_query hash return "" if hash.empty? r = "" c = '?' hash.each_pair do |k,v| r << c r << k.to_s r << '=' r << v.to_s c = '&' end r end |
#insert_nulls!(rest_resource, hash) ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/arrest/http_source.rb', line 126 def insert_nulls!(rest_resource, hash) rest_resource.class.all_fields.each do |field| changed = (!rest_resource.respond_to?("#{field.name}_changed?" || rest_resource.send("#{field}_changed?"))) if !field.read_only && hash[field.json_name] == nil && changed hash[field.json_name] = Null.singleton end end end |
#internal_put(rest_resource, location, body) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/arrest/http_source.rb', line 110 def internal_put(rest_resource, location, body) headers = nil response = self.connection().put do |req| req.url(location) headers = add_headers(rest_resource.context, req.headers) req.body = body end rql = RequestLog.new(:put, location, body, headers) rsl = ResponseLog.new(response.env[:status], response.body) Arrest::Source.call_logger.log(rql, rsl) if response.env[:status] != 200 handle_errors(rest_resource, response.body, response.env[:status]) end response.env[:status] == 200 end |
#post(context, rest_resource) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/arrest/http_source.rb', line 135 def post(context, rest_resource) raise "new object must have setter for id" unless rest_resource.respond_to?(:id=) raise "new object must not have id" if rest_resource.respond_to?(:id) && rest_resource.id != nil hash = rest_resource.to_jhash hash.delete(:id) hash.delete('id') insert_nulls!(rest_resource,hash) body = JSON.generate(hash) headers = nil response = self.connection().post do |req| req.url rest_resource.resource_path headers = add_headers(context, req.headers) req.body = body end rql = RequestLog.new(:post, rest_resource.resource_path, body, headers) rsl = ResponseLog.new(response.env[:status], response.body) Arrest::Source.call_logger.log(rql, rsl) if (response.env[:status] == 201) location = response.env[:response_headers][:location] id = location.gsub(/^.*\//, '') rest_resource.id= id true else handle_errors(rest_resource, response.body, response.env[:status]) false end end |
#put(context, rest_resource) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/arrest/http_source.rb', line 96 def put(context, rest_resource) raise "To change an object it must have an id" unless rest_resource.respond_to?(:id) && rest_resource.id != nil hash = rest_resource.to_jhash insert_nulls!(rest_resource,hash) hash.delete(:id) hash.delete("id") body = JSON.generate(hash) internal_put(rest_resource, rest_resource.resource_location, body) end |
#put_sub_resource(rest_resource, sub_url, ids) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/arrest/http_source.rb', line 89 def put_sub_resource(rest_resource, sub_url, ids) location = "#{rest_resource.resource_location}/#{sub_url}" body = ids.to_json internal_put(rest_resource, location, body) end |
#url ⇒ Object
14 15 16 |
# File 'lib/arrest/http_source.rb', line 14 def url @base end |