Skip to content

Instantly share code, notes, and snippets.

@MGalv
Created January 27, 2025 14:57
Show Gist options
  • Save MGalv/725014bf63280a5f93c0b7693eafc75e to your computer and use it in GitHub Desktop.
Save MGalv/725014bf63280a5f93c0b7693eafc75e to your computer and use it in GitHub Desktop.
easybroker
require 'net/http'
require 'json'
class EasyBrokerClient
BASE_URL = 'https://api.stagingeb.com/v1'.freeze
def initialize(api_key)
@api_key = api_key
end
def fetch_properties
endpoint = '/properties'
properties = []
page = 1
loop do
response = make_request("#{endpoint}?page=#{page}")
break unless response['content']
properties.concat(response['content'])
break if response['pagination']['next_page'].nil?
page += 1
end
properties
end
private
def make_request(path)
uri = URI("#{BASE_URL}#{path}")
request = Net::HTTP::Get.new(uri)
request['X-Authorization'] = @api_key
response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
http.request(request)
end
raise "API Error: #{response.code}" unless response.is_a?(Net::HTTPSuccess)
JSON.parse(response.body)
end
end
class PropertyPrinter
def initialize(client)
@client = client
end
def print_titles
properties = @client.fetch_properties
properties.each { |property| puts property['title'] }
end
end
# Example usage (replace YOUR_API_KEY with the API key from EasyBroker)
# client = EasyBrokerClient.new('YOUR_API_KEY')
# printer = PropertyPrinter.new(client)
# printer.print_titles
require 'minitest/autorun'
require_relative 'easy_broker_client'
class EasyBrokerClientTest < Minitest::Test
def setup
@client = EasyBrokerClient.new('YOUR_API_KEY')
end
def test_fetch_properties
mock_response = {
'content' => [
{ 'title' => 'Propiedad 1' },
{ 'title' => 'Propiedad 2' }
],
'pagination' => { 'next_page' => nil }
}
@client.stub :make_request, mock_response do
properties = @client.fetch_properties
assert_equal 2, properties.size
assert_equal 'Propiedad 1', properties.first['title']
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment