Class: Dratools::DdbjResourceClient

Inherits:
Object
  • Object
show all
Defined in:
lib/dratools/ddbj_resource_client.rb

Overview

DDBJ Search API を呼び出す薄い HTTP クライアント。

Constant Summary collapse

DDBJ_SEARCH_API_BASE_URL =
'https://ddbj.nig.ac.jp/search/api'
ENTRIES_PATH =
'entries'
'dblink'
ENTRY_RECORD_EXTENSION =
'.json'
BULK_MAX_IDS =
1000
100
HTTPS_SCHEME =
'https'
HTTP_LOCATION_HEADER =
'location'
USER_AGENT_HEADER =
'User-Agent'
DEFAULT_REDIRECT_LIMIT =
5
DEFAULT_OPEN_TIMEOUT_SECONDS =
10
DEFAULT_READ_TIMEOUT_SECONDS =
30

Instance Method Summary collapse

Constructor Details

#initialize(base_url: DDBJ_SEARCH_API_BASE_URL, open_timeout: DEFAULT_OPEN_TIMEOUT_SECONDS, read_timeout: DEFAULT_READ_TIMEOUT_SECONDS, progress: ProgressReporter.new) ⇒ DdbjResourceClient

Returns a new instance of DdbjResourceClient.



29
30
31
32
33
34
35
# File 'lib/dratools/ddbj_resource_client.rb', line 29

def initialize(base_url: DDBJ_SEARCH_API_BASE_URL, open_timeout: DEFAULT_OPEN_TIMEOUT_SECONDS,
               read_timeout: DEFAULT_READ_TIMEOUT_SECONDS, progress: ProgressReporter.new)
  @base_url = base_url.delete_suffix('/')
  @open_timeout = open_timeout
  @read_timeout = read_timeout
  @progress = progress
end

Instance Method Details



57
58
59
60
61
# File 'lib/dratools/ddbj_resource_client.rb', line 57

def fetch_db_link_counts(items)
  items.each_slice(DBLINK_COUNTS_MAX_ITEMS).with_object({}) do |chunk, counts|
    counts.merge!(fetch_db_link_counts_chunk(chunk))
  end
end


42
43
44
45
46
47
# File 'lib/dratools/ddbj_resource_client.rb', line 42

def fetch_db_links(type, accession, target: nil)
  @progress.report("linking #{type} #{accession}")
  request_uri = URI("#{@base_url}/#{DBLINK_PATH}/#{type}/#{accession}")
  request_uri.query = URI.encode_www_form(target: target) if target
  fetch_json(request_uri.to_s).fetch(DdbjRecordFields::DB_XREFS_KEY, [])
end

#fetch_resource_record(type, accession) ⇒ Object



37
38
39
40
# File 'lib/dratools/ddbj_resource_client.rb', line 37

def fetch_resource_record(type, accession)
  @progress.report("fetching #{type} #{accession}")
  fetch_json("#{@base_url}/#{ENTRIES_PATH}/#{type}/#{accession}#{ENTRY_RECORD_EXTENSION}")
end

#fetch_resource_records_bulk(type, accessions, include_db_xrefs: false) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/dratools/ddbj_resource_client.rb', line 49

def fetch_resource_records_bulk(type, accessions, include_db_xrefs: false)
  accessions.each_slice(BULK_MAX_IDS).with_object({}) do |chunk, records|
    records.merge!(
      fetch_resource_records_bulk_chunk(type, chunk, include_db_xrefs: include_db_xrefs)
    )
  end
end