Skip to content

Instantly share code, notes, and snippets.

@jvidalba1
Last active November 30, 2023 21:39
Show Gist options
  • Save jvidalba1/1aabfb70997c1ade84d4481e1db889d9 to your computer and use it in GitHub Desktop.
Save jvidalba1/1aabfb70997c1ade84d4481e1db889d9 to your computer and use it in GitHub Desktop.
Easy Broker Integration staging API
require 'json'
require 'httparty'
require 'rspec'
class Property
URL = '/properties'
def self.list
easy_broker = EasyBroker.new(URL)
properties = easy_broker.get
Presentation.print(properties)
end
end
class EasyBroker
BASE_URL = 'https://api.stagingeb.com/v1'.freeze
API_KEY = 'l7u502p8v46ba3ppgvj5y2aad50lb9'.freeze
def initialize(resource)
@resource = resource
end
def get
response = HTTParty.get(BASE_URL + @resource, headers: headers, query: query).parsed_response
raise response['error'] if response['error']
response['content']
rescue => e
p e
end
private
def headers
{
"X-Authorization" => "#{API_KEY}",
"Accept" => "application/json"
}
end
# Default values
def query
{
page: 1,
limit: 20
}
end
end
class Presentation
def self.print(resources)
resources.each do |resource|
p "Title: #{resource.dig('title')}"
end
end
end
class InvalidApiKeyError < StandardError; end
# p Property.list
##### Unit tests
describe Property do
let(:property_url) { '/properties' }
it 'responds to own url' do
expect(described_class::URL).to eq property_url
end
describe '.list' do
let(:properties) do
[
{ "public_id" => "EB-D834", "title" => "Listing 1" },
{ "public_id" => "EB-D835", "title" => "Listing 2" }
]
end
it 'shows all properties' do
allow_any_instance_of(EasyBroker).to receive(:get).and_return(properties)
expect(Presentation).to receive(:print).and_return(properties)
Property.list
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment