Skip to content

Instantly share code, notes, and snippets.

@ysim-precich
Created February 12, 2025 19:25
Show Gist options
  • Select an option

  • Save ysim-precich/905b886e24daec9eec8cd3496c7f73dc to your computer and use it in GitHub Desktop.

Select an option

Save ysim-precich/905b886e24daec9eec8cd3496c7f73dc to your computer and use it in GitHub Desktop.

Revisions

  1. ysim-precich created this gist Feb 12, 2025.
    55 changes: 55 additions & 0 deletions api.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    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