Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AlexcastroDev/34005a524cbe24e9dfb186c8eb81e2be to your computer and use it in GitHub Desktop.
Save AlexcastroDev/34005a524cbe24e9dfb186c8eb81e2be to your computer and use it in GitHub Desktop.
something_lib.rb
require 'time'
module SomethingLib
class Images
@last_called = nil
@call_count = 0
BING_API_LIMIT = 5
INTERVAL = 1 # 1 second interval
def self.generate(title)
if rate_limited?
puts "|SomethingLib| BingImageSearch global limit exceeded, waiting..."
return []
end
begin
# Simulate BingImageSearch::search
response = fake_bing_image_search(title)
images = response.map { |i| i[:contentUrl] }
images
rescue => e
puts "|FlectoAI| BingImageSearch error: #{e.message}"
[]
end
end
private
def self.rate_limited?
now = Time.now
if @last_called.nil? || now - @last_called >= INTERVAL
@last_called = now
@call_count = 1
false
elsif @call_count < BING_API_LIMIT
@call_count += 1
false
else
true
end
end
def self.fake_bing_image_search(title)
# Simulating an API response
[
{ contentUrl: "http://example.com/image1.jpg" },
{ contentUrl: "http://example.com/image2.jpg" }
]
end
end
end
# Example usage
images = SomethingLib::Images.generate("example title")
puts images
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment