Class: Dato::Site::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dato/site/client.rb

Constant Summary collapse

REPOS =
{
  fields: Repo::Field,
  item_types: Repo::ItemType,
  menu_items: Repo::MenuItem,
  site: Repo::Site,
  upload_requests: Repo::UploadRequest,
  users: Repo::User,
  items: Repo::Item
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, options = {}) ⇒ Client

Returns a new instance of Client.



38
39
40
41
42
# File 'lib/dato/site/client.rb', line 38

def initialize(token, options = {})
  @token = token
  @base_url = options[:base_url] || 'https://site-api.datocms.com'
  @extra_headers = options[:extra_headers] || {}
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



36
37
38
# File 'lib/dato/site/client.rb', line 36

def base_url
  @base_url
end

#extra_headersObject (readonly)

Returns the value of attribute extra_headers.



36
37
38
# File 'lib/dato/site/client.rb', line 36

def extra_headers
  @extra_headers
end

#schemaObject (readonly)

Returns the value of attribute schema.



36
37
38
# File 'lib/dato/site/client.rb', line 36

def schema
  @schema
end

#tokenObject (readonly)

Returns the value of attribute token.



36
37
38
# File 'lib/dato/site/client.rb', line 36

def token
  @token
end

Instance Method Details

#request(*args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dato/site/client.rb', line 64

def request(*args)
  connection.send(*args).body.with_indifferent_access
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
  raise e
rescue Faraday::ClientError => e
  raise ApiError, e
end

#upload_file(path_or_url) ⇒ Object



44
45
46
47
# File 'lib/dato/site/client.rb', line 44

def upload_file(path_or_url)
  file = Upload::File.new(self, path_or_url)
  file.upload
end

#upload_image(path_or_url) ⇒ Object



49
50
51
52
# File 'lib/dato/site/client.rb', line 49

def upload_image(path_or_url)
  file = Upload::Image.new(self, path_or_url)
  file.upload
end