Class: Freee::Api::Deals

Inherits:
Object
  • Object
show all
Defined in:
lib/freee/deals/client.rb

Constant Summary collapse

PATH =

取引作成用PATH

'/api/1/deals'

Instance Method Summary collapse

Constructor Details

#initializeDeals

A new instance of HTTP Client.



11
12
13
14
15
16
17
# File 'lib/freee/deals/client.rb', line 11

def initialize
  @client = Faraday.new(url: Parameter::SITE) do |faraday|
    faraday.request :json
    faraday.response :json, content_type: /\bjson$/
    faraday.adapter Faraday.default_adapter
  end
end

Instance Method Details

#create_deal(access_token, params) ⇒ Hash

Parameters:

  • access_token (String)

    アクセストークン

  • params (Hash)

    新規取引作成用のパラメータ

Returns:

  • (Hash)

    取引作成の結果



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/freee/deals/client.rb', line 24

def create_deal(access_token, params)
  raise 'アクセストークンが設定されていません' if access_token.empty?
  raise '収入・支出の発生日が指定されていません' unless params.key?(:issue_date)
  raise '収支区分が指定されていません' unless params.key?(:type)
  raise '事業所IDが設定されていません' unless params.key?(:company_id)
  @client.authorization :Bearer, access_token
  response = @client.post do |req|
    req.url PATH
    req.body = params.to_json
  end
  case response.status
  when 400
    raise StandardError, response.body
  when 401
    raise 'Unauthorized'
  end
  response
end