Class: BadPigeon::HARRequest

Inherits:
Object
  • Object
show all
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

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_nameObject



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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

#inspectObject



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

#methodObject



19
20
21
# File 'lib/bad_pigeon/har/har_request.rb', line 19

def method
  @json['request']['method'].downcase.to_sym
end

#mime_typeObject



50
51
52
# File 'lib/bad_pigeon/har/har_request.rb', line 50

def mime_type
  @json['response']['content']['mimeType']
end

#paramsObject



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_bodyObject



58
59
60
# File 'lib/bad_pigeon/har/har_request.rb', line 58

def response_body
  @json['response']['content']['text']
end

#response_jsonObject



62
63
64
# File 'lib/bad_pigeon/har/har_request.rb', line 62

def response_json
  response_body && JSON.parse(response_body)
end

#statusObject



46
47
48
# File 'lib/bad_pigeon/har/har_request.rb', line 46

def status
  @json['response']['status']
end

#urlObject



23
24
25
# File 'lib/bad_pigeon/har/har_request.rb', line 23

def url
  @json['request']['url']
end