Module: Pixela::Client::GraphMethods

Included in:
Pixela::Client
Defined in:
lib/pixela/client/graph_methods.rb

Instance Method Summary collapse

Instance Method Details

#create_graph(graph_id:, name:, unit:, type:, color:, timezone: nil, self_sufficient: nil, is_secret: nil, publish_optional_data: nil) ⇒ Pixela::Response

Create a new pixelation graph definition.

Examples:

client.create_graph(graph_id: "test-graph", name: "graph-name", unit: "commit", type: "int", color: "shibafu", timezone: "Asia/Tokyo", self_sufficient: "increment", is_secret: true, publish_optional_data: true)

Parameters:

  • graph_id (String)
  • name (String)
  • unit (String)
  • type (String)
  • color (String)
  • timezone (String) (defaults to: nil)
  • self_sufficient (String) (defaults to: nil)

    If SVG graph with this field increment or decrement is referenced, Pixel of this graph itself will be incremented or decremented

  • is_secret (Boolean) (defaults to: nil)
  • publish_optional_data (Boolean) (defaults to: nil)

Returns:

Raises:

See Also:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pixela/client/graph_methods.rb', line 22

def create_graph(graph_id:, name:, unit:, type:, color:, timezone: nil, self_sufficient: nil, is_secret: nil, publish_optional_data: nil)
  params = {
    id:                  graph_id,
    name:                name,
    unit:                unit,
    type:                type,
    color:               color,
    timezone:            timezone,
    selfSufficient:      self_sufficient,
    isSecret:            is_secret,
    publishOptionalData: publish_optional_data
  }

  with_error_handling do
    connection.post("users/#{username}/graphs", params.compact).body
  end
end

#delete_graph(graph_id) ⇒ Pixela::Response

Delete the predefined pixelation graph definition.

Examples:

client.delete_graph("test-graph")

Parameters:

  • graph_id (String)

Returns:

Raises:

See Also:



151
152
153
154
155
# File 'lib/pixela/client/graph_methods.rb', line 151

def delete_graph(graph_id)
  with_error_handling do
    connection.delete("users/#{username}/graphs/#{graph_id}").body
  end
end

#get_graph_def(graph_id:) ⇒ Pixela::Response

Get a predefined pixelation graph definition.

Examples:

client.get_graph_def(graph_id: "test-graph")

Parameters:

  • graph_id (String)

Returns:

Raises:

See Also:



262
263
264
265
266
# File 'lib/pixela/client/graph_methods.rb', line 262

def get_graph_def(graph_id:)
  with_error_handling do
    connection.get("users/#{username}/graphs/#{graph_id}/graph-def").body
  end
end

#get_graph_latest(graph_id:) ⇒ Pixela::Response

This API is used to get latest Pixel of the graph which specified by <graphID> .

Examples:

client.get_graph_latest(graph_id: "test-graph")

Parameters:

  • graph_id (String)

Returns:

Raises:

See Also:



280
281
282
283
284
# File 'lib/pixela/client/graph_methods.rb', line 280

def get_graph_latest(graph_id:)
  with_error_handling do
    connection.get("users/#{username}/graphs/#{graph_id}/latest").body
  end
end

#get_graph_stats(graph_id:) ⇒ Pixela::Response

Based on the registered information, get various statistics.

Examples:

client.get_graph_stats(graph_id: "test-graph")

Parameters:

  • graph_id (String)

Returns:

Raises:

See Also:



223
224
225
226
227
# File 'lib/pixela/client/graph_methods.rb', line 223

def get_graph_stats(graph_id:)
  with_error_handling do
    connection.get("users/#{username}/graphs/#{graph_id}/stats").body
  end
end

#get_graphsArray<Hashie::Mash>

Get all predefined pixelation graph definitions.

Examples:

client.get_graphs

Returns:

  • (Array<Hashie::Mash>)

Raises:

See Also:



50
51
52
53
54
# File 'lib/pixela/client/graph_methods.rb', line 50

def get_graphs
  with_error_handling do
    connection.get("users/#{username}/graphs").body.graphs
  end
end

#get_pixel_dates(graph_id:, from: nil, to: nil) ⇒ Array<Date>

Get a Date list of Pixel registered in the graph specified by graphID.

Examples:

client.get_pixel_dates(graph_id: "test-graph", from: Date.new(2018, 1, 1), to: Date.new(2018, 12, 31))

Parameters:

  • graph_id (String)
  • from (Date) (defaults to: nil)

    Specify the start position of the period.

  • to (Date) (defaults to: nil)

    Specify the end position of the period.

Returns:

  • (Array<Date>)

Raises:

See Also:



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/pixela/client/graph_methods.rb', line 171

def get_pixel_dates(graph_id:, from: nil, to: nil)
  params = {
    from: to_ymd(from),
    to:   to_ymd(to),
  }

  res =
    with_error_handling do
      connection.get("users/#{username}/graphs/#{graph_id}/pixels", params.compact).body
    end

  res.pixels.map { |ymd| Date.parse(ymd) }
end

#get_pixels(graph_id:, from: nil, to: nil) ⇒ Array<Hashie::Mash>

Get a Date list of Pixel registered in the graph specified by graphID.

Examples:

client.get_pixels(graph_id: "test-graph", from: Date.new(2018, 1, 1), to: Date.new(2018, 12, 31))

Parameters:

  • graph_id (String)
  • from (Date) (defaults to: nil)

    Specify the start position of the period.

  • to (Date) (defaults to: nil)

    Specify the end position of the period.

Returns:

  • (Array<Hashie::Mash>)

Raises:

See Also:



199
200
201
202
203
204
205
206
207
208
209
# File 'lib/pixela/client/graph_methods.rb', line 199

def get_pixels(graph_id:, from: nil, to: nil)
  params = {
    from:     to_ymd(from),
    to:       to_ymd(to),
    withBody: true,
  }

  with_error_handling do
    connection.get("users/#{username}/graphs/#{graph_id}/pixels", params.compact).body.pixels
  end
end

#graph_url(graph_id:, date: nil, mode: nil, appearance: nil, less_than: nil, greater_than: nil) ⇒ String

Get graph url

Examples:

client.graph_url(graph_id: "test-graph")
client.graph_url(graph_id: "test-graph", date: Date.new(2018, 3, 31), mode: "short")

Parameters:

  • graph_id (String)
  • date (Date, Time) (defaults to: nil)
  • mode (String) (defaults to: nil)

    e.g) short

  • appearance (String) (defaults to: nil)

    e.g) dark

  • less_than (String) (defaults to: nil)
  • greater_than (String) (defaults to: nil)

Returns:

  • (String)

See Also:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pixela/client/graph_methods.rb', line 72

def graph_url(graph_id:, date: nil, mode: nil, appearance: nil, less_than: nil, greater_than: nil)
  url = "#{Pixela::Client::API_ENDPOINT}/users/#{username}/graphs/#{graph_id}"

  params = Faraday::Utils::ParamsHash.new
  params[:date] = to_ymd(date) if date
  params[:mode] = mode if mode
  params[:appearance] = appearance if appearance
  params[:lessThan] = less_than if less_than
  params[:greaterThan] = greater_than if greater_than

  url << "?#{params.to_query}" unless params.empty?

  url
end

#graphs_urlString

Displays graph list by detail in html format.

Examples:

client.graphs_url

Returns:

  • (String)

See Also:



95
96
97
# File 'lib/pixela/client/graph_methods.rb', line 95

def graphs_url
  "https://pixe.la/v1/users/#{username}/graphs.html"
end

#run_stopwatch(graph_id:) ⇒ Pixela::Response Also known as: start_stopwatch, end_stopwatch

This will start and end the measurement of the time.

Examples:

client.run_stopwatch(graph_id: "test-graph")

Parameters:

  • graph_id (String)

Returns:

Raises:

See Also:



241
242
243
244
245
# File 'lib/pixela/client/graph_methods.rb', line 241

def run_stopwatch(graph_id:)
  with_error_handling do
    connection.post("users/#{username}/graphs/#{graph_id}/stopwatch").body
  end
end

#update_graph(graph_id:, name: nil, unit: nil, color: nil, timezone: nil, self_sufficient: nil, purge_cache_urls: nil, is_secret: nil, publish_optional_data: nil) ⇒ Pixela::Response

Update predefined pixelation graph definitions.

Examples:

client.update_graph(graph_id: "test-graph", name: "graph-name", unit: "commit", color: "shibafu", timezone: "Asia/Tokyo", self_sufficient: "increment", purge_cache_urls: ["https://camo.githubusercontent.com/xxx/xxxx"])

Parameters:

  • graph_id (String)
  • name (String) (defaults to: nil)
  • unit (String) (defaults to: nil)
  • color (String) (defaults to: nil)
  • timezone (String) (defaults to: nil)
  • self_sufficient (String) (defaults to: nil)

    If SVG graph with this field increment or decrement is referenced, Pixel of this graph itself will be incremented or decremented

  • purge_cache_urls (String, Array<String>) (defaults to: nil)
  • is_secret (Boolean) (defaults to: nil)
  • publish_optional_data (Boolean) (defaults to: nil)

Returns:

Raises:

See Also:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/pixela/client/graph_methods.rb', line 119

def update_graph(graph_id:, name: nil, unit: nil, color: nil, timezone: nil, self_sufficient: nil, purge_cache_urls: nil, is_secret: nil, publish_optional_data: nil)
  params = {
    name:                name,
    unit:                unit,
    color:               color,
    timezone:            timezone,
    selfSufficient:      self_sufficient,
    isSecret:            is_secret,
    publishOptionalData: publish_optional_data
  }

  if purge_cache_urls
    params[:purgeCacheURLs] = Array(purge_cache_urls)
  end

  with_error_handling do
    connection.put("users/#{username}/graphs/#{graph_id}", params.compact).body
  end
end