class BigMarkerClient::Base
Constants
- DEFAULT_PER_PAGE_SIZE
Public Class Methods
delete(path, body = {})
click to toggle source
# File lib/big_marker_client/base.rb, line 21 def delete(path, body = {}) request(verb: :delete, path: path, params: body) end
get(path, params = {})
click to toggle source
# File lib/big_marker_client/base.rb, line 25 def get(path, params = {}) request(verb: :get, path: path, params: params) end
patch(path, body = {})
click to toggle source
# File lib/big_marker_client/base.rb, line 17 def patch(path, body = {}) request(verb: :patch, path: path, params: body) end
post(path, body = {})
click to toggle source
# File lib/big_marker_client/base.rb, line 9 def post(path, body = {}) request(verb: :post, path: path, params: body) end
put(path, body = {})
click to toggle source
# File lib/big_marker_client/base.rb, line 13 def put(path, body = {}) request(verb: :put, path: path, params: body) end
Protected Class Methods
loop_over(path, field, model_class, params = {}, method = :get)
click to toggle source
# File lib/big_marker_client/base.rb, line 37 def loop_over(path, field, model_class, params = {}, method = :get) page = 1 results = [] loop do params["page"] = page result = send(method, path, params) results += map_to_model_array(result[field], model_class) if result[field] break if break?(results: results, result: result, page: page, page_size: page_size(params)) page += 1 end results end
replace_path_params(path:, replacements: {})
click to toggle source
# File lib/big_marker_client/base.rb, line 31 def replace_path_params(path:, replacements: {}) new_path = path.dup replacements.each { |k, v| new_path.gsub!(k.to_s, v) } new_path end
Private Class Methods
base_url(path)
click to toggle source
# File lib/big_marker_client/base.rb, line 77 def base_url(path) Config.base_url + (path.start_with?("/") ? path : "/#{path}") end
break?(results:, result:, page:, page_size:)
click to toggle source
BigMarker API is a total mess that won’t return total_pages or total_entries on all request types so we have to get creative
# File lib/big_marker_client/base.rb, line 98 def break?(results:, result:, page:, page_size:) return true if break_on_full_metadata?(results: results, result: result, page: page) || break_on_partial_metadata?(results: results, result: result, page: page) results.length.zero? || (results.length % page_size) != 0 || (results.length.to_f / page_size) < page end
break_on_full_metadata?(results:, result:, page:)
click to toggle source
# File lib/big_marker_client/base.rb, line 105 def break_on_full_metadata?(results:, result:, page:) if !result["total_pages"].nil? && !total_count(result).nil? return ((page >= result["total_pages"].to_i) || (results.length >= total_count(result).to_i)) end false end
break_on_partial_metadata?(results:, result:, page:)
click to toggle source
# File lib/big_marker_client/base.rb, line 113 def break_on_partial_metadata?(results:, result:, page:) return page >= result["total_pages"].to_i unless result["total_pages"].nil? return results.length >= total_count(result).to_i unless total_count(result).nil? false end
check_preconditions(verb, path)
click to toggle source
# File lib/big_marker_client/base.rb, line 63 def check_preconditions(verb, path) raise ArgumentError, "http_method, path or api key is missing" if verb.nil? || path.nil? || Config.api_key.nil? raise ArgumentError, "unsupported http_method: #{verb}" unless %w[post put patch delete get].include?(verb.to_s) end
page_size(params)
click to toggle source
# File lib/big_marker_client/base.rb, line 91 def page_size(params) params["page_count"] || params.fetch("per_page", DEFAULT_PER_PAGE_SIZE) end
parse_body(body)
click to toggle source
# File lib/big_marker_client/base.rb, line 81 def parse_body(body) return nil if body.strip == "" json = JSON.parse(body) Config.logger.debug(json) if Config.debug json rescue JSON::ParserError raise ResponseError, "invalid response" end
request(path:, verb: :get, params: {})
click to toggle source
# File lib/big_marker_client/base.rb, line 53 def request(path:, verb: :get, params: {}) check_preconditions(verb, path) params = stringify_keys(params) params = params.to_json unless %w[get delete].include?(verb.to_s) @http_client ||= HttpClient.new response = @http_client.connection.send(verb.to_s, base_url(path), params) parse_body(response.body) end
stringify_keys(hash)
click to toggle source
# File lib/big_marker_client/base.rb, line 68 def stringify_keys(hash) hash_symbol_keys = hash.keys.select { |key| key.is_a?(Symbol) } hash_symbol_keys.each do |key| hash[key.to_s] = hash[key] hash.delete(key) end hash end
total_count(response)
click to toggle source
conferences#list is a total mess as requests require ‘page_count` instead of `per_page` as everywhere else and ti will return `total_count` instead of `total_entries` compared to the rest
# File lib/big_marker_client/base.rb, line 123 def total_count(response) response["total_entries"] || response["total_count"] end