Skip to content

Instantly share code, notes, and snippets.

@kishikawakatsumi
Last active January 20, 2021 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kishikawakatsumi/83c4ef41bb03eb2a4cf80af794fc6760 to your computer and use it in GitHub Desktop.
Save kishikawakatsumi/83c4ef41bb03eb2a4cf80af794fc6760 to your computer and use it in GitHub Desktop.
require "net/http"
require "uri"
require "pty"
require "expect"
require "fastlane"
require "spaceship"
def fastlane_spaceauth(user, password, default_phone_number)
ENV["FASTLANE_USER"] = user
ENV["FASTLANE_PASSWORD"] = password
ENV["SPACESHIP_2FA_SMS_DEFAULT_PHONE_NUMBER"] = default_phone_number
$expect_verbose = true
cmd = "fastlane spaceauth"
PTY.spawn(cmd) do |i, o|
o.sync = true
i.expect(/Pass the following via the FASTLANE_SESSION environment variable:/, 10) do |match|
if match
o.puts "y"
return
end
end
i.expect(/Please enter the 6 digit code you received at .+:/, 60) do |match|
raise "UnknownError" unless match
sleep 10
now = Time.now.utc - 120
date_start = now.strftime("%Y-%m-%dT%H:%M:%SZ")
date_end = (now + 120 + 120).strftime("%Y-%m-%dT%H:%M:%SZ")
api_key = ENV["VONAGE_API_KEY"]
api_secret = ENV["VONAGE_API_SECRET"]
uri = URI.parse("https://api.nexmo.com/v2/reports/records?account_id=#{api_key}&product=SMS&direction=inbound&include_message=true&date_start=#{date_start}&date_end=#{date_end}")
request = Net::HTTP::Get.new(uri)
request.basic_auth(api_key, api_secret)
options = {
use_ssl: true,
}
response = Net::HTTP.start(uri.hostname, uri.port, options) do |http|
http.request(request)
end
puts response.code
puts response.body
records = JSON.parse(response.body)["records"]
if records.nil? || records.empty?
raise "NotFoundError"
end
message_body = records[0]["message_body"]
code = message_body[/\d{6}/]
if code.nil? || code.empty?
raise "NotFoundError"
end
puts code
o.puts code
end
i.expect(/Pass the following via the FASTLANE_SESSION environment variable:/, 10) do |match|
raise "UnknownError" unless match
o.puts "y"
end
begin
while (i.eof? == false)
puts i.gets
end
rescue Errno::EIO
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment