Class: Webflow::Client

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

Constant Summary collapse

HOST =
'https://api.webflow.com'

Instance Method Summary collapse

Constructor Details

#initialize(token = nil) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/webflow/client.rb', line 7

def initialize(token = nil)
  @token = token || Webflow.config.api_token
  @rate_limit = {}
end

Instance Method Details

#collection(collection_id) ⇒ Object



49
50
51
# File 'lib/webflow/client.rb', line 49

def collection(collection_id)
  get("/collections/#{collection_id}")
end

#collections(site_id) ⇒ Object



45
46
47
# File 'lib/webflow/client.rb', line 45

def collections(site_id)
  get("/sites/#{site_id}/collections")
end

#create_item(collection_id, data, live: false) ⇒ Object



93
94
95
# 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) ⇒ Object



101
102
103
# File 'lib/webflow/client.rb', line 101

def delete_item(item)
  delete("/collections/#{item['_cid']}/items/#{item['_id']}")
end

#domains(site_id) ⇒ Object



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

def domains(site_id)
  get("/sites/#{site_id}/domains")
end

#infoObject



24
25
26
# File 'lib/webflow/client.rb', line 24

def info
  get("/info")
end

#item(collection_id, item_id) ⇒ Object



88
89
90
91
# 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) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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

#limitObject



16
17
18
# 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) ⇒ Object

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



66
67
68
# 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) ⇒ Object



40
41
42
43
# 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_limitObject



12
13
14
# File 'lib/webflow/client.rb', line 12

def rate_limit
  @rate_limit || {}
end

#remainingObject



20
21
22
# File 'lib/webflow/client.rb', line 20

def remaining
  rate_limit['X-Ratelimit-Remaining'].to_i
end

#site(site_id) ⇒ Object



32
33
34
# File 'lib/webflow/client.rb', line 32

def site(site_id)
  get("/sites/#{site_id}")
end

#sitesObject



28
29
30
# File 'lib/webflow/client.rb', line 28

def sites
  get("/sites")
end

#update_item(item, data, live: false) ⇒ Object



97
98
99
# 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