Created
October 16, 2024 18:23
-
-
Save carlos-talavera/fb4b160c25ebb8b2b7187e684676cdfa to your computer and use it in GitHub Desktop.
Example of a Ruby class for fetching properties from the EasyBroker staging API, including RSpec tests using Webmock to simulate HTTP requests.
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 'net/http' | |
require 'json' | |
class EasyBrokerClient | |
BASE_URL = 'https://api.stagingeb.com/v1' | |
API_KEY = 'l7u502p8v46ba3ppgvj5y2aad50lb9' | |
def initialize | |
@headers = { 'X-Authorization' => API_KEY } | |
end | |
def fetch_properties | |
uri = URI("#{BASE_URL}/properties") | |
begin | |
response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| | |
request = Net::HTTP::Get.new(uri, @headers) | |
http.request(request) | |
end | |
case response | |
when Net::HTTPSuccess | |
JSON.parse(response.body)['content'] | |
else | |
puts "Error: Received HTTP #{response.code}" | |
[] | |
end | |
rescue SocketError => e | |
puts "Network error: #{e.message}" | |
[] | |
rescue JSON::ParserError => e | |
puts "Error parsing JSON: #{e.message}" | |
[] | |
rescue StandardError => e | |
puts "An unexpected error occurred: #{e.message}" | |
[] | |
end | |
end | |
def print_property_titles | |
fetch_properties.each do |property| | |
puts property['title'] | |
end | |
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
require 'rspec' | |
require 'webmock/rspec' | |
require_relative 'easy_broker_client' | |
RSpec.describe EasyBrokerClient do | |
before do | |
@client = EasyBrokerClient.new | |
end | |
describe '#print_property_titles' do | |
it 'prints the titles of properties' do | |
stub_request(:get, 'https://api.stagingeb.com/v1/properties') | |
.with(headers: { 'X-Authorization' => 'l7u502p8v46ba3ppgvj5y2aad50lb9' }) | |
.to_return( | |
status: 200, | |
body: { | |
pagination: { | |
limit: 20, | |
page: 1, | |
total: 1, | |
next_page: nil | |
}, | |
content: [ | |
{ | |
title: 'Beautiful property in Condesa' | |
} | |
] | |
}.to_json, | |
headers: { 'Content-Type' => 'application/json' } | |
) | |
expect { @client.print_property_titles }.to output("Beautiful property in Condesa\n").to_stdout | |
end | |
it 'handles non-200 HTTP responses' do | |
stub_request(:get, 'https://api.stagingeb.com/v1/properties') | |
.with(headers: { 'X-Authorization' => 'l7u502p8v46ba3ppgvj5y2aad50lb9' }) | |
.to_return(status: 404) | |
expect { @client.print_property_titles }.to output("Error: Received HTTP 404\n").to_stdout | |
end | |
it 'handles network errors gracefully' do | |
stub_request(:get, 'https://api.stagingeb.com/v1/properties') | |
.with(headers: { 'X-Authorization' => 'l7u502p8v46ba3ppgvj5y2aad50lb9' }) | |
.to_raise(SocketError) | |
expect { @client.print_property_titles }.to output(/Network error/).to_stdout | |
end | |
it 'handles JSON parsing errors gracefully' do | |
stub_request(:get, 'https://api.stagingeb.com/v1/properties') | |
.with(headers: { 'X-Authorization' => 'l7u502p8v46ba3ppgvj5y2aad50lb9' }) | |
.to_return(status: 200, body: 'invalid_json') | |
expect { @client.print_property_titles }.to output(/Error parsing JSON/).to_stdout | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment