Module: Dato::ApiClient

Included in:
Dato::Account::Client, Site::Client
Defined in:
lib/dato/api_client.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dato/api_client.rb', line 50

def method_missing(method, *args, &block)
  json_schema.definitions.each do |type, obj|
    is_collection = obj.links.select { |x| x.rel == "instances" }.any?
    namespace = is_collection ? type.pluralize : type

    next unless method.to_s == namespace

    instance_variable_set(
      "@#{namespace}",
      instance_variable_get("@#{namespace}") ||
      Dato::Repo.new(self, type, obj),
    )

    return instance_variable_get("@#{namespace}")
  end

  super
end

Class Method Details

.included(base) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/dato/api_client.rb', line 17

def self.included(base)
  base.extend ClassMethods

  base.class_eval do
    attr_reader :token, :environment, :base_url, :schema, :extra_headers
  end
end

Instance Method Details

#delete(absolute_path, params = {}) ⇒ Object



95
96
97
# File 'lib/dato/api_client.rb', line 95

def delete(absolute_path, params = {})
  request(:delete, absolute_path, nil, params)
end

#get(absolute_path, params = {}) ⇒ Object



91
92
93
# File 'lib/dato/api_client.rb', line 91

def get(absolute_path, params = {})
  request(:get, absolute_path, nil, params)
end

#json_schemaObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/dato/api_client.rb', line 69

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",
    )

    schema = JsonSchema.parse!(JSON.parse(response.body))
    schema.expand_references!

    schema
  end
end

#post(absolute_path, body = {}, params = {}) ⇒ Object



87
88
89
# File 'lib/dato/api_client.rb', line 87

def post(absolute_path, body = {}, params = {})
  request(:post, absolute_path, body, params)
end

#put(absolute_path, body = {}, params = {}) ⇒ Object



83
84
85
# File 'lib/dato/api_client.rb', line 83

def put(absolute_path, body = {}, params = {})
  request(:put, absolute_path, body, params)
end

#request(*args) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/dato/api_client.rb', line 99

def request(*args)
  method, absolute_path, body, params = args

  response = connection.send(method, absolute_path, body) do |c|
    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

  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
    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..."
    sleep(1)
    request(*args)
  else
    # puts body.inspect
    # puts '===='
    # puts error.message
    # puts '===='
    error = ApiError.new(e.response)
    raise error
  end
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
# File 'lib/dato/api_client.rb', line 40

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?
    namespace = is_collection ? type.pluralize : type
    return true if method.to_s == namespace
  end

  super
end