Sha256: 2528809f54cc19e16ce7558494f1726f35bd414673ad8bf8d6d1d5a6541f650c

Contents?: true

Size: 871 Bytes

Versions: 4

Compression:

Stored size: 871 Bytes

Contents

class ThingSpeakReporter
  include UseCasePattern

  def initialize(api_key:, params: {})
    @api_key = api_key
    @params = params
  end

  def perform
    post_data
    check_response
  end

  private

  attr_reader :params, :api_key, :response

  def post_data
    uri = URI("https://api.thingspeak.com/update.json")
    @response = Net::HTTP.post_form(uri, request_params)
  end

  def check_response
    if response.code != '200'
      errors.add(:base, "Non 200 response #{response.code}: #{response.body}")
    end
  end

  def request_params
    params.merge(api_key: api_key)
  end
end

# example usage

reporter = ThingSpeakReporter.perform(api_key: 'XZY123', params: {indoor_temperature: 22, outdoor_temperature: 15})

if reporter.success?
  puts "Temperature saved"
else
  puts "Oh dear, there is a problem: #{reporter.errors.full_messages.join('; ')}"
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
use_case_pattern-1.0.4 examples/thing_speak_reporter.rb
use_case_pattern-1.0.3 examples/thing_speak_reporter.rb
use_case_pattern-1.0.2 examples/thing_speak_reporter.rb
use_case_pattern-1.0.1 examples/thing_speak_reporter.rb