Class: KenpoApi::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/kenpo_api/client.rb

Constant Summary collapse

BASE_URL =
'https://as.its-kenpo.or.jp/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



14
15
16
17
18
19
20
21
22
23
# File 'lib/kenpo_api/client.rb', line 14

def initialize
  @conn = Faraday.new(url: BASE_URL) do |builder|
    builder.use Faraday::Request::UrlEncoded
    builder.adapter Faraday.default_adapter
  end
  # Set default settings.
  @timeout = 5
  @open_timeout = 5
  @cookiejar = CookieJar::Jar.new
end

Instance Attribute Details

#open_timeoutObject

Returns the value of attribute open_timeout.



12
13
14
# File 'lib/kenpo_api/client.rb', line 12

def open_timeout
  @open_timeout
end

#timeoutObject

Returns the value of attribute timeout.



12
13
14
# File 'lib/kenpo_api/client.rb', line 12

def timeout
  @timeout
end

Instance Method Details

#access(path:, method: :get, params: {}, headers: {}, redirect_limit: 0) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kenpo_api/client.rb', line 25

def access(path:, method: :get, params: {}, headers: {}, redirect_limit: 0)
  response = @conn.send(method, path, params) do |req|
    req.options.timeout = @timeout
    req.options.open_timeout = @open_timeout
    req.headers = headers
    req.headers['cookie'] = @cookiejar.get_cookie_header(@conn.url_prefix.to_s)
  end
  raise NetworkError.new("Failed to fetch http content. path: #{path} status_code: #{response.status}") unless (200...400).include?(response.status)

  @cookiejar.set_cookie(@conn.url_prefix.to_s, response.headers['set-cookie']) if response.headers.has_key?('set-cookie')

  if (redirect_limit > 0) && (location = response['location'])
    response = self.access(path: location, method: method, params: params, headers: headers, redirect_limit: redirect_limit - 1)
  end

  return (yield response) if block_given?
  response
rescue KenpoApiError => e
  raise e
rescue => e
  raise NetworkError.new("Failed to fetch http content. path: #{path} original_error: #{e.message}")
end

#fetch_document(path:, method: :get, params: {}, headers: {}) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/kenpo_api/client.rb', line 48

def fetch_document(path:, method: :get, params: {}, headers: {})
  response = access(path: path, method: method, params: params, headers: headers)
  document = Nokogiri::HTML(response.body)

  return (yield document) if block_given?
  document
end

#parse_single_form_page(path:, method: :get, params: {}, headers: {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kenpo_api/client.rb', line 56

def parse_single_form_page(path:, method: :get, params: {}, headers: {})
  document = fetch_document(path: path, method: method, params: params, headers: headers)
  form_element = document.xpath('//form').first

  next_page_info = nil
  unless form_element.nil?
    path = form_element['action']
    method = form_element['method']
    params = document.xpath('//input[@type="hidden"]').select {|input| ! input['name'].nil?}.map {|input| [input['name'], input['value']]}.to_h
    next_page_info = {path: path, method: method, params: params}
  end

  return (yield next_page_info, document) if block_given?
  return next_page_info, document
rescue KenpoApiError => e
  raise e
rescue => e
  raise ParseError.new("Failed to parse HTML. message: #{e.message}")
end