Class: Webflow::Client

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

Constant Summary collapse

HOST =
'https://api.webflow.com/v2'.freeze
PAGINATION_LIMIT =
100

Instance Method Summary collapse

Constructor Details

#initialize(token = Webflow.config.api_token) ⇒ Client

Returns a new instance of Client.



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

def initialize(token = Webflow.config.api_token)
  @token = token
end

Instance Method Details

#collection(collection_id) ⇒ Object



30
31
32
# File 'lib/webflow/client.rb', line 30

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

#collections(site_id) ⇒ Object



26
27
28
# File 'lib/webflow/client.rb', line 26

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

#create_item(collection_id, data, is_draft: false, is_archived: false) ⇒ Object



74
75
76
77
78
# File 'lib/webflow/client.rb', line 74

def create_item(collection_id, data, is_draft: false, is_archived: false)
  result = post("/collections/#{collection_id}/items", { isArchived: is_archived, isDraft: is_draft, fieldData: data })
  publish_item(collection_id, result.fetch(:id))
  result
end

#delete_item(collection_id, item_id) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/webflow/client.rb', line 89

def delete_item(collection_id, item_id)
  # deleting items from Webflow doesn't work as expected.
  # if we delete without archiving, the item will stay visible on the site until the site is published
  # if we first archive + publish item, the item will be set as archived and not visible on the site
  # then we call delete to remove the item from Webflow CMS
  patch("/collections/#{collection_id}/items/#{item_id}", { isArchived: true, isDraft: false })
  publish_item(collection_id, item_id)
  delete("/collections/#{collection_id}/items/#{item_id}")
end

#get_item(collection_id, item_id) ⇒ Object



70
71
72
# File 'lib/webflow/client.rb', line 70

def get_item(collection_id, item_id)
  get("/collections/#{collection_id}/items/#{item_id}")
end

#list_all_items(collection_id) ⇒ Object

rubocop:disable Metrics/MethodLength



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/webflow/client.rb', line 49

def list_all_items(collection_id) # rubocop:disable Metrics/MethodLength
  fetched_items = []
  offset = 0

  loop do
    response = get("/collections/#{collection_id}/items", { limit: PAGINATION_LIMIT, offset: offset })
    items = response.fetch(:items)

    if block_given?
      yield(items)
    else
      fetched_items.concat(items)
    end

    offset += PAGINATION_LIMIT
    break if offset >= response.dig(:pagination, :total)
  end

  fetched_items
end

#list_items(collection_id, limit: PAGINATION_LIMIT, offset: 0) ⇒ Object

developers.webflow.com/reference/list-collection-items

{

items: [] your list of items returned,
pagination: {
  limit:  the limit specified in the request (default: 100),
  offset: the offset specified for pagination (default: 0),
  total:  total # of items in the collection
}

}



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

def list_items(collection_id, limit: PAGINATION_LIMIT, offset: 0)
  limit = [limit, PAGINATION_LIMIT].min
  get("/collections/#{collection_id}/items", { limit: limit, offset: offset }).fetch(:items)
end

#publish(site_id) ⇒ Object



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

def publish(site_id)
  custom_domains = site(site_id).fetch(:customDomains).map { |domain| domain[:id] }
  post("/sites/#{site_id}/publish", { publishToWebflowSubdomain: true, customDomains: custom_domains })
end

#site(site_id) ⇒ Object



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

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

#sitesObject



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

def sites
  get('/sites').fetch(:sites)
end

#update_item(collection_id, item_id, data, is_draft: false, is_archived: false) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/webflow/client.rb', line 80

def update_item(collection_id, item_id, data, is_draft: false, is_archived: false)
  result = patch(
    "/collections/#{collection_id}/items/#{item_id}",
    { isArchived: is_archived, isDraft: is_draft, fieldData: data }.compact
  )
  publish_item(collection_id, item_id)
  result
end