module EDIS

Friendly little GEM for interacting with the United States International Trade Commission’s EDIS data REST services.

Public Instance Methods

build_date_params(options, optional_date_params) click to toggle source

Appends to the params date comparisions.

# File lib/edis_client.rb, line 314
def build_date_params(options, optional_date_params)
  optional_date_params.inject({}) do |params, date_param|
    if options[date_param]
      comparison = options[date_param]
      case comparison[:type]
        when :between
          params[camelize(date_param)] =
            "#BETWEEN:#{date_param[:from_date]}:#{date_param[:to_date]}"
        when :before, :after, :exact
          params[camelize(date_param)] = 
            "#{comparison[:type].uppercase}:#{comparision[:date]}"
        else
          raise ArgumentError, 
            "Unknown comparison type #{comparison[:comparison_type]}"
      end
    end
    params
  end
end
build_params(options, optional_params) click to toggle source

Builds the params hash from the options.

# File lib/edis_client.rb, line 304
def build_params(options, optional_params)
  optional_params.inject({}) do |params, param|
    params[camelize(param.to_s, false)] = options[param] if options[param]
    params
  end
end
build_path(root, options, optional_resources) click to toggle source

Builds a path with optional resources paths if specified.

# File lib/edis_client.rb, line 294
def build_path(root, options, optional_resources)
  optional_resources.inject(root) do |path, resource|
    path << "/#{options[resource]}" if options[resource]
    path
  end
end
camelize(s, first_letter_in_uppercase = true) click to toggle source

Camelize a string, lifted from Rails.

# File lib/edis_client.rb, line 337
def camelize(s, first_letter_in_uppercase = true)
  if first_letter_in_uppercase
    s.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  else
    s.to_s[0].chr.downcase + camelize(s)[1..-1]
  end
end
connect() click to toggle source

Get the correct net:http class based on config

# File lib/edis_client.rb, line 249
def connect
  uri  = URI.parse('https://edis.usitc.gov/')
  http = net_http_class.new(uri.host, uri.port)
  http.use_ssl = true 
  http.read_timeout = @config[:timeout] and http
end
document_date_params() click to toggle source
# File lib/edis_client.rb, line 147
def document_date_params
  [:official_received_date,  :modified_date]
end
download_paths() click to toggle source

download released

# File lib/edis_client.rb, line 160
def download_paths
  [:document_id, :attachment_id]
end
get_resource(path, options, params = {}) click to toggle source

Get the resource at the given path.

# File lib/edis_client.rb, line 207
def get_resource(path, options, params = {})
  connect.start do |http|
    path = path_with_params(path, params) unless params.empty?
    xml  = http.get("/data/#{path}", header(options) || {}).body
    RecursiveOpenStruct.new Crack::XML.parse(xml)
  end
end
header(options) click to toggle source

Creates the authorization header if the digest is present in the options or if it was specified as being retained when gen_disgest was called.

# File lib/edis_client.rb, line 261
def header(options)
  digest = if options[:digest]
    options[:digest]
  elsif @config[:digest]
    @config[:digest]
  else
    false
  end
  {'Authorization' => "Basic #{digest}"} if digest
end
investigation_params() click to toggle source
# File lib/edis_client.rb, line 155
def investigation_params
  [:page, :investigation_type, :investigation_status]
end
investigation_paths() click to toggle source

investigation related

# File lib/edis_client.rb, line 152
def investigation_paths
  [:investigation_number, :investigation_phase]
end
net_http_class() click to toggle source

Get the correct class based on proxy settings.

# File lib/edis_client.rb, line 282
def net_http_class
  if proxy?
    proxy = @config[:proxy]
    Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:user], proxy[:password])
  else
    Net::HTTP
  end
end
path_with_params(path, params) click to toggle source

Generate a query string reprsentation of the params and append to the path.

# File lib/edis_client.rb, line 241
def path_with_params(path, params)
  "#{path}?".concat \
    params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.reverse.join('&')
end
post_resource(path, params) click to toggle source

Post resource.

# File lib/edis_client.rb, line 230
def post_resource(path, params)
  connect.start do |http|
    req = Net::HTTP::Post.new("/data/#{path}") and req.set_form_data params
    xml = http.request(req).body
    RecursiveOpenStruct.new Crack::XML.parse(xml)
  end
end
proxy?() click to toggle source

Proxy connections?

# File lib/edis_client.rb, line 275
def proxy?
  @config.key? :proxy
end
stream_resource(path, options) { |chunk| ... } click to toggle source

Get the resource at the given path streaming the result, in chunks, to the block.

# File lib/edis_client.rb, line 219
def stream_resource(path, options)
  connect.start do |http|
    http.get("/data/#{path}", header(options) || {}) do |chunk|
      yield chunk
    end
  end
end
validate_digest(options) click to toggle source

Validate either the digest is specified or that it is set in the env.

# File lib/edis_client.rb, line 187
def validate_digest(options)
  unless options[:digest] || @config[:digest]
    raise ArgumentError, "A digest is required.  Please use gen_digest."
  end
end
validate_download(options) click to toggle source

Validate that is digest is available and the required fields are present

# File lib/edis_client.rb, line 179
def validate_download(options)
  validate_digest options 
  validate_presence_of [:document_id, :attachment_id], options
end
validate_investigation_options(options) click to toggle source

Validates that when :investigation_phase is specified so is :investigation_number

# File lib/edis_client.rb, line 197
def validate_investigation_options(options)
  if options[:investigation_phase]
    validate_presence_of [:investigation_number], options,
      ":investigation_number is required when :investigation_phase is specified."
  end
end
validate_presence_of(requires, options, msg = nil) click to toggle source

Validates the requires are present in the options. Raises ArgumentError if not.

# File lib/edis_client.rb, line 168
def validate_presence_of(requires, options, msg = nil)
  requires.each do |required|
    unless options[required]
      raise ArgumentError, msg || "Missing one or more required options #{requires}"
    end
  end
end