Skip to content

Instantly share code, notes, and snippets.

@RickSandov
Created July 25, 2024 07:19
Show Gist options
  • Save RickSandov/bdbd01e6ad579346d683ee6b49128456 to your computer and use it in GitHub Desktop.
Save RickSandov/bdbd01e6ad579346d683ee6b49128456 to your computer and use it in GitHub Desktop.
Títulos de propiedades de staging api de EasyBroker
require 'net/http'
require 'json'
require 'uri'
class EasyBrokerClient
BASE_URI = 'https://api.stagingeb.com/v1'.freeze
def initialize(api_key)
@api_key = api_key
end
def get_properties
uri = URI("#{BASE_URI}/properties?page=1&limit=20")
request = Net::HTTP::Get.new(uri)
request['X-Authorization'] = @api_key
request['accept'] = 'application/json'
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
http.request(request)
end
if response.is_a?(Net::HTTPSuccess)
JSON.parse(response.body)['content']
else
raise "Failed to fetch properties: #{response.message}"
end
end
def print_property_titles
properties = get_properties
properties.each do |property|
puts property['title']
end
end
end
# Ejemplo de uso
api_key = 'l7u502p8v46ba3ppgvj5y2aad50lb9'
client = EasyBrokerClient.new(api_key)
client.print_property_titles
# test_easy_broker_client.rb
require 'minitest/autorun'
require_relative 'easy_broker_client'
class TestEasyBrokerClient < Minitest::Test
def setup
@api_key = 'l7u502p8v46ba3ppgvj5y2aad50lb9' # API key de staging
@client = EasyBrokerClient.new(@api_key)
end
def test_get_properties
properties = @client.get_properties
assert_instance_of Array, properties
assert properties.first.key?('title')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment