Created
February 7, 2026 06:15
-
-
Save SergioGomez321/0987390990973d0a0c35dfb6e2998f6f to your computer and use it in GitHub Desktop.
EasyBroker API – List All Properties by Sergio Gomez
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| require "uri" | |
| require "net/http" | |
| require "json" | |
| require_relative "property" | |
| class EasyBrokerClient | |
| DEFAULT_BASE_URL = "https://api.stagingeb.com/v1" | |
| DEFAULT_LIMIT = 50 | |
| def initialize( | |
| api_key: ENV.fetch("EASYBROKER_API_KEY"), | |
| base_url: ENV.fetch("EASYBROKER_BASE_URL", DEFAULT_BASE_URL) | |
| ) | |
| raise ArgumentError, "EASYBROKER_API_KEY is required" if api_key.strip.empty? | |
| @api_key = api_key | |
| @base_url = base_url | |
| end | |
| def each_property(limit: DEFAULT_LIMIT) | |
| url = "#{@base_url}/properties?#{URI.encode_www_form(limit: limit, page: 1)}" | |
| loop do | |
| data = get_json(url) | |
| data.fetch("content", []).each do |attrs| | |
| yield Property.new(attrs) | |
| end | |
| next_url = data.dig("pagination", "next_page") | |
| break if next_url.nil? || next_url.to_s.strip.empty? | |
| url = next_url | |
| end | |
| end | |
| private | |
| def get_json(url) | |
| uri = URI(url) | |
| http = Net::HTTP.new(uri.host, uri.port) | |
| http.use_ssl = (uri.scheme == "https") | |
| req = Net::HTTP::Get.new(uri) | |
| req["accept"] = "application/json" | |
| req["X-Authorization"] = @api_key | |
| res = http.request(req) | |
| case res.code.to_i | |
| when 200..299 | |
| JSON.parse(res.body) | |
| when 401 | |
| raise "Unauthorized (check EASYBROKER_API_KEY)" | |
| when 429 | |
| raise "Rate limited (HTTP 429)" | |
| else | |
| raise "HTTP #{res.code}: #{res.body}" | |
| end | |
| rescue JSON::ParserError | |
| raise "Invalid JSON response" | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| source "https://rubygems.org" | |
| gem "faraday" | |
| gem "json" | |
| group :test do | |
| gem "rspec" | |
| gem "webmock" | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| require_relative "easybroker_client" | |
| client = EasyBrokerClient.new | |
| client.each_property do |property| | |
| puts property.title | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| # modelo simple para el caso de uso | |
| class Property | |
| attr_reader :title, :public_id | |
| def initialize(attrs) | |
| @title = attrs.fetch("title", nil) | |
| @public_id = attrs.fetch("public_id", nil) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| require "minitest/autorun" | |
| require_relative "easybroker_client" | |
| class EasyBrokerClientTest < Minitest::Test | |
| def test_each_property_paginates_following_next_page_url_and_yields_property_objects | |
| client = EasyBrokerClient.new(api_key: "x", base_url: "https://example.test/v1") | |
| calls = 0 | |
| client.define_singleton_method(:get_json) do |url| | |
| calls += 1 | |
| if calls == 1 | |
| { | |
| "pagination" => { "next_page" => "https://example.test/v1/properties?page=2&limit=50" }, | |
| "content" => [{ "title" => "Casa 1", "public_id" => "EB-1" }] | |
| } | |
| else | |
| { | |
| "pagination" => { "next_page" => nil }, | |
| "content" => [{ "title" => "Casa 2", "public_id" => "EB-2" }] | |
| } | |
| end | |
| end | |
| yielded = [] | |
| client.each_property { |p| yielded << p.title } | |
| assert_equal ["Casa 1", "Casa 2"], yielded | |
| assert_equal 2, calls | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nota:
export EASYBROKER_API_KEY = "API_KEY"
ruby main.rb > titles.txt
Utilice estos comandos para probarlo, decidí imprimir el resultado en un texto para visualizar mejor el resultado final.