lib/dato/api_client.rb in dato-0.8.0 vs lib/dato/api_client.rb in dato-0.8.1
- old
+ new
@@ -1,20 +1,20 @@
# frozen_string_literal: true
-require 'faraday'
-require 'faraday_middleware'
-require 'json'
-require 'json_schema'
-require 'active_support/core_ext/hash/indifferent_access'
-require 'active_support/inflector'
+require "faraday"
+require "faraday_middleware"
+require "json"
+require "json_schema"
+require "active_support/core_ext/hash/indifferent_access"
+require "active_support/inflector"
-require 'dato/version'
-require 'dato/repo'
+require "dato/version"
+require "dato/repo"
-require 'dato/api_error'
+require "dato/api_error"
-require 'cacert'
+require "cacert"
module Dato
module ApiClient
def self.included(base)
base.extend ClassMethods
@@ -39,44 +39,42 @@
end
end
def respond_to_missing?(method, include_private = false)
json_schema.definitions.each do |type, obj|
- is_collection = obj.links.select { |x| x.rel == 'instances' }.any?
+ is_collection = obj.links.select { |x| x.rel == "instances" }.any?
namespace = is_collection ? type.pluralize : type
- if method.to_s === namespace
- return true
- end
+ return true if method.to_s == namespace
end
super
end
def method_missing(method, *args, &block)
json_schema.definitions.each do |type, obj|
- is_collection = obj.links.select { |x| x.rel == 'instances' }.any?
+ is_collection = obj.links.select { |x| x.rel == "instances" }.any?
namespace = is_collection ? type.pluralize : type
- if method.to_s === namespace
- instance_variable_set(
- "@#{namespace}",
- instance_variable_get("@#{namespace}") ||
- Dato::Repo.new(self, type, obj)
- )
+ next unless method.to_s == namespace
- return instance_variable_get("@#{namespace}")
- end
+ instance_variable_set(
+ "@#{namespace}",
+ instance_variable_get("@#{namespace}") ||
+ Dato::Repo.new(self, type, obj),
+ )
+
+ return instance_variable_get("@#{namespace}")
end
super
end
def json_schema
@json_schema ||= begin
response = Faraday.get(
# "http://#{subdomain}.lvh.me:3001/docs/#{subdomain}-hyperschema.json"
- "#{base_url}/docs/#{self.class.subdomain}-hyperschema.json"
+ "#{base_url}/docs/#{self.class.subdomain}-hyperschema.json",
)
schema = JsonSchema.parse!(JSON.parse(response.body))
schema.expand_references!
@@ -107,25 +105,25 @@
c.params = params if params
end
response.body.with_indifferent_access if response.body.is_a?(Hash)
rescue Faraday::SSLError => e
- raise e if ENV['SSL_CERT_FILE'] == Cacert.pem
+ raise e if ENV["SSL_CERT_FILE"] == Cacert.pem
Cacert.set_in_env
request(*args)
rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
puts e.message
raise e
rescue Faraday::ClientError => e
if e.response[:status] == 429
- to_wait = e.response[:headers]['x-ratelimit-reset'].to_i
+ to_wait = e.response[:headers]["x-ratelimit-reset"].to_i
puts "Rate limit exceeded, waiting #{to_wait} seconds..."
sleep(to_wait + 1)
request(*args)
elsif e.response[:status] == 422 && batch_data_validation?(e.response)
- puts 'Validating items, waiting 1 second and retrying...'
+ puts "Validating items, waiting 1 second and retrying..."
sleep(1)
request(*args)
else
# puts body.inspect
# puts '===='
@@ -138,40 +136,38 @@
private
def batch_data_validation?(response)
body = begin
- JSON.parse(response[:body])
- rescue JSON::ParserError => e
- nil
- end
+ JSON.parse(response[:body])
+ rescue JSON::ParserError
+ nil
+ end
return false unless body
- return false unless body['data']
+ return false unless body["data"]
- body['data'].any? do |e|
- e['attributes']['code'] == 'BATCH_DATA_VALIDATION_IN_PROGRESS'
+ body["data"].any? do |e|
+ e["attributes"]["code"] == "BATCH_DATA_VALIDATION_IN_PROGRESS"
end
rescue StandardError
false
end
def connection
default_headers = {
- 'Accept' => 'application/json',
- 'Content-Type' => 'application/json',
- 'Authorization' => "Bearer #{@token}",
- 'User-Agent' => "ruby-client v#{Dato::VERSION}",
- 'X-Api-Version' => '3'
+ "Accept" => "application/json",
+ "Content-Type" => "application/json",
+ "Authorization" => "Bearer #{@token}",
+ "User-Agent" => "ruby-client v#{Dato::VERSION}",
+ "X-Api-Version" => "3",
}
- if environment
- default_headers.merge!('X-Environment' => environment)
- end
+ default_headers.merge!("X-Environment" => environment) if environment
options = {
url: base_url,
- headers: default_headers.merge(extra_headers)
+ headers: default_headers.merge(extra_headers),
}
@connection ||= Faraday.new(options) do |c|
c.request :json
c.response :json, content_type: /\bjson$/