Class: Webflow::Client
- Inherits:
-
Object
- Object
- Webflow::Client
- Defined in:
- lib/webflow/client.rb
Constant Summary collapse
- HOST =
'https://api.webflow.com'
Instance Method Summary collapse
- #collection(collection_id) ⇒ Object
- #collections(site_id) ⇒ Object
- #create_item(collection_id, data, live: false) ⇒ Object
- #delete_item(item) ⇒ Object
- #domains(site_id) ⇒ Object
- #info ⇒ Object
-
#initialize(token = nil) ⇒ Client
constructor
A new instance of Client.
- #item(collection_id, item_id) ⇒ Object
- #items(collection_id, limit: 100) ⇒ Object
- #limit ⇒ Object
-
#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.
- #publish(site_id, domain_names: nil) ⇒ Object
- #rate_limit ⇒ Object
- #remaining ⇒ Object
- #site(site_id) ⇒ Object
- #sites ⇒ Object
- #update_item(item, data, live: false) ⇒ Object
Constructor Details
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 |
#info ⇒ Object
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 |
#limit ⇒ Object
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_limit ⇒ Object
12 13 14 |
# File 'lib/webflow/client.rb', line 12 def rate_limit @rate_limit || {} end |
#remaining ⇒ Object
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 |
#sites ⇒ Object
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 |