Class: WeInc::Client
- Inherits:
-
Object
- Object
- WeInc::Client
- 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.
Constant Summary collapse
- DEFAULT_BASE_URL =
"https://my.we.inc/api/v1"
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Instance Method Summary collapse
-
#create_client(data) ⇒ Object
Create a client.
-
#create_project(data) ⇒ Object
Create a project for a client.
-
#delete_project(project_id) ⇒ Object
Delete a project.
-
#get_analytics(days: nil, project_id: nil) ⇒ Object
Get aggregated analytics.
-
#get_client(client_id) ⇒ Object
Get client details.
-
#get_preview_urls(project_id) ⇒ Hash
Get preview / published URLs for a project.
-
#get_project(project_id) ⇒ Object
Get one project by ID.
-
#initialize(api_key:, base_url: DEFAULT_BASE_URL, transport: nil) ⇒ Client
constructor
A new instance of Client.
-
#list_clients ⇒ Object
List clients.
-
#list_plans ⇒ Object
List client plans.
-
#list_projects(limit: nil, offset: nil, client_id: nil, status: nil) ⇒ Hash
List projects in your org.
-
#list_templates ⇒ Object
List org templates.
-
#request(method, path, query: {}, body: nil) ⇒ Object
Perform an authenticated JSON request.
-
#update_client(client_id, data) ⇒ Object
Update a client.
-
#update_project(project_id, data) ⇒ Object
Update a project.
Constructor Details
#initialize(api_key:, base_url: DEFAULT_BASE_URL, transport: nil) ⇒ Client
Returns a new instance of Client.
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_url ⇒ Object (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.
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).
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_clients ⇒ Object
List clients. GET /clients.
107 108 109 |
# File 'lib/weinc/client.rb', line 107 def list_clients request("GET", "/clients") end |
#list_plans ⇒ Object
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.
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_templates ⇒ Object
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.
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) = data.is_a?(Hash) && data["error"].is_a?(String) ? data["error"] : "HTTP #{status}" raise Error.new("#{method} #{path} failed: #{}", 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 |