diff --git a/.rubocop.yml b/.rubocop.yml index 20c874e..cf00839 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -23,12 +23,11 @@ Metrics/BlockNesting: Max: 2 Layout/LineLength: - AllowURI: true - Enabled: false + Max: 120 Metrics/MethodLength: CountComments: false - Max: 15 + Max: 20 Metrics/ParameterLists: Max: 4 @@ -39,10 +38,10 @@ Metrics/AbcSize: Style/CollectionMethods: PreferredMethods: - map: 'collect' - reduce: 'inject' - find: 'detect' - find_all: 'select' + map: "collect" + reduce: "inject" + find: "detect" + find_all: "select" Style/Documentation: Enabled: false @@ -60,10 +59,13 @@ Style/ExpandPathArguments: Enabled: false Style/HashSyntax: - EnforcedStyle: hash_rockets + EnforcedStyle: ruby19 Style/Lambda: Enabled: false Style/RaiseArgs: - EnforcedStyle: compact \ No newline at end of file + EnforcedStyle: compact + +Style/AsciiComments: + Enabled: false diff --git a/CHANGELOG.md b/CHANGELOG.md index 98a703a..b602684 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,17 +1,23 @@ -# 0.5.0 +# CHANGELOG + +## 0.5.1 + +- fix rubocop warnings. + +## 0.5.0 - refs #12 fix dependencies. -# 0.4.1 +## 0.4.1 - refs #4 fix settings for simplecov. -# 0.4.0 +## 0.4.0 - refs #4 measure code coverage - refs #5 setup GitHub Actions for rspec and pushing to RubyGems -# 0.3.0 +## 0.3.0 changed Result hash to be Object. @@ -22,11 +28,11 @@ result.items[0][:weight] result.items[0].weight ``` -# 0.2.3 +## 0.2.3 remove Gemfile.lock -# 0.2.2 +## 0.2.2 - define constants - `Tanita::Api::Client::AUTH_URL` @@ -34,12 +40,12 @@ remove Gemfile.lock - `Tanita::Api::Client::TOKEN_URL` - `Tanita::Api::Client::TOKEN_URL_PATH` -# 0.2.1 +## 0.2.1 - rename constant - from `Tanita::Api::Client::HttpHelper::BASE_URL` to `Tanita::Api::Client::BASE_URL` -# 0.2.0 +## 0.2.0 - set required ruby version to greater than or equal to v2.4 - added `data_type` argument in BaseApiClient#initialize @@ -49,11 +55,11 @@ remove Gemfile.lock - `@data` to `@items` - `@data[0][:date]` to `@items[0][:measured_at]` or `@items[0][:registered_at]` -# 0.1.1 +## 0.1.1 - support Tanita::Api::Client.configure - wrote spec -# 0.1.0 +## 0.1.0 - Initial release diff --git a/Rakefile b/Rakefile index b7e9ed5..82bb534 100644 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,8 @@ -require "bundler/gem_tasks" -require "rspec/core/rake_task" +# frozen_string_literal: true + +require 'bundler/gem_tasks' +require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) -task :default => :spec +task default: :spec diff --git a/bin/console b/bin/console index 70e2ff3..c496808 100755 --- a/bin/console +++ b/bin/console @@ -1,6 +1,7 @@ #!/usr/bin/env ruby +# frozen_string_literal: true -require "bundler/setup" -require "tanita/api/client" -require "irb" +require 'bundler/setup' +require 'tanita/api/client' +require 'irb' IRB.start(__FILE__) diff --git a/lib/tanita/api/client.rb b/lib/tanita/api/client.rb index 8e9092e..940d59b 100644 --- a/lib/tanita/api/client.rb +++ b/lib/tanita/api/client.rb @@ -35,21 +35,21 @@ def initialize(client_id: nil, client_secret: nil, redirect_uri: nil, scopes: ni def auth_uri params = { - :client_id => @client_id, - :redirect_uri => @redirect_uri, - :scope => @scopes.join(','), - :response_type => 'code' + client_id: @client_id, + redirect_uri: @redirect_uri, + scope: @scopes.join(','), + response_type: 'code' } generate_uri(AUTH_URL_PATH, params) end def exchange_token(auth_code:) params = { - :client_id => @client_id, - :client_secret => @client_secret, - :redirect_uri => DEFAULT_REDIRECT_URI, - :code => auth_code, - :grant_type => 'authorization_code' + client_id: @client_id, + client_secret: @client_secret, + redirect_uri: DEFAULT_REDIRECT_URI, + code: auth_code, + grant_type: 'authorization_code' } res = request(TOKEN_URL_PATH, params) token = parse_json(res.body) @@ -76,14 +76,14 @@ def self.endpoint def self.properties { - :weight => {:code => '6021', :type => Float}, - :body_fat => {:code => '6022', :type => Float}, - :muscle_mass => {:code => '6023', :type => Float}, - :physique_rating => {:code => '6024', :type => Integer}, - :visceral_fat_rating => {:code => '6025', :type => Float}, - :basal_metabolic_rate => {:code => '6027', :type => Integer}, - :metabolic_age => {:code => '6028', :type => Integer}, - :bone_mass => {:code => '6029', :type => Float} + weight: {code: '6021', type: Float}, + body_fat: {code: '6022', type: Float}, + muscle_mass: {code: '6023', type: Float}, + physique_rating: {code: '6024', type: Integer}, + visceral_fat_rating: {code: '6025', type: Float}, + basal_metabolic_rate: {code: '6027', type: Integer}, + metabolic_age: {code: '6028', type: Integer}, + bone_mass: {code: '6029', type: Float} } end end @@ -95,9 +95,9 @@ def self.endpoint def self.properties { - :maximal_pressure => {:code => '622E', :type => Integer}, - :minimal_pressure => {:code => '622F', :type => Integer}, - :pulse => {:code => '6230', :type => Integer} + maximal_pressure: {code: '622E', type: Integer}, + minimal_pressure: {code: '622F', type: Integer}, + pulse: {code: '6230', type: Integer} } end end @@ -109,9 +109,9 @@ def self.endpoint def self.properties { - :steps => {:code => '6331', :type => Integer}, - :exercise => {:code => '6335', :type => Integer}, - :calories => {:code => '6336', :type => Integer} + steps: {code: '6331', type: Integer}, + exercise: {code: '6335', type: Integer}, + calories: {code: '6336', type: Integer} } end end @@ -123,7 +123,7 @@ def self.endpoint def self.properties { - :urinary_sugar => {:code => '6240', :type => Integer} + urinary_sugar: {code: '6240', type: Integer} } end end diff --git a/lib/tanita/api/client/base.rb b/lib/tanita/api/client/base.rb index 3b7aee7..396880e 100644 --- a/lib/tanita/api/client/base.rb +++ b/lib/tanita/api/client/base.rb @@ -3,7 +3,6 @@ module Tanita module Api module Client - module Scope INNERSCAN = 'innerscan' SPHYGMOMANOMETER = 'sphygmomanometer' @@ -16,7 +15,6 @@ def self.all class Error < StandardError end - end end end diff --git a/lib/tanita/api/client/base_api_client.rb b/lib/tanita/api/client/base_api_client.rb index 5a18093..4ddb892 100644 --- a/lib/tanita/api/client/base_api_client.rb +++ b/lib/tanita/api/client/base_api_client.rb @@ -26,7 +26,9 @@ def initialize(access_token: nil, date_type: DATE_TYPE_MEASURED_AT) 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 + unless [DATE_TYPE_REGISTERD_AT, DATE_TYPE_MEASURED_AT].include? date_type + raise Error.new("param:'date_type' is invalid.'") + end ClassBuilder.load end @@ -37,9 +39,9 @@ def status( ) tags = self.class.properties.values.map { |i| i[:code] }.join(',') params = { - :access_token => @access_token, - :date => @date_type, - :tag => tags + access_token: @access_token, + date: @date_type, + tag: tags } params[:from] = time_format(from) unless from.nil? params[:to] = time_format(to) unless to.nil? @@ -56,10 +58,10 @@ def inspect def build_result(res) result = parse_json(res.body) Result.new( - :birth_date => Date.parse(result[:birth_date]), - :height => result[:height].to_f, - :sex => result[:sex], - :items => build_result_items(:raw_items => result[:data]) + birth_date: Date.parse(result[:birth_date]), + height: result[:height].to_f, + sex: result[:sex], + items: build_result_items(raw_items: result[:data]) ) end @@ -69,8 +71,8 @@ def build_result_items(raw_items:) date = item[:date] model = item[:model] key = "#{date}_#{model}" - property = find_property_by_code(:code => item[:tag]) - value = cast(:value => item[:keydata], :type => property[:type]) + property = find_property_by_code(code: item[:tag]) + value = cast(value: item[:keydata], type: property[:type]) item_dic[key] ||= {} item_dic[key][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 @@ -93,7 +95,7 @@ def find_property_by_code(code:) @property_code_dic = {} self.class.properties.each do |m_name, m_info| - @property_code_dic[m_info[:code]] = {:name => m_name, :type => m_info[:type]} + @property_code_dic[m_info[:code]] = {name: m_name, type: m_info[:type]} end @property_code_dic[code] end diff --git a/lib/tanita/api/client/base_entity.rb b/lib/tanita/api/client/base_entity.rb index f00aefd..d865e48 100644 --- a/lib/tanita/api/client/base_entity.rb +++ b/lib/tanita/api/client/base_entity.rb @@ -12,7 +12,7 @@ def initialize(property_values = {}) def to_h ret = {} self.class.properties.each do |property| - ret[property.to_sym] = eval property.to_s + ret[property.to_sym] = eval property.to_s # rubocop:disable Security/Eval end ret end diff --git a/lib/tanita/api/client/configuration.rb b/lib/tanita/api/client/configuration.rb index 9e0d10f..5af2032 100644 --- a/lib/tanita/api/client/configuration.rb +++ b/lib/tanita/api/client/configuration.rb @@ -4,19 +4,15 @@ module Tanita module Api module Client class Configuration - # [String] + # @return [String] attr_accessor :client_id - - # [String] + # @return [String] attr_accessor :client_secret - - # [String] + # @return [String] attr_accessor :redirect_uri - - # [String] + # @return [String] attr_accessor :access_token - - # [Array] + # @return [Array] attr_accessor :scopes end end diff --git a/lib/tanita/api/client/helpers.rb b/lib/tanita/api/client/helpers.rb index 3b39dee..ad6bd67 100644 --- a/lib/tanita/api/client/helpers.rb +++ b/lib/tanita/api/client/helpers.rb @@ -33,7 +33,7 @@ def request(path, params) end def parse_json(str) - JSON.parse(str, :symbolize_names => true) + JSON.parse(str, symbolize_names: true) rescue JSON::ParserError => e raise Error.new("JSON::ParseError: '#{e}'\nstr:#{str}") end diff --git a/lib/tanita/api/client/version.rb b/lib/tanita/api/client/version.rb index 6932b7a..c978375 100644 --- a/lib/tanita/api/client/version.rb +++ b/lib/tanita/api/client/version.rb @@ -3,7 +3,7 @@ module Tanita module Api module Client - VERSION = '0.5.0' + VERSION = '0.5.1' end end end diff --git a/spec/tanita/api/client_spec.rb b/spec/tanita/api/client_spec.rb index fc38cc2..186b250 100644 --- a/spec/tanita/api/client_spec.rb +++ b/spec/tanita/api/client_spec.rb @@ -41,16 +41,16 @@ it 'raise Error when exchange access token by invalid auth code' do auth_helper = Client::Auth.new body = read_fixture('exchange_token', 'invalid.json') - WebMock.stub_request(:post, Client::TOKEN_URL.to_s).to_return(:body => body) - expect { auth_helper.exchange_token(:auth_code => 'invalid_code') }.to raise_error(Client::Error) + WebMock.stub_request(:post, Client::TOKEN_URL.to_s).to_return(body: body) + expect { auth_helper.exchange_token(auth_code: 'invalid_code') }.to raise_error(Client::Error) end it 'exchange access token by valid auth code' do auth_helper = Client::Auth.new body = read_fixture('exchange_token', 'valid.json') - WebMock.stub_request(:post, Client::TOKEN_URL.to_s).to_return(:body => body) - expected_token = {:access_token => 'hoge_access_token', :expires_in => 12_345_678, :refresh_token => 'hoge_refresh_token'} - expect(auth_helper.exchange_token(:auth_code => 'valid_code')).to eq expected_token + WebMock.stub_request(:post, Client::TOKEN_URL.to_s).to_return(body: body) + expected_token = {access_token: 'hoge_access_token', expires_in: 12_345_678, refresh_token: 'hoge_refresh_token'} + expect(auth_helper.exchange_token(auth_code: 'valid_code')).to eq expected_token end end @@ -60,7 +60,7 @@ end it 'raise Error when initialize Service class for invalid parameters' do - expect { Client::Innerscan.new(:date_type => 'unknown') }.to raise_error(Client::Error) + expect { Client::Innerscan.new(date_type: 'unknown') }.to raise_error(Client::Error) end it 'set configuration for access_token' do @@ -78,7 +78,7 @@ it 'raise Error invalid token' do innerscan = Client::Innerscan.new body = read_fixture('services', 'invalid_token.html') - WebMock.stub_request(:post, "#{Client::BASE_URL}/status/innerscan.json").to_return(:body => body) + WebMock.stub_request(:post, "#{Client::BASE_URL}/status/innerscan.json").to_return(body: body) expect { innerscan.status }.to raise_error(Client::Error) end @@ -115,7 +115,7 @@ it 'fetch Innerscan data' do api = Client::Innerscan.new body = read_fixture('services', 'innerscan_valid.json') - WebMock.stub_request(:post, "#{Client::BASE_URL}#{api.class.endpoint}").to_return(:body => body) + WebMock.stub_request(:post, "#{Client::BASE_URL}#{api.class.endpoint}").to_return(body: body) result = api.status expect(result.birth_date).to eq Date.parse('20200101') @@ -123,30 +123,30 @@ expect(result.sex).to eq 'male' expected_items = [ { - :registered_at => nil, - :measured_at => to_unixtime('201912050838'), - :model => '01000144', - :weight => 77.10, - :body_fat => 21.30, - :muscle_mass => 57.50, - :physique_rating => 2, - :visceral_fat_rating => 10.5, - :basal_metabolic_rate => 1721, - :metabolic_age => 32, - :bone_mass => 3.10 + registered_at: nil, + measured_at: to_unixtime('201912050838'), + model: '01000144', + weight: 77.10, + body_fat: 21.30, + muscle_mass: 57.50, + physique_rating: 2, + visceral_fat_rating: 10.5, + basal_metabolic_rate: 1721, + metabolic_age: 32, + bone_mass: 3.10 }, { - :registered_at => nil, - :measured_at => to_unixtime('201912070806'), - :model => '01000144', - :weight => 76.70, - :body_fat => 22.80, - :muscle_mass => 56.20, - :physique_rating => 2, - :visceral_fat_rating => 11.0, - :basal_metabolic_rate => 1680, - :metabolic_age => 34, - :bone_mass => 3.10 + registered_at: nil, + measured_at: to_unixtime('201912070806'), + model: '01000144', + weight: 76.70, + body_fat: 22.80, + muscle_mass: 56.20, + physique_rating: 2, + visceral_fat_rating: 11.0, + basal_metabolic_rate: 1680, + metabolic_age: 34, + bone_mass: 3.10 } ] expect(result.items.map(&:to_h)).to eq expected_items @@ -154,9 +154,9 @@ end it 'fetch Sphygmomanometer data' do - api = Client::Sphygmomanometer.new(:date_type => Client::DATE_TYPE_REGISTERD_AT) + api = Client::Sphygmomanometer.new(date_type: Client::DATE_TYPE_REGISTERD_AT) body = read_fixture('services', 'sphygmomanometer_valid.json') - WebMock.stub_request(:post, "#{Client::BASE_URL}#{api.class.endpoint}").to_return(:body => body) + WebMock.stub_request(:post, "#{Client::BASE_URL}#{api.class.endpoint}").to_return(body: body) result = api.status expect(result.birth_date).to eq Date.parse('20200101') @@ -164,20 +164,20 @@ expect(result.sex).to eq 'female' expected_items = [ { - :registered_at => to_unixtime('202001070115'), - :measured_at => nil, - :model => '00000000', - :maximal_pressure => 180, - :minimal_pressure => 70, - :pulse => 20 + registered_at: to_unixtime('202001070115'), + measured_at: nil, + model: '00000000', + maximal_pressure: 180, + minimal_pressure: 70, + pulse: 20 }, { - :registered_at => to_unixtime('202001070130'), - :measured_at => nil, - :model => '00000000', - :maximal_pressure => 130, - :minimal_pressure => 80, - :pulse => 50 + registered_at: to_unixtime('202001070130'), + measured_at: nil, + model: '00000000', + maximal_pressure: 130, + minimal_pressure: 80, + pulse: 50 } ] expect(result.items.map(&:to_h)).to eq expected_items @@ -187,7 +187,7 @@ it 'fetch Pedometer items' do api = Client::Pedometer.new body = read_fixture('services', 'pedometer_valid.json') - WebMock.stub_request(:post, "#{Client::BASE_URL}#{api.class.endpoint}").to_return(:body => body) + WebMock.stub_request(:post, "#{Client::BASE_URL}#{api.class.endpoint}").to_return(body: body) result = api.status expect(result.birth_date).to eq Date.parse('20200101') @@ -195,20 +195,20 @@ expect(result.sex).to eq 'male' expected_items = [ { - :registered_at => nil, - :measured_at => to_unixtime('202001070000'), - :model => '00000000', - :steps => 1000, - :exercise => nil, - :calories => 5500 + registered_at: nil, + measured_at: to_unixtime('202001070000'), + model: '00000000', + steps: 1000, + exercise: nil, + calories: 5500 }, { - :registered_at => nil, - :measured_at => to_unixtime('202001080000'), - :model => '00000000', - :steps => 2222, - :exercise => nil, - :calories => 6660 + registered_at: nil, + measured_at: to_unixtime('202001080000'), + model: '00000000', + steps: 2222, + exercise: nil, + calories: 6660 } ] expect(result.items.map(&:to_h)).to eq expected_items @@ -218,7 +218,7 @@ it 'fetch Smug data' do api = Client::Smug.new body = read_fixture('services', 'smug_valid.json') - WebMock.stub_request(:post, "#{Client::BASE_URL}#{api.class.endpoint}").to_return(:body => body) + WebMock.stub_request(:post, "#{Client::BASE_URL}#{api.class.endpoint}").to_return(body: body) result = api.status expect(result.birth_date).to eq Date.parse('20200101') @@ -226,16 +226,16 @@ expect(result.sex).to eq 'male' expected_items = [ { - :registered_at => nil, - :measured_at => to_unixtime('202001070120'), - :model => '00000000', - :urinary_sugar => 500 + registered_at: nil, + measured_at: to_unixtime('202001070120'), + model: '00000000', + urinary_sugar: 500 }, { - :registered_at => nil, - :measured_at => to_unixtime('202001070125'), - :model => '00000000', - :urinary_sugar => 550 + registered_at: nil, + measured_at: to_unixtime('202001070125'), + model: '00000000', + urinary_sugar: 550 } ] expect(result.items.map(&:to_h)).to eq expected_items