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

Class Method Summary collapse

Class Attribute Details

.faraday_middlewareObject

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_optionsObject

Returns the value of attribute http_options.



10
11
12
# File 'lib/edh/http_service.rb', line 10

def http_options
  @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.

Examples:

EDH.http_service.encode_params({:a => 2, :b => "My String"})
=> "a=2&b=My+String"

Parameters:

  • params_hash

    a hash of values to CGI-encode and appropriately join

Returns:

  • the appropriately-encoded string



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

Note:

You’ll rarely need to call this method directly.

Makes a request directly to Passport.

Parameters:

  • path

    the server path for this request

  • verb

    the HTTP method to use. If not get or post, this will be turned into a POST request with the appropriate :method specified in the arguments.

  • args

    arguments to be sent to Passport

  • options (defaults to: {})

    request-related options for EDH and Faraday.

Returns:

Raises:

  • an appropriate connection error if unable to make the request to Passport

See Also:



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, options = {})
  # 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
  request_options = {:params => (verb == "get" ? params : {})}.merge(http_options || {}).merge(process_options(options))
  if request_options[:use_ssl]
    ssl = (request_options[: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(request_options), request_options, &(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.

Parameters:

  • options (defaults to: {})

    various flags to indicate which server to use.

Options Hash (options):

  • :rest_api (Object)

    use the old REST API instead of the Graph API

  • :use_ssl (Object)

    force https, even if not needed

Returns:

  • a complete server address with protocol



31
32
33
34
35
36
37
38
# File 'lib/edh/http_service.rb', line 31

def self.server(options = {})
  if options[:server]
    options[:server]
  else
    server = Passport::REST_SERVER
    "#{options[:use_ssl] ? "https" : "http"}://#{server}"
  end
end