Class: Diffbot::Request
- Inherits:
-
Object
- Object
- Diffbot::Request
- Defined in:
- lib/diffbot/request.rb
Class Attribute Summary collapse
-
.testing ⇒ Object
Public: Whether we are in test mode or not (so we stub out HTTP calls).
Instance Attribute Summary collapse
-
#token ⇒ Object
readonly
The API token for Diffbot.
Instance Method Summary collapse
-
#build_request(method, query_params = {}) ⇒ Object
Build the hash of options that Excon requires for an HTTP request.
-
#initialize(token, test_mode = self.class.testing) ⇒ Request
constructor
Public: Initialize a new request to the API.
-
#perform(method, endpoint, query = {}) {|request_options| ... } ⇒ Object
Public: Perform an HTTP request against Diffbot’s API.
Constructor Details
#initialize(token, test_mode = self.class.testing) ⇒ Request
Public: Initialize a new request to the API.
token - The API token for Diffbot. test_mode - Whether requests are in test mode or not. This is passed to
Excon so we can mock connections.
21 22 23 24 |
# File 'lib/diffbot/request.rb', line 21 def initialize(token, test_mode=self.class.testing) @token = token @test_mode = test_mode end |
Class Attribute Details
.testing ⇒ Object
Public: Whether we are in test mode or not (so we stub out HTTP calls). Defaults to false.
8 9 10 |
# File 'lib/diffbot/request.rb', line 8 def testing @testing end |
Instance Attribute Details
#token ⇒ Object (readonly)
The API token for Diffbot.
14 15 16 |
# File 'lib/diffbot/request.rb', line 14 def token @token end |
Instance Method Details
#build_request(method, query_params = {}) ⇒ Object
Build the hash of options that Excon requires for an HTTP request.
method - A Symbol with the HTTP method (:get, :post, etc). query_params - Any query parameters to add to the request.
Returns a Hash.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/diffbot/request.rb', line 50 def build_request(method, query_params={}) query = { token: token }.merge(query_params) request = { query: query, method: method, headers: {}, mock: @test_mode } if Diffbot.instrumentor request.update( instrumentor: Diffbot.instrumentor, instrumentor_name: "diffbot" ) end request end |
#perform(method, endpoint, query = {}) {|request_options| ... } ⇒ Object
Public: Perform an HTTP request against Diffbot’s API.
method - The request method, one of :get, :head, :post, :put, or
:delete.
endpoint - The URL to which we’ll make the request, as a String. query - A hash of query string params we want to pass along.
Yields the request hash before making the request.
Returns the response.
36 37 38 39 40 41 42 |
# File 'lib/diffbot/request.rb', line 36 def perform(method, endpoint, query={}) = build_request(method, query) yield if block_given? request = Excon.new(endpoint) request.request() end |