class MfCloud::Invoice::Client

Public Class Methods

new(options = {}) { |config| ... } click to toggle source
# File lib/mf_cloud/invoice/client.rb, line 29
def initialize(options = {}, &block)
  @config = MfCloud::Invoice::Configure.new(options)

  yield @config if block_given?

  @conn = Faraday.new(url: "#{MfCloud::Invoice::API_URL}#{@config.api_version}") do |faraday|
    faraday.request :json

    faraday.response :json

    faraday.adapter Faraday.default_adapter
  end
end

Public Instance Methods

billing_status_payment() click to toggle source
# File lib/mf_cloud/invoice/client.rb, line 59
def billing_status_payment
  @_billing_status_payment ||= MfCloud::Invoice::Api::BillingStatusPayment.new(self)
end
billings() click to toggle source
# File lib/mf_cloud/invoice/client.rb, line 55
def billings
  @_billings ||= MfCloud::Invoice::Api::Billing.new(self)
end
delete(path, params = {}) click to toggle source
# File lib/mf_cloud/invoice/client.rb, line 95
def delete(path, params = {})
  res = @conn.delete do |req|
    req.headers['Authorization'] = "Bearer #{@config.access_token}"
    req.url path, params
  end

  check_response!(res)
end
get(path, params = {}) click to toggle source
# File lib/mf_cloud/invoice/client.rb, line 63
def get(path, params = {})
  res = @conn.get do |req|
    req.headers['Authorization'] = "Bearer #{@config.access_token}"
    req.url path, params
  end

  check_response!(res)
  res.body
end
items() click to toggle source
# File lib/mf_cloud/invoice/client.rb, line 51
def items
  @_items ||= MfCloud::Invoice::Api::Item.new(self)
end
office() click to toggle source
# File lib/mf_cloud/invoice/client.rb, line 43
def office
  @_office ||= MfCloud::Invoice::Api::Office.new(self)
end
partners() click to toggle source
# File lib/mf_cloud/invoice/client.rb, line 47
def partners
  @_partners ||= MfCloud::Invoice::Api::Partner.new(self)
end
post(path, params = {}) click to toggle source
# File lib/mf_cloud/invoice/client.rb, line 73
def post(path, params = {})
  res = @conn.post do |req|
    req.headers['Authorization'] = "Bearer #{@config.access_token}"
    req.url path
    req.body = params
  end

  check_response!(res)
  res.body
end
put(path, params = {}) click to toggle source
# File lib/mf_cloud/invoice/client.rb, line 84
def put(path, params = {})
  res = @conn.put do |req|
    req.headers['Authorization'] = "Bearer #{@config.access_token}"
    req.url path
    req.body = params
  end

  check_response!(res)
  res.body
end

Private Instance Methods

check_response!(res) click to toggle source
# File lib/mf_cloud/invoice/client.rb, line 106
def check_response!(res)
  case res.status
  when 400
    fail MfCloud::Invoice::InvalidRequest, res.body["errors"].first["message"]
  when 401
    fail MfCloud::Invoice::InvalidAccessToken, 'アクセストークンが不正です'
  when 402
    fail MfCloud::Invoice::PaymentRequired, res.body["errors"].first["message"]
  when 404
    fail MfCloud::Invoice::ResourceNotFound, res.body["errors"].first["message"]
  when 422
    fail MfCloud::Invoice::InvalidParameter, res.body["errors"].first["message"]
  when 429
    fail MfCloud::Invoice::RateLimitted, res.body["errors"].first["message"]
  when 500
    if res.body["errors"].nil?
      msg = 'サーバーがメンテナンス中かダウンしている可能性があります。しばらく待って再度お試しください。'
    else
      msg = res.body["errors"].first["message"]
    end

    fail MfCloud::Invoice::InternalServerError, msg
  else
    true
  end
end