Created
February 12, 2025 19:25
-
-
Save ysim-precich/905b886e24daec9eec8cd3496c7f73dc to your computer and use it in GitHub Desktop.
EasyBroker API
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
| require 'uri' | |
| require 'net/http' | |
| require 'JSON' | |
| class EasyBrokerReader | |
| attr_reader :titles | |
| def initialize(url) | |
| @titles = [] | |
| @url = url | |
| @http = nil | |
| setup_http | |
| manage_titles | |
| end | |
| def setup_http | |
| uri = URI(@url) | |
| @http = Net::HTTP.new(uri.host, uri.port) | |
| @http.use_ssl = true | |
| end | |
| def manage_titles | |
| body = request_properties | |
| body["content"].each { |property| @titles << property["title"] } | |
| @url = body["pagination"]["next_page"] | |
| manage_titles unless @url.nil? | |
| end | |
| def request_properties | |
| request = Net::HTTP::Get.new(URI(@url)) | |
| request["accept"] = 'application/json' | |
| request["X-Authorization"] = 'l7u502p8v46ba3ppgvj5y2aad50lb9' | |
| response = @http.request(request) | |
| JSON.parse(response.body) | |
| end | |
| end | |
| # Testing with native Minitest | |
| require "minitest/autorun" | |
| class TestApi < Minitest::Test | |
| def setup | |
| # Using class variable to run the api call only once per suite | |
| @@titles ||= EasyBrokerReader.new("https://api.stagingeb.com/v1/properties?limit=50").titles | |
| end | |
| def test_amount_of_records | |
| assert_equal 1107, @@titles.count | |
| end | |
| def test_first_title | |
| assert_equal 'Casa bien bonita', @@titles.first | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment