Class: Arrest::HttpSource
- Inherits:
-
Object
- Object
- Arrest::HttpSource
- Defined in:
- lib/arrest/transport/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(context, sub, filter = {}) ⇒ Object
- #hash_to_query(hash) ⇒ Object
-
#initialize(base) ⇒ HttpSource
constructor
A new instance of HttpSource.
- #post(context, rest_resource) ⇒ Object
- #put(context, rest_resource) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(base) ⇒ HttpSource
Returns a new instance of HttpSource.
10 11 12 |
# File 'lib/arrest/transport/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/transport/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/transport/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
151 152 153 154 155 156 157 |
# File 'lib/arrest/transport/http_source.rb', line 151 def connection conn = Faraday.new(:url => @base) do |builder| builder.request :url_encoded builder.adapter :net_http builder.use Faraday::Response::Logger, Arrest::logger end end |
#delete(context, rest_resource) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/arrest/transport/http_source.rb', line 66 def delete(context, rest_resource) profiler_status_str = "" ::ActiveSupport::Notifications.instrument("http.sgdb", :method => :delete, :url => rest_resource.resource_location, :status => profiler_status_str) do 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 profiler_status_str << response.env[:status].to_s response.env[:status] == 200 end end |
#delete_all(context, resource_path) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/arrest/transport/http_source.rb', line 53 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(context, sub, filter = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/arrest/transport/http_source.rb', line 28 def get(context, sub, filter={}) sub = fix_url_encode(sub) profiler_status_str = "" ::ActiveSupport::Notifications.instrument("http.sgdb", :method => :get, :url => sub, :status => profiler_status_str) do 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] == 401 raise Errors::PermissionDeniedError.new(response.body) elsif response.env[:status] == 404 raise Errors::DocumentNotFoundError elsif response.env[:status] != 200 raise Errors::UnknownError.new(response.body) end profiler_status_str << response.env[:status].to_s response.body end end |
#hash_to_query(hash) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/arrest/transport/http_source.rb', line 159 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 |
#post(context, rest_resource) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/arrest/transport/http_source.rb', line 117 def post(context, rest_resource) profiler_status_str = "" ::ActiveSupport::Notifications.instrument("http.sgdb", :method => :post, :url => rest_resource.resource_path, :status => profiler_status_str) do 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(:create) hash.delete(:id) hash.delete('id') 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 end |
#put(context, rest_resource) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/arrest/transport/http_source.rb', line 87 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(:update) hash.delete(:id) hash.delete("id") body = JSON.generate(hash) profiler_status_str = "" ::ActiveSupport::Notifications.instrument("http.sgdb", :method => :delete, :url => rest_resource.resource_location, :status => profiler_status_str) do headers = nil location = rest_resource.resource_location 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 profiler_status_str << response.env[:status].to_s response.env[:status] == 200 end end |
#url ⇒ Object
14 15 16 |
# File 'lib/arrest/transport/http_source.rb', line 14 def url @base end |