Module: Pixela::Client::PixelMethods

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

Instance Method Summary collapse

Instance Method Details

#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"})

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}", compact_hash(params)).body
  end
end

#decrement_pixel(graph_id:) ⇒ Pixela::Response

Decrement quantity "Pixel" of the day (UTC).

Examples:

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

Raises:

See Also:


136
137
138
139
140
# File 'lib/pixela/client/pixel_methods.rb', line 136

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))

Raises:

See Also:


100
101
102
103
104
# File 'lib/pixela/client/pixel_methods.rb', line 100

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))

Raises:

See Also:


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

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")

Raises:

See Also:


118
119
120
121
122
# File 'lib/pixela/client/pixel_methods.rb', line 118

def increment_pixel(graph_id:)
  with_error_handling do
    connection.put("users/#{username}/graphs/#{graph_id}/increment").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"})

Raises:

See Also:


76
77
78
79
80
81
82
83
84
85
# File 'lib/pixela/client/pixel_methods.rb', line 76

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)}", compact_hash(params)).body
  end
end