Skip to content

Instantly share code, notes, and snippets.

@dev2me
Created June 17, 2023 20:36
Show Gist options
  • Save dev2me/6b4dbb8a3dbfcc7da581ced635246257 to your computer and use it in GitHub Desktop.
Save dev2me/6b4dbb8a3dbfcc7da581ced635246257 to your computer and use it in GitHub Desktop.
EasyBroker Integreation
require 'uri'
require 'net/http'
require 'openssl'
require 'json'
class EasyBrokerIntegration
attr_accessor :endpoint
API_ENDPOINT = "https://api.stagingeb.com/v1/properties?page=1&limit=50".freeze
def initialize
@properties = []
end
def title_properties
load_properties(API_ENDPOINT)
@properties
end
private
def load_properties(endpoint = nil)
response = connect(endpoint)
@properties += response['content'].map{ |property| property['title']}
if response['pagination']['next_page']
next_page_endpoint = response['pagination']['next_page']
load_properties(next_page_endpoint)
end
end
def connect(ep)
url = URI(ep)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url)
request["accept"] = 'application/json'
request["X-Authorization"] = 'l7u502p8v46ba3ppgvj5y2aad50lb9'
response = http.request(request)
JSON.parse(response.read_body)
end
end
require "test/unit"
require_relative 'easy_broker_integration'
class EasyBrokerIntegrationTest < Test::Unit::TestCase
def setup
@integration = EasyBrokerIntegration.new
end
def test_get_title_properties
assert_not_empty(@integration.title_properties, 'La lista de títulos de propiedades está vacía')
end
def test_connect
response = @integration.send(:connect, "https://api.stagingeb.com/v1/properties?page=1&limit=50")
assert_instance_of(Hash, response, 'La respuesta no es un hash')
assert_true(response.key?('content'), 'La respuesta no contiene la clave "content"')
assert_true(response.key?('pagination'), 'La respuesta no contiene la clave "pagination"')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment