Last active
January 5, 2024 23:15
-
-
Save eduardogpg/d477ec7cf15b545a4999b43295b361f7 to your computer and use it in GitHub Desktop.
This file contains 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 'uri' | |
require 'net/http' | |
require 'json' | |
class EasyBrokerAPI | |
BASE_URL = "https://api.stagingeb.com/v1/properties" | |
def initialize(access_token, page: 1, limit: 20) | |
@page = page | |
@limit = limit | |
@access_token = access_token | |
end | |
def fetch_properties | |
url = URI("#{BASE_URL}?page=#{@page}&limit=#{@limit}") | |
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"] = @access_token | |
response = http.request(request) | |
parse_response(response) if response.code == '200' | |
rescue | |
puts "Error fetching properties" | |
end | |
def retrieve_properties | |
content = fetch_properties | |
return unless content | |
content.each { |property| puts property['title'] } | |
end | |
private | |
def parse_response(response) | |
JSON.parse(response.read_body)['content'] | |
rescue JSON::ParserError => e | |
puts "Error parsing response: #{e.message}" | |
end | |
end | |
# For a this example the key is hardcoded, but in a real scenario it should be in a .env file | |
api = EasyBrokerAPI.new('l7u502p8v46ba3ppgvj5y2aad50lb9', page: 1, limit: 20) | |
properties = api.retrieve_properties | |
# Test | |
# https://gist.github.com/eduardogpg/5f58a69e5356b4e0428533cd50cb03db |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment