lib/hackerone/client.rb in hackerone-client-0.3.2 vs lib/hackerone/client.rb in hackerone-client-0.4.0
- old
+ new
@@ -1,10 +1,13 @@
require "faraday"
-require 'active_support/time'
+require "json"
+require "active_support/time"
require_relative "client/version"
require_relative "client/report"
require_relative "client/activity"
+require_relative "client/program"
+require_relative "client/reporter"
module HackerOne
module Client
class NotConfiguredError < StandardError; end
@@ -50,10 +53,26 @@
def program
@program || HackerOne::Client.program
end
+ def reporters
+ raise ArgumentError, "Program cannot be nil" unless program
+ response = self.class.hackerone_api_connection.get do |req|
+ req.url "programs/#{Program.find(program).id}/reporters"
+ end
+
+ data = self.class.parse_response(response)
+ if data.nil?
+ raise RuntimeError, "Expected data attribute in response: #{response.body}"
+ end
+
+ data.map do |reporter|
+ Reporter.new(reporter)
+ end
+ end
+
## Returns all open reports, optionally with a time bound
#
# program: the HackerOne program to search on (configure globally with Hackerone::Client.program=)
# since (optional): a time bound, don't include reports earlier than +since+. Must be a DateTime object.
#
@@ -67,14 +86,11 @@
"filter[created_at__gt]" => since.iso8601
}
req.url "reports", options
end
- data = JSON.parse(response.body, :symbolize_names => true)[:data]
- if data.nil?
- raise RuntimeError, "Expected data attribute in response: #{response.body}"
- end
+ data = self.class.parse_response(response)
data.map do |report|
Report.new(report)
end
end
@@ -106,11 +122,11 @@
reference: reference
}
}
}
- post("reports/#{id}/issue_tracker_reference_id", body)
+ Report.new(post("reports/#{id}/issue_tracker_reference_id", body))
end
## Idempotent: change the state of a report. See STATES for valid values.
#
# id: the ID of the report
@@ -146,11 +162,11 @@
# id: the ID of a specific report
#
# returns an HackerOne::Client::Report object or raises an error if
# no report is found.
def report(id)
- get("reports/#{id}")
+ Report.new(get("reports/#{id}"))
end
private
def post(endpoint, body)
response = with_retry do
@@ -159,11 +175,11 @@
req.body = body.to_json
req.url endpoint
end
end
- parse_response(response)
+ self.class.parse_response(response)
end
def get(endpoint, params = nil)
response = with_retry do
self.class.hackerone_api_connection.get do |req|
@@ -171,19 +187,19 @@
req.params = params || {}
req.url endpoint
end
end
- parse_response(response)
+ self.class.parse_response(response)
end
- def parse_response(response)
+ def self.parse_response(response)
if response.status.to_s.start_with?("4")
raise ArgumentError, "API called failed, probably your fault: #{response.body}"
elsif response.status.to_s.start_with?("5")
raise RuntimeError, "API called failed, probably their fault: #{response.body}"
elsif response.success?
- Report.new(JSON.parse(response.body, :symbolize_names => true)[:data])
+ JSON.parse(response.body, :symbolize_names => true)[:data]
else
raise RuntimeError, "Not sure what to do here: #{response.body}"
end
end