Module: EDH::HTTPService
- Defined in:
- lib/edh/http_service.rb,
lib/edh/http_service/response.rb,
lib/edh/http_service/multipart_request.rb
Defined Under Namespace
Classes: MultipartRequest, Response
Constant Summary collapse
- DEFAULT_MIDDLEWARE =
EDH’s default middleware stack. We encode requests in a Passport-compatible multipart request, and use whichever adapter has been configured for this application.
Proc.new do |builder| builder.use EDH::HTTPService::MultipartRequest builder.request :url_encoded builder.adapter Faraday.default_adapter end
Class Attribute Summary collapse
-
.faraday_middleware ⇒ Object
A customized stack of Faraday middleware that will be used to make each request.
-
.http_options ⇒ Object
Returns the value of attribute http_options.
Class Method Summary collapse
-
.encode_params(param_hash) ⇒ Object
Encodes a given hash into a query string.
-
.make_request(path, args, verb, options = {}) ⇒ EDH::HTTPService::Response
Makes a request directly to Passport.
-
.server(options = {}) ⇒ Object
The address of the appropriate Passport server.
Class Attribute Details
.faraday_middleware ⇒ Object
A customized stack of Faraday middleware that will be used to make each request.
9 10 11 |
# File 'lib/edh/http_service.rb', line 9 def faraday_middleware @faraday_middleware end |
.http_options ⇒ Object
Returns the value of attribute http_options.
10 11 12 |
# File 'lib/edh/http_service.rb', line 10 def @http_options end |
Class Method Details
.encode_params(param_hash) ⇒ Object
Encodes a given hash into a query string. This is used mainly by the Batch API nowadays, since Faraday handles this for regular cases.
91 92 93 94 95 96 |
# File 'lib/edh/http_service.rb', line 91 def self.encode_params(param_hash) ((param_hash || {}).sort_by{|k, v| k.to_s}.collect do |key_and_value| key_and_value[1] = MultiJson.dump(key_and_value[1]) unless key_and_value[1].is_a? String "#{key_and_value[0].to_s}=#{CGI.escape key_and_value[1]}" end).join("&") end |
.make_request(path, args, verb, options = {}) ⇒ EDH::HTTPService::Response
You’ll rarely need to call this method directly.
Makes a request directly to Passport.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/edh/http_service.rb', line 56 def self.make_request(path, args, verb, = {}) # if the verb isn't get or post, send it as a post argument args.merge!({:method => verb}) && verb = "post" if verb != "get" && verb != "post" # turn all the keys to strings (Faraday has issues with symbols under 1.8.7) params = args.inject({}) {|hash, kv| hash[kv.first.to_s] = kv.last; hash} # figure out our options for this request = {:params => (verb == "get" ? params : {})}.merge( || {}).merge(()) if [:use_ssl] ssl = ([:ssl] ||= {}) ssl[:verify] = true unless ssl.has_key?(:verify) end # set up our Faraday connection # we have to manually assign params to the URL or the conn = Faraday.new(server(), , &(faraday_middleware || DEFAULT_MIDDLEWARE)) response = conn.send(verb, path, (verb == "post" ? params : {})) # Log URL information EDH::Utils.debug "#{verb.upcase}: #{path} params: #{params.inspect}" EDH::HTTPService::Response.new(response.status.to_i, response.body, response.headers) end |
.server(options = {}) ⇒ Object
The address of the appropriate Passport server.
31 32 33 34 35 36 37 38 |
# File 'lib/edh/http_service.rb', line 31 def self.server( = {}) if [:server] [:server] else server = Passport::REST_SERVER "#{[:use_ssl] ? "https" : "http"}://#{server}" end end |