lib/dato/api_client.rb in dato-0.7.10 vs lib/dato/api_client.rb in dato-0.7.13
- old
+ new
@@ -18,40 +18,41 @@
module ApiClient
def self.included(base)
base.extend ClassMethods
base.class_eval do
- attr_reader :token, :base_url, :schema, :extra_headers
+ attr_reader :token, :environment, :base_url, :schema, :extra_headers
end
end
module ClassMethods
def json_schema(subdomain)
define_method(:initialize) do |token, options = {}|
@token = token
@base_url = options[:base_url] || "https://#{subdomain}.datocms.com"
+ @environment = options[:environment]
@extra_headers = options[:extra_headers] || {}
end
+ # FOR DEV
+ # "http://#{subdomain}.lvh.me:3001/docs/#{subdomain}-hyperschema.json"
response = Faraday.get(
- # FOR DEV
- # "http://#{subdomain}.lvh.me:3001/docs/#{subdomain}-hyperschema.json"
"https://#{subdomain}.datocms.com/docs/#{subdomain}-hyperschema.json"
)
schema = JsonSchema.parse!(JSON.parse(response.body))
schema.expand_references!
- schema.definitions.each do |type, schema|
- is_collection = schema.links.select { |x| x.rel === 'instances' }.any?
+ schema.definitions.each do |type, obj|
+ is_collection = obj.links.select { |x| x.rel == 'instances' }.any?
namespace = is_collection ? type.pluralize : type
define_method(namespace) do
instance_variable_set(
"@#{namespace}",
instance_variable_get("@#{namespace}") ||
- Dato::Repo.new(self, type, schema)
+ Dato::Repo.new(self, type, obj)
)
end
end
end
end
@@ -74,20 +75,14 @@
def request(*args)
method, absolute_path, body, params = args
response = connection.send(method, absolute_path, body) do |c|
- if params
- c.params = params
- end
+ c.params = params if params
end
- if response.body.is_a?(Hash)
- response.body.with_indifferent_access
- else
- nil
- 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
Cacert.set_in_env
request(*args)
@@ -99,18 +94,18 @@
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
error = ApiError.new(e.response)
- puts "===="
+ puts '===='
puts error.message
- puts "===="
+ puts '===='
raise error
end
end
private
@@ -121,16 +116,16 @@
rescue JSON::ParserError => e
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
+ rescue StandardError
false
end
def connection
default_headers = {
@@ -139,12 +134,16 @@
'Authorization' => "Bearer #{@token}",
'User-Agent' => "ruby-client v#{Dato::VERSION}",
'X-Api-Version' => '3'
}
+ if environment
+ default_headers.merge!('X-Environment' => environment)
+ end
+
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$/