Sha256: beec7891cd586f60c4d523cce4bd59f2c46b720167d474844f3a756f0fa94aba

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

require 'faraday'
require 'json'

module ChatWork
  class Client
    def initialize(api_key, api_base, api_version)
      default_header = {
        'X-ChatWorkToken' => api_key,
        'User-Agent' => 'ChatWork#{api_version} RubyBinding/#{ChatWork::VERSION}'
      }

      @conn = Faraday.new(api_base, headers: default_header) do |builder|
        builder.request :url_encoded
        builder.adapter Faraday.default_adapter
      end
      @api_version = api_version
    end

    def handle_response(response)
      case response.status
      when 200..299
        begin
          JSON.parse(response.body)
        rescue JSON::ParserError => e
          raise ChatWork::APIConnectionError.new("Response JSON is broken. #{e.message}: #{response.body}")
        end
      else
        ChatWork::ChatWorkError.from_response(response.status, rensponse.body)
      end
    end

    Faraday::Connection::METHODS.each do |method|
      define_method(method) do |url, *args|
        begin
          response = @conn.__send__(method, @api_version + url, *args)
        rescue Faraday::Error::ClientError => e
          raise ChatWork::APIConnectionError.faraday_error(e)
        end
        handle_response(response)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chatwork-0.0.2 lib/chatwork/client.rb
chatwork-0.0.1 lib/chatwork/client.rb