lib/tanita/api/client/base.rb in tanita-api-client-0.1.0 vs lib/tanita/api/client/base.rb in tanita-api-client-0.1.1

- old
+ new

@@ -8,10 +8,13 @@ module Scope INNERSCAN = 'innerscan' SPHYGMOMANOMETER = 'sphygmomanometer' PEDOMETER = 'pedometer' SMUG = 'smug' + def self.all + constants.map { |name| const_get(name) } + end end class Error < StandardError end @@ -19,22 +22,16 @@ include HttpHelper DATE_REGISTERD_AT = 0 DATE_MEASURED_AT = 1 - def initialize(access_token:) - @access_token = access_token + def initialize(access_token: nil) + config = Tanita::Api::Client.configuration + @access_token = access_token || config.access_token + raise Error.new("param:'access_token' is required.'") if @access_token.nil? end - def endpoint - raise NotImplementedError - end - - def measurement_tags - raise NotImplementedError - end - def status( date_type: DATE_MEASURED_AT, from: nil, to: nil ) @@ -44,15 +41,22 @@ :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) end + def endpoint + raise NotImplementedError + end + + def measurement_tags + raise NotImplementedError + end + private def time_format(time) time.strftime('%Y%m%d%H%M%S') end @@ -90,10 +94,10 @@ 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 end # sort by date in ascending order - @data = data_dic.values { |dic| -dic[:date] } + @data = data_dic.values.sort_by { |dic| dic[:date] } end end end end end