Module: Pixela::Client::PixelMethods

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

Instance Method Summary collapse

Instance Method Details

#add_pixel(graph_id:, quantity:) ⇒ Pixela::Response

Add quantity to the “Pixel” of the day

Examples:

client.add_pixel(graph_id: "test-graph", quantity: "1")

Parameters:

  • graph_id (String)
  • quantity (String)

Returns:

Raises:

See Also:



182
183
184
185
186
187
188
189
190
# File 'lib/pixela/client/pixel_methods.rb', line 182

def add_pixel(graph_id:, quantity:)
  params = {
    quantity: quantity.to_s,
  }

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

#create_pixel(graph_id:, date: Date.today, quantity:, optional_data: nil) ⇒ Pixela::Response

It records the quantity of the specified date as a “Pixel”.

Examples:

client.create_pixel(graph_id: "test-graph", date: Date.new(2018, 9, 15), quantity: 5, optional_data: {key: "value"})

Parameters:

  • graph_id (String)
  • date (Date, Time) (defaults to: Date.today)
  • quantity (Integer, Float)
  • optional_data (Object) (defaults to: nil)

    Additional information other than quantity

Returns:

Raises:

See Also:



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pixela/client/pixel_methods.rb', line 17

def create_pixel(graph_id:, date: Date.today, quantity:, optional_data: nil)
  params = {
    date:         to_ymd(date),
    quantity:     quantity.to_s,
    optionalData: optional_data&.to_json,
  }

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

#create_pixels(graph_id:, pixels:) ⇒ Pixela::Response

This API is used to register multiple Pixels (quantities for a specific day) at a time.

Examples:

client.create_pixels(graph_id: "test-graph", pixels: [{date: Date.new(2018, 9, 15), quantity: 5, optional_data: {key: "value"}}])

Parameters:

  • graph_id (String)
  • pixels (Array<Hash>)

    key: date, quantity, optional_data

Returns:

Raises:

See Also:



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pixela/client/pixel_methods.rb', line 42

def create_pixels(graph_id:, pixels:)
  pixels = pixels.map do |pixel|
    {
      date:         to_ymd(pixel[:date]),
      quantity:     pixel[:quantity].to_s,
      optionalData: pixel[:optional_data]&.to_json,
    }.compact
  end

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

#decrement_pixel(graph_id:) ⇒ Pixela::Response

Decrement quantity “Pixel” of the day (UTC).

Examples:

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

Parameters:

  • graph_id (String)

Returns:

Raises:

See Also:



163
164
165
166
167
# File 'lib/pixela/client/pixel_methods.rb', line 163

def decrement_pixel(graph_id:)
  with_error_handling do
    connection.put("users/#{username}/graphs/#{graph_id}/decrement").body
  end
end

#delete_pixel(graph_id:, date: Date.today) ⇒ Pixela::Response

Delete the registered “Pixel”.

Examples:

client.delete_pixel(graph_id: "test-graph", date: Date.new(2018, 9, 15))

Parameters:

  • graph_id (String)
  • date (Date, Time) (defaults to: Date.today)

Returns:

Raises:

See Also:



127
128
129
130
131
# File 'lib/pixela/client/pixel_methods.rb', line 127

def delete_pixel(graph_id:, date: Date.today)
  with_error_handling do
    connection.delete("users/#{username}/graphs/#{graph_id}/#{to_ymd(date)}").body
  end
end

#get_pixel(graph_id:, date: Date.today) ⇒ Pixela::Response

Get registered quantity as “Pixel”.

Examples:

client.get_pixel(graph_id: "test-graph", date: Date.new(2018, 9, 15))

Parameters:

  • graph_id (String)
  • date (Date, Time) (defaults to: Date.today)

Returns:

Raises:

See Also:



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

def get_pixel(graph_id:, date: Date.today)
  res =
    with_error_handling do
      connection.get("users/#{username}/graphs/#{graph_id}/#{to_ymd(date)}").body
    end

  if res.key?(:optionalData)
    res[:optional_data] =
      begin
        JSON.parse(res[:optionalData])
      rescue JSON::ParserError
        res[:optionalData]
      end
    res.delete(:optionalData)
  end

  res
end

#increment_pixel(graph_id:) ⇒ Pixela::Response

Increment quantity “Pixel” of the day (UTC).

Examples:

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

Parameters:

  • graph_id (String)

Returns:

Raises:

See Also:



145
146
147
148
149
# File 'lib/pixela/client/pixel_methods.rb', line 145

def increment_pixel(graph_id:)
  with_error_handling do
    connection.put("users/#{username}/graphs/#{graph_id}/increment").body
  end
end

#subtract_pixel(graph_id:, quantity:) ⇒ Pixela::Response

Subtract quantity from the “Pixel” of the day

Examples:

client.subtract_pixel(graph_id: "test-graph", quantity: "1")

Parameters:

  • graph_id (String)
  • quantity (String)

Returns:

Raises:

See Also:



205
206
207
208
209
210
211
212
213
# File 'lib/pixela/client/pixel_methods.rb', line 205

def subtract_pixel(graph_id:, quantity:)
  params = {
    quantity: quantity.to_s,
  }

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

#update_pixel(graph_id:, date: Date.today, quantity:, optional_data: nil) ⇒ Pixela::Response

Update the quantity already registered as a “Pixel”.

Examples:

client.update_pixel(graph_id: "test-graph", date: Date.new(2018, 9, 15), quantity: 7, optional_data: {key: "value"})

Parameters:

  • graph_id (String)
  • date (Date, Time) (defaults to: Date.today)
  • quantity (Integer, Float)
  • optional_data (Object) (defaults to: nil)

    Additional information other than quantity

Returns:

Raises:

See Also:



103
104
105
106
107
108
109
110
111
112
# File 'lib/pixela/client/pixel_methods.rb', line 103

def update_pixel(graph_id:, date: Date.today, quantity:, optional_data: nil)
  params = {
    quantity:     quantity.to_s,
    optionalData: optional_data&.to_json
  }

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