class Webflow::Client

Constants

HOST

Public Class Methods

new(token = nil) click to toggle source
# File lib/webflow/client.rb, line 7
def initialize(token = nil)
  @token = token || Webflow.config.api_token
  @rate_limit = {}
end

Public Instance Methods

collection(collection_id) click to toggle source
# File lib/webflow/client.rb, line 49
def collection(collection_id)
  get("/collections/#{collection_id}")
end
collections(site_id) click to toggle source
# File lib/webflow/client.rb, line 45
def collections(site_id)
  get("/sites/#{site_id}/collections")
end
create_item(collection_id, data, live: false) click to toggle source
# File lib/webflow/client.rb, line 93
def create_item(collection_id, data, live: false)
  post("/collections/#{collection_id}/items", {fields: data}, live: live)
end
delete_item(item) click to toggle source
# File lib/webflow/client.rb, line 101
def delete_item(item)
  delete("/collections/#{item['_cid']}/items/#{item['_id']}")
end
domains(site_id) click to toggle source
# File lib/webflow/client.rb, line 36
def domains(site_id)
  get("/sites/#{site_id}/domains")
end
info() click to toggle source
# File lib/webflow/client.rb, line 24
def info
  get("/info")
end
item(collection_id, item_id) click to toggle source
# File lib/webflow/client.rb, line 88
def item(collection_id, item_id)
  json = get("/collections/#{collection_id}/items/#{item_id}")
  json['items'].first
end
items(collection_id, limit: 100) { |items| ... } click to toggle source
# File lib/webflow/client.rb, line 71
def items(collection_id, limit: 100)
  fetched_items = []
  num_pages     = (limit.to_f / 100.0).ceil
  per_page      = limit > 100 ? 100 : limit

  num_pages.times do |i|
    resp = paginate_items(collection_id, per_page: per_page, page: i+1)
    items = resp['items']
    yield(items) if block_given?
    fetched_items += items
    limit -= resp['count']
    break if limit <= 0 || resp['total'] <= fetched_items.length
  end

  fetched_items
end
limit() click to toggle source
# File lib/webflow/client.rb, line 16
def limit
  rate_limit['X-Ratelimit-Limit'].to_i
end
paginate_items(collection_id, per_page: 100, page: 1) click to toggle source

developers.webflow.com/?javascript#get-all-items-for-a-collection returns json object with data to help paginate collection

{

items:  your list of items returned,
count:  number of items returned,
limit:  the limit specified in the request (default: 100),
offset: the offset specified for pagination (default: 0),
total:  total # of items in the collection

}

page starts at 1

# File lib/webflow/client.rb, line 66
def paginate_items(collection_id, per_page: 100, page: 1)
  get("/collections/#{collection_id}/items", params: { limit: per_page, offset: per_page * (page - 1) })
end
publish(site_id, domain_names: nil) click to toggle source
# File lib/webflow/client.rb, line 40
def publish(site_id, domain_names: nil)
  domain_names ||= domains(site_id).map { |domain| domain['name'] }
  post("/sites/#{site_id}/publish", {domains: domain_names})
end
rate_limit() click to toggle source
# File lib/webflow/client.rb, line 12
def rate_limit
  @rate_limit || {}
end
remaining() click to toggle source
# File lib/webflow/client.rb, line 20
def remaining
  rate_limit['X-Ratelimit-Remaining'].to_i
end
site(site_id) click to toggle source
# File lib/webflow/client.rb, line 32
def site(site_id)
  get("/sites/#{site_id}")
end
sites() click to toggle source
# File lib/webflow/client.rb, line 28
def sites
  get("/sites")
end
update_item(item, data, live: false) click to toggle source
# File lib/webflow/client.rb, line 97
def update_item(item, data, live: false)
  patch("/collections/#{item['_cid']}/items/#{item['_id']}", {fields: data}, live: live)
end

Private Instance Methods

delete(path) click to toggle source
# File lib/webflow/client.rb, line 121
def delete(path)
  request(path, method: :delete)
end
get(path, params: nil) click to toggle source
# File lib/webflow/client.rb, line 107
def get(path, params: nil)
  request(path, method: :get, params: params)
end
patch(path, data, live: nil) click to toggle source
# File lib/webflow/client.rb, line 116
def patch(path, data, live: nil)
  params = { live: 'true' } if live
  request(path, method: :patch, params: params, data: data)
end
post(path, data, live: nil) click to toggle source
# File lib/webflow/client.rb, line 111
def post(path, data, live: nil)
  params = { live: 'true' } if live
  request(path, method: :post, params: params, data: data)
end
request(path, method: :get, params: nil, data: nil) click to toggle source
# File lib/webflow/client.rb, line 125
def request(path, method: :get, params: nil, data: nil)
  url = URI.join(HOST, path)
  bearer = "Bearer #{@token}"
  headers = {'Accept-Version' => '1.0.0'}

  response = HTTP.auth(bearer).headers(headers).request(method, url, params: params, json: data)

  track_rate_limit(response.headers)

  result = JSON.parse(response.body)
  raise Webflow::Error.new(result) if response.code >= 400

  result
end
track_rate_limit(headers) click to toggle source
# File lib/webflow/client.rb, line 140
def track_rate_limit(headers)
  rate_limit = headers.select { |key, value| key =~ /X-Ratelimit/ }.to_h
  @rate_limit = rate_limit unless rate_limit.empty?
end