Class: ChatWork::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_base, api_version) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/chatwork/client.rb', line 6

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}#{api_version}", headers: default_header) do |builder|
    builder.request :url_encoded
    builder.adapter Faraday.default_adapter
  end
  @api_version = api_version
end

Instance Method Details

#handle_response(response) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/chatwork/client.rb', line 19

def handle_response(response)
  case response.status
  when 204
    ChatWork::ChatWorkError.from_response(response.status, response.body)
  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, response.body)
  end
end