lib/tanita/api/client/base.rb in tanita-api-client-0.1.1 vs lib/tanita/api/client/base.rb in tanita-api-client-0.2.0
- old
+ new
@@ -1,7 +1,8 @@
# frozen_string_literal: true
+require 'time'
require 'tanita/api/client/helpers'
module Tanita
module Api
module Client
@@ -16,88 +17,123 @@
end
class Error < StandardError
end
+ DATE_TYPE_REGISTERD_AT = 0
+ DATE_TYPE_MEASURED_AT = 1
+
class BaseApiClient
include HttpHelper
- DATE_REGISTERD_AT = 0
- DATE_MEASURED_AT = 1
-
- def initialize(access_token: nil)
+ def initialize(access_token: nil, date_type: DATE_TYPE_MEASURED_AT)
config = Tanita::Api::Client.configuration
@access_token = access_token || config.access_token
raise Error.new("param:'access_token' is required.'") if @access_token.nil?
+
+ @date_type = date_type
+ raise Error.new("param:'date_type' is invalid.'") unless [DATE_TYPE_REGISTERD_AT, DATE_TYPE_MEASURED_AT].include? date_type
end
def status(
- date_type: DATE_MEASURED_AT,
from: nil,
to: nil
)
- tags = measurement_tags.values.join(',')
+ tags = measurement_tags.values.map { |i| i[:code] }.join(',')
params = {
:access_token => @access_token,
- :date => date_type,
+ :date => @date_type,
:tag => tags
}
params[:from] = time_format(from) unless from.nil?
params[:to] = time_format(to) unless to.nil?
res = request(endpoint, params)
- Result.new(:response => res, :client => self)
+ Result.new(:client => self, :response => res)
end
def endpoint
raise NotImplementedError
end
def measurement_tags
raise NotImplementedError
end
+ def find_measurement_tag(code:)
+ return @inverted_measurement[code] unless @inverted_measurement.nil?
+
+ @inverted_measurement = {}
+ measurement_tags.each do |m_name, m_info|
+ @inverted_measurement[m_info[:code]] = {:name => m_name, :type => m_info[:type]}
+ end
+ @inverted_measurement[code]
+ end
+
+ def date_key
+ case @date_type
+ when DATE_TYPE_REGISTERD_AT
+ :registered_at
+ when DATE_TYPE_MEASURED_AT
+ :measured_at
+ end
+ end
+
private
def time_format(time)
time.strftime('%Y%m%d%H%M%S')
end
end
class Result
include HttpHelper
+
+ # [Date]
attr_reader :birth_date
+
+ # [Float] (centimeter)
attr_reader :height
+
+ # [String] 'male' or 'female'
attr_reader :sex
- attr_reader :data
- def initialize(response:, client:)
+ # [Array<Hash>]
+ attr_reader :items
+
+ def initialize(client:, response:)
+ @client = client
result = parse_json(response.body)
- @birth_date = result[:birth_date]
- @height = result[:height]
+ @birth_date = Date.parse(result[:birth_date])
+ @height = result[:height].to_f
@sex = result[:sex]
-
- mapper = client.measurement_tags.invert
- set_data(result[:data], mapper)
+ @items = build_items(result[:data])
end
private
- def set_data(items, mapper)
- data_dic = {}
- items.each do |item|
+ def build_items(raw_items)
+ item_dic = {}
+ raw_items.each do |item|
date = item[:date]
model = item[:model]
key = "#{date}_#{model}"
- measurement = mapper[item[:tag]]
- value = item[:keydata]
-
- data_dic[key] ||= {}
- data_dic[key][:date] = date unless data_dic[key].key? :date
- data_dic[key][:model] = model unless data_dic[key].key? :model
- data_dic[key][measurement] = value
+ measurement = @client.find_measurement_tag(:code => item[:tag])
+ value = cast(:value => item[:keydata], :type => measurement[:type])
+ item_dic[key] ||= {}
+ item_dic[key][@client.date_key] = Time.parse("#{date} +09:00").to_i unless item_dic[key].key? :date
+ item_dic[key][:model] = model unless item_dic[key].key? :model
+ item_dic[key][measurement[:name]] = value
end
# sort by date in ascending order
- @data = data_dic.values.sort_by { |dic| dic[:date] }
+ item_dic.values.sort_by { |dic| dic[@client.date_key] }
+ end
+
+ def cast(value:, type:)
+ return value if value.nil?
+ return value.to_i if type == Integer
+ return value.to_f if type == Float
+
+ value
end
end
end
end
end