Class: Ecfr::VersionerService::Structure

Inherits:
Base show all
Defined in:
lib/ecfr/versioner_service/structure.rb

Constant Summary collapse

STRUCTURE_PATH =
"v1/structure"

Constants inherited from Base

Base::SUPPORTED_ARRAY_ACCESSORS

Instance Attribute Summary collapse

Attributes inherited from Base

#metadata, #request_data, #response_status, #results

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

base_url, service_name, service_path

Methods inherited from Base

basic_auth_client_options, #each, metadata, metadata_key, result_key

Methods included from Extensible

#inherited

Methods included from AttributeMethodDefinition

included

Methods inherited from Client

build, cache_key, client, client_pool, delete, execute, get, handle_response, perform, post, purge

Methods included from ParallelClient

included

Constructor Details

#initialize(data, options = {format: "json"}) ⇒ Structure

Returns a new instance of Structure.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ecfr/versioner_service/structure.rb', line 61

def initialize(data, options = {format: "json"})
  data = data.body if data.is_a?(Faraday::Response)

  format = options[:format]
  skip_cache = false

  # we're going to load a response from sideloaded cache
  if data.is_a?(Hash) && format != "sideloaded"
    format = "sideloaded"
    skip_cache = true
  end

  if format == "json"
    @data = JSON.parse(data).with_indifferent_access
  elsif format == "xml"
    @data = Nokogiri::XML::Document.parse(data)
  elsif format == "sideloaded"
    @data = data.with_indifferent_access

    if Ecfr.config.cache_responses && !skip_cache
      # cache sideloaded response seperately so that a non sideloaded
      # call to structure can take advantage of cache
      cache_key = determine_cache_key(options[:referrer].request_data)

      # the client expects to be dealing with response objects, in a
      # sideload scenario we need to cache a response like object
      RequestStore[cache_key] = OpenStruct.new(
        body: data,
        status: options[:referrer].response_status
      )
    end
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



59
60
61
# File 'lib/ecfr/versioner_service/structure.rb', line 59

def data
  @data
end

Class Method Details

.find(date, title_number, options = {}) ⇒ <Structure>

Retrieves the structure content for given date and hierarchy

Parameters:

  • date (<Date, String, 'current'>)

    ISO date string or ‘current’

  • title_number (<Integer, String>)

    CFR title number

  • options (<Hash>) (defaults to: {})

Options Hash (options):

  • format (String) — default: json

    the format that should be returned

Returns:

  • (<Structure>)

    a structure representation based on the format requested



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ecfr/versioner_service/structure.rb', line 16

def self.find(date, title_number, options = {})
  default_options = {format: "json"}
  options = default_options.merge(options.symbolize_keys)

  format = options.delete(:format)

  perform(
    :get,
    structure_path(date, title_number, format),
    params: options.except(:section, :appendix).compact,
    perform_options: {
      init_data: {format: format},
      parse_response: false
    }
  )
end

.url_for(date, title_number, options = {}) ⇒ <String>

Provides a url to the structure content for given date and hierarchy

Parameters:

  • date (<Date, String, 'current'>)

    ISO date string or ‘current’

  • title_number (<Integer, String>)

    CFR title number

  • options (<Hash>) (defaults to: {})

    <description>

Options Hash (options):

  • format (String) — default: json

    the format that should be returned

Returns:

  • (<String>)

    URL to retreive structure content for given date and hierarchy



43
44
45
46
47
48
49
50
51
# File 'lib/ecfr/versioner_service/structure.rb', line 43

def self.url_for(date, title_number, options = {})
  default_options = {format: "json"}
  options = default_options.merge(options.symbolize_keys)

  format = options.delete(:format)
  path = [service_path, structure_path(date, title_number, format)].join("/")

  client.build_url(path, options).to_s
end