module Freee::Pigmon

Constants

CALLBACK_URL
OAUTH2_SITE
OAUTH2_URL
VERSION

Public Class Methods

config() click to toggle source
# File lib/freee/pigmon/helper/config_helper.rb, line 45
def self.config
  yml_path = get_config_path
  yml_file = YAML.load_file(yml_path)
  if yml_file
    @config = yml_file
  else
    File.open(yml_path, 'w') { |f| YAML.dump(@config, f) }
  end
end
config_dir_path() click to toggle source
# File lib/freee/pigmon/helper/config_helper.rb, line 37
def self.config_dir_path
  Dir.home + '/.freee-pigmon'
end
config_path() click to toggle source
# File lib/freee/pigmon/helper/config_helper.rb, line 41
def self.config_path
  config_dir_path + '/settings.yml'
end
configure(opts = {}) click to toggle source
# File lib/freee/pigmon/helper/config_helper.rb, line 19
def self.configure(opts = {})
  config
  opts.each do |k, v|
    @config[k.to_sym] = v if @valid_config_keys.include? k.to_sym
  end
  save_config
end
fetch_token_from_code(client_id, client_secret, authrorize_code) click to toggle source
# File lib/freee/pigmon/helper/oauth2_helper.rb, line 14
def fetch_token_from_code(client_id, client_secret, authrorize_code)
  client = oauth2_client(client_id, client_secret)
  response = client.auth_code.get_token(authrorize_code,
                                        redirect_uri: CALLBACK_URL)
  token_info_hash(response)
end
generate_config() click to toggle source
# File lib/freee/pigmon/helper/config_helper.rb, line 32
def self.generate_config
  Dir.mkdir(config_dir_path) unless Dir.exist? config_dir_path
  FileUtils.touch(config_path) unless File.exist? config_path
end
get_config_path() click to toggle source
# File lib/freee/pigmon/helper/config_helper.rb, line 27
def self.get_config_path
  generate_config
  config_path
end
oauth2_client(client_id, client_secret) click to toggle source
# File lib/freee/pigmon/helper/oauth2_helper.rb, line 41
def oauth2_client(client_id, client_secret)
  OAuth2::Client.new(client_id,
                     client_secret,
                     site: OAUTH2_SITE,
                     token_url: OAUTH2_URL)
end
save_config() click to toggle source

現在の@configをyamlに保存

# File lib/freee/pigmon/helper/config_helper.rb, line 56
def self.save_config
  yml_path = get_config_path
  if File.exist?(yml_path)
    File.open(yml_path, 'w') { |f| YAML.dump(@config, f) }
  else
    raise "Can't find #{yml_path}. please set configure."
  end
end
token_info_hash(response) click to toggle source
# File lib/freee/pigmon/helper/oauth2_helper.rb, line 48
def token_info_hash(response)
  {
    access_token: response.token,
    refresh_token: response.refresh_token,
    expires_at: Time.at(response.expires_at).strftime('%Y-%m-%d %H:%M:%S')
  }
end
token_refresh( client_id, client_secret, access_token, refresh_token, expires_at ) click to toggle source
# File lib/freee/pigmon/helper/oauth2_helper.rb, line 21
def token_refresh(
  client_id,
  client_secret,
  access_token,
  refresh_token,
  expires_at
)
  return false if refresh_token.nil?

  client = oauth2_client(client_id, client_secret)
  oauth2_access_token = OAuth2::AccessToken.new(
    client,
    access_token,
    refresh_token: refresh_token,
    expires_at: expires_at.to_i
  )
  response = oauth2_access_token.refresh!
  token_info_hash(response)
end
url_to_generate_code(client_id) click to toggle source
# File lib/freee/pigmon/helper/oauth2_helper.rb, line 10
def url_to_generate_code(client_id)
  "https://accounts.secure.freee.co.jp/public_api/authorize?client_id=#{client_id}&redirect_uri=#{CALLBACK_URL}&response_type=code"
end