Class: BadPigeon::HARRequest
- Inherits:
-
Object
- Object
- BadPigeon::HARRequest
- Defined in:
- lib/bad_pigeon/har/har_request.rb
Overview
Represents info about one request and response to it, including the complete response data (#response_body, or #response_json for a parsed JSON form).
Requests that may potentially include tweet data return true from the #includes_tweet_data? method. The JSON data from each such request represents a “timeline” and may be parsed using a specific timeline class like HomeTimeline or UserTimeline; the TIMELINE_TYPES hash provides a mapping of GraphQL endpoint names to timeline classes, and the endpoint name can be read using #endpoint_name method.
Instance Method Summary collapse
- #endpoint_name ⇒ Object
- #graphql_endpoint? ⇒ Boolean
- #has_json_response? ⇒ Boolean
- #includes_tweet_data? ⇒ Boolean
-
#initialize(json) ⇒ HARRequest
constructor
A new instance of HARRequest.
- #inspect ⇒ Object
- #method ⇒ Object
- #mime_type ⇒ Object
- #params ⇒ Object
- #response_body ⇒ Object
- #response_json ⇒ Object
- #status ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(json) ⇒ HARRequest
Returns a new instance of HARRequest.
15 16 17 |
# File 'lib/bad_pigeon/har/har_request.rb', line 15 def initialize(json) @json = json end |
Instance Method Details
#endpoint_name ⇒ Object
37 38 39 |
# File 'lib/bad_pigeon/har/har_request.rb', line 37 def endpoint_name Addressable::URI.parse(url).path.split('/').last end |
#graphql_endpoint? ⇒ Boolean
27 28 29 30 31 |
# File 'lib/bad_pigeon/har/har_request.rb', line 27 def graphql_endpoint? url.start_with?('https://api.twitter.com/graphql/') || url.start_with?('https://twitter.com/i/api/graphql/') || url.start_with?('https://x.com/i/api/graphql/') end |
#has_json_response? ⇒ Boolean
54 55 56 |
# File 'lib/bad_pigeon/har/har_request.rb', line 54 def has_json_response? mime_type.gsub(/;.*/, '').strip == 'application/json' end |
#includes_tweet_data? ⇒ Boolean
33 34 35 |
# File 'lib/bad_pigeon/har/har_request.rb', line 33 def includes_tweet_data? graphql_endpoint? && status == 200 && has_json_response? end |
#inspect ⇒ Object
66 67 68 69 70 |
# File 'lib/bad_pigeon/har/har_request.rb', line 66 def inspect keys = [:method, :url, :status] vars = keys.map { |k| "#{k}=#{self.send(k).inspect}" }.join(", ") "#<#{self.class}:0x#{object_id} #{vars}>" end |
#method ⇒ Object
19 20 21 |
# File 'lib/bad_pigeon/har/har_request.rb', line 19 def method @json['request']['method'].downcase.to_sym end |
#mime_type ⇒ Object
50 51 52 |
# File 'lib/bad_pigeon/har/har_request.rb', line 50 def mime_type @json['response']['content']['mimeType'] end |
#params ⇒ Object
41 42 43 44 |
# File 'lib/bad_pigeon/har/har_request.rb', line 41 def params vars = Addressable::URI.parse(url).query_values['variables'] vars && JSON.parse(vars) || {} end |
#response_body ⇒ Object
58 59 60 |
# File 'lib/bad_pigeon/har/har_request.rb', line 58 def response_body @json['response']['content']['text'] end |
#response_json ⇒ Object
62 63 64 |
# File 'lib/bad_pigeon/har/har_request.rb', line 62 def response_json response_body && JSON.parse(response_body) end |
#status ⇒ Object
46 47 48 |
# File 'lib/bad_pigeon/har/har_request.rb', line 46 def status @json['response']['status'] end |
#url ⇒ Object
23 24 25 |
# File 'lib/bad_pigeon/har/har_request.rb', line 23 def url @json['request']['url'] end |