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



41
42
43
# File 'lib/webflow/client.rb', line 41

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

#collections(site_id) ⇒ Object



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

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

#create_item(collection_id, data) ⇒ Object



83
84
85
# File 'lib/webflow/client.rb', line 83

def create_item(collection_id, data)
  post("/collections/#{collection_id}/items", fields: data)
end

#delete_item(item) ⇒ Object



93
94
95
# File 'lib/webflow/client.rb', line 93

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

#domains(site_id) ⇒ Object



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

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

#infoObject



16
17
18
# File 'lib/webflow/client.rb', line 16

def info
  get("/info")
end

#item(collection_id, item_id) ⇒ Object



78
79
80
81
# File 'lib/webflow/client.rb', line 78

def item(collection_id, item_id)
  json = get("/collections/#{collection_id}/items/#{item_id}")
  json['items'].first
end

#items(collection_id, limit: 100) ⇒ Object



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

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)
    fetched_items += resp['items']
    limit -= resp['count']
    break if limit <= 0 || resp['total'] <= fetched_items.length
  end

  fetched_items
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



58
59
60
# File 'lib/webflow/client.rb', line 58

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



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

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

#site(site_id) ⇒ Object



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

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

#sitesObject



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

def sites
  get("/sites")
end

#update_item(item, data) ⇒ Object



87
88
89
90
91
# File 'lib/webflow/client.rb', line 87

def update_item(item, data)
  # FIXME: (PS) looks like the API does not have partial updates...
  base = item.reject {|key, _| ['_id', 'published-by', 'published-on', 'created-on', 'created-by', 'updated-by', 'updated-on', '_cid'].include?(key) }
  put("/collections/#{item['_cid']}/items/#{item['_id']}", fields: base.merge(data))
end