Skip to content

Commit

Permalink
Port the Usage class
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Nov 7, 2024
1 parent f1dd07c commit 0d5392e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
16 changes: 2 additions & 14 deletions src/usage.ts → lib/usage.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,14 @@ export class Usage {
* Creates a new usage.
* @param options An object providing values to initialize this instance.
*/
constructor(options: UsageOptions = {}) {
this.limit = options.limit ?? -1;
this.percentage = options.percentage ?? 0;
this.throttled = options.throttled ?? false;
this.usage = options.usage ?? 0;
}
constructor(options?: UsageOptions);

/**
* Creates a new usage from the specified JSON object.
* @param json A JSON object representing a usage.
* @returns The instance corresponding to the specified JSON object.
*/
static fromJson(json: Record<string, any>): Usage {
return new this({
limit: Number.isInteger(json.limit) ? json.limit as number : -1,
percentage: typeof json.percentage == "number" ? json.percentage : 0,
throttled: typeof json.throttled == "boolean" ? json.throttled : false,
usage: Number.isInteger(json.usage) ? json.usage as number : 0
});
}
static fromJson(json: Record<string, any>): Usage;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/usage.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Provides API usage for a given month.
export class Usage

# Creates a new usage.
constructor: (options = {}) ->

# The number of monthly API calls your plan entitles you to.
@limit = options.limit ? (-1)

# The percentage of the limit used since the beginning of the month.
@percentage = options.percentage ? 0

# Value indicating whether the requests are being throttled for having consistently gone over the limit.
@throttled = options.throttled ? no

# The number of calls (spam + ham) since the beginning of the month.
@usage = options.usage ? 0

# Creates a new usage from the specified JSON object.
@fromJson: (json) -> new @
limit: if Number.isInteger(json.limit) then json.limit else -1
percentage: if typeof json.percentage == "number" then json.percentage else 0
throttled: if typeof json.throttled == "boolean" then json.throttled else no
usage: if Number.isInteger(json.usage) then json.usage else 0

0 comments on commit 0d5392e

Please sign in to comment.