Class: WeInc::Client

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

Overview

weinc — thin client for the WeInc v1 REST API.

Base URL: https://my.we.inc/api/v1 Auth: org API key passed as Authorization: Bearer wk_...

Endpoint shapes were taken from the live OpenAPI spec (https://my.we.inc/api/v1/docs) where available. The preview endpoint is implemented by the API but not yet listed in the OpenAPI document; its shape was verified against the server source on 2026-08-04.

Pure standard library: uses Net::HTTP only.

Examples:

weinc = WeInc::Client.new(api_key: ENV["WEINC_API_KEY"])
result = weinc.list_projects(limit: 10)
puts result["total"]

Constant Summary collapse

DEFAULT_BASE_URL =
"https://my.we.inc/api/v1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: DEFAULT_BASE_URL, transport: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String)

    Org API key from the agency dashboard (starts with wk_).

  • base_url (String) (defaults to: DEFAULT_BASE_URL)

    Override the API base URL.

  • transport (#call, nil) (defaults to: nil)

    Custom transport for tests. Called with (method, uri, headers, body) and must return [Integer status, String body].

Raises:



50
51
52
53
54
55
56
# File 'lib/weinc/client.rb', line 50

def initialize(api_key:, base_url: DEFAULT_BASE_URL, transport: nil)
  raise Error, "api_key must be a non-empty string" unless api_key.is_a?(String) && !api_key.empty?

  @api_key = api_key
  @base_url = base_url.sub(%r{/+\z}, "")
  @transport = transport
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

Instance Method Details

#create_client(data) ⇒ Object

Create a client. POST /clients. Required key: email. Optional: name, company, plan_id.



113
114
115
# File 'lib/weinc/client.rb', line 113

def create_client(data)
  request("POST", "/clients", body: data)
end

#create_project(data) ⇒ Object

Create a project for a client. POST /projects.

Required keys: client_email, name. Optional: description, template_id (clone files from an org template).



80
81
82
# File 'lib/weinc/client.rb', line 80

def create_project(data)
  request("POST", "/projects", body: data)
end

#delete_project(project_id) ⇒ Object

Delete a project. DELETE /projects/{projectId}.



90
91
92
# File 'lib/weinc/client.rb', line 90

def delete_project(project_id)
  request("DELETE", "/projects/#{id!(project_id, 'project_id')}")
end

#get_analytics(days: nil, project_id: nil) ⇒ Object

Get aggregated analytics. GET /analytics.

Parameters:

  • days (Integer, nil) (defaults to: nil)
  • project_id (String, nil) (defaults to: nil)


145
146
147
# File 'lib/weinc/client.rb', line 145

def get_analytics(days: nil, project_id: nil)
  request("GET", "/analytics", query: { days: days, project_id: project_id })
end

#get_client(client_id) ⇒ Object

Get client details. GET /clients/{clientId}.



118
119
120
# File 'lib/weinc/client.rb', line 118

def get_client(client_id)
  request("GET", "/clients/#{id!(client_id, 'client_id')}")
end

#get_preview_urls(project_id) ⇒ Hash

Get preview / published URLs for a project.

GET /projects/{projectId}/preview — implemented by the API but not yet in the OpenAPI spec (shape verified against server source 2026-08-04).

Returns:

  • (Hash)

    {"published_url" => String|nil, "has_published" => Boolean, "embed_preview_path" => String}



100
101
102
# File 'lib/weinc/client.rb', line 100

def get_preview_urls(project_id)
  request("GET", "/projects/#{id!(project_id, 'project_id')}/preview")
end

#get_project(project_id) ⇒ Object

Get one project by ID. GET /projects/{projectId}.



72
73
74
# File 'lib/weinc/client.rb', line 72

def get_project(project_id)
  request("GET", "/projects/#{id!(project_id, 'project_id')}")
end

#list_clientsObject

List clients. GET /clients.



107
108
109
# File 'lib/weinc/client.rb', line 107

def list_clients
  request("GET", "/clients")
end

#list_plansObject

List client plans. GET /plans.



135
136
137
# File 'lib/weinc/client.rb', line 135

def list_plans
  request("GET", "/plans")
end

#list_projects(limit: nil, offset: nil, client_id: nil, status: nil) ⇒ Hash

List projects in your org. GET /projects.

Parameters:

  • limit (Integer, nil) (defaults to: nil)

    Max results (server default 50).

  • offset (Integer, nil) (defaults to: nil)

    Pagination offset.

  • client_id (String, nil) (defaults to: nil)

    Filter by client UUID.

  • status (String, nil) (defaults to: nil)

    Filter by project status.

Returns:

  • (Hash)

    {"projects" => [...], "total" => Integer}



67
68
69
# File 'lib/weinc/client.rb', line 67

def list_projects(limit: nil, offset: nil, client_id: nil, status: nil)
  request("GET", "/projects", query: { limit: limit, offset: offset, client_id: client_id, status: status })
end

#list_templatesObject

List org templates. GET /templates.



130
131
132
# File 'lib/weinc/client.rb', line 130

def list_templates
  request("GET", "/templates")
end

#request(method, path, query: {}, body: nil) ⇒ Object

Perform an authenticated JSON request. Exposed for calling endpoints this client does not wrap yet.

Parameters:

  • method (String)

    HTTP method.

  • path (String)

    Path relative to the base URL, e.g. "/projects".

  • query (Hash) (defaults to: {})

    Query parameters (nil values are dropped).

  • body (Hash, nil) (defaults to: nil)

    JSON request body.

Returns:

  • (Object)

    Parsed JSON response body.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/weinc/client.rb', line 159

def request(method, path, query: {}, body: nil)
  uri = URI.parse(@base_url + path)
  pairs = query.reject { |_k, v| v.nil? }
  uri.query = URI.encode_www_form(pairs) unless pairs.empty?

  headers = {
    "Authorization" => "Bearer #{@api_key}",
    "Accept" => "application/json"
  }
  payload = nil
  unless body.nil?
    headers["Content-Type"] = "application/json"
    payload = JSON.generate(body)
  end

  status, text = send_request(method, uri, headers, payload)

  data =
    if text.nil? || text.empty?
      nil
    else
      begin
        JSON.parse(text)
      rescue JSON::ParserError
        text
      end
    end

  unless (200..299).cover?(status)
    server_message = data.is_a?(Hash) && data["error"].is_a?(String) ? data["error"] : "HTTP #{status}"
    raise Error.new("#{method} #{path} failed: #{server_message}", status: status, body: data)
  end

  data
end

#update_client(client_id, data) ⇒ Object

Update a client. PATCH /clients/{clientId}.



123
124
125
# File 'lib/weinc/client.rb', line 123

def update_client(client_id, data)
  request("PATCH", "/clients/#{id!(client_id, 'client_id')}", body: data)
end

#update_project(project_id, data) ⇒ Object

Update a project. PATCH /projects/{projectId}.



85
86
87
# File 'lib/weinc/client.rb', line 85

def update_project(project_id, data)
  request("PATCH", "/projects/#{id!(project_id, 'project_id')}", body: data)
end