|
| 1 | +require 'net/http' |
| 2 | +require 'json' |
| 3 | +require 'fb/http_error' |
| 4 | + |
| 5 | +module Fb |
| 6 | + # A wrapper around +Net::HTTP+ to send HTTP requests to any web API and |
| 7 | + # return their result or raise an error if the result is unexpected. |
| 8 | + # The basic way to use Request is by calling +run+ on an instance. |
| 9 | + # @example Get information about Fullscreen’s Facebook page |
| 10 | + # path = '/v2.10/221406534569729' |
| 11 | + # params = {fields: 'id,link,about', access_token: access_token} |
| 12 | + # response = Fb::HTTPRequest.new(path: path, params: params).run |
| 13 | + # response.body |
| 14 | + # @api private |
| 15 | + class HTTPRequest |
| 16 | + # Initializes a Request object. |
| 17 | + # @param [Hash] options the options for the request. |
| 18 | + # @option options [Class] :expected_response (Net::HTTPSuccess) The class |
| 19 | + # of response that the request should obtain when run. |
| 20 | + # @option options [String] :host The host of the request URI. |
| 21 | + # @option options [String] :path The path of the request URI. |
| 22 | + # @option options [Hash] :params ({}) The params to use as the query |
| 23 | + # component of the request URI, for instance the Hash +{a: 1, b: 2}+ |
| 24 | + # corresponds to the query parameters +"a=1&b=2"+. |
| 25 | + # @option options [#size] :body The body of the request. |
| 26 | + # @option options [Proc] :error_message The block that will be invoked |
| 27 | + # when a request fails. |
| 28 | + def initialize(options = {}) |
| 29 | + @expected_response = options.fetch :expected_response, Net::HTTPSuccess |
| 30 | + @host = options.fetch :host, 'graph.facebook.com' |
| 31 | + @path = options[:path] |
| 32 | + @params = options.fetch :params, {} |
| 33 | + end |
| 34 | + |
| 35 | + # Sends the request and returns the response with the body parsed from JSON. |
| 36 | + # @return [Net::HTTPResponse] if the request succeeds. |
| 37 | + # @raise [Fb::HTTPError] if the request fails. |
| 38 | + def run |
| 39 | + if response.is_a? @expected_response |
| 40 | + response.tap do |
| 41 | + parse_response! |
| 42 | + end |
| 43 | + else |
| 44 | + raise HTTPError.new(error_message, response: response) |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + # @return [String] the request URL. |
| 49 | + def url |
| 50 | + uri.to_s |
| 51 | + end |
| 52 | + |
| 53 | + private |
| 54 | + |
| 55 | + # @return [URI::HTTPS] the (memoized) URI of the request. |
| 56 | + def uri |
| 57 | + @uri ||= URI::HTTPS.build host: @host, path: @path, query: query |
| 58 | + end |
| 59 | + |
| 60 | + def query |
| 61 | + URI.encode_www_form @params |
| 62 | + end |
| 63 | + |
| 64 | + # @return [Net::HTTPRequest] the full HTTP request object. |
| 65 | + def http_request |
| 66 | + @http_request ||= Net::HTTP::Get.new uri.request_uri |
| 67 | + end |
| 68 | + |
| 69 | + def as_curl |
| 70 | + 'curl'.tap do |curl| |
| 71 | + http_request.each_header{|k, v| curl << %Q{ -H "#{k}: #{v}"}} |
| 72 | + curl << %Q{ "#{url}"} |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + # Run the request and memoize the response or the server error received. |
| 77 | + def response |
| 78 | + @response ||= Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| |
| 79 | + curl_request = as_curl |
| 80 | + p curl_request if Fb.configuration.developing? |
| 81 | + http.request http_request |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + # Replaces the body of the response with the parsed version of the body, |
| 86 | + # according to the format specified in the HTTPRequest. |
| 87 | + def parse_response! |
| 88 | + if response.body |
| 89 | + response.body = JSON response.body |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + def error_message |
| 94 | + JSON(response.body)['error']['message'] |
| 95 | + end |
| 96 | + end |
| 97 | +end |
0 commit comments