Module: OpenStax::Cnx::V1

Defined in:
lib/openstax_cnx.rb,
lib/openstax/cnx/v1.rb,
lib/openstax/cnx/v1/book.rb,
lib/openstax/cnx/v1/page.rb,
lib/openstax/cnx/v1/figure.rb,
lib/openstax/cnx/v1/element.rb,
lib/openstax/cnx/v1/key_term.rb,
lib/openstax/cnx/v1/book_part.rb,
lib/openstax/cnx/v1/paragraph.rb

Defined Under Namespace

Modules: Baked Classes: Book, BookPart, Configuration, Element, Figure, KeyTerm, NullLogger, Page, Paragraph

Class Method Summary collapse

Class Method Details

.archive_url_baseObject



75
76
77
# File 'lib/openstax/cnx/v1.rb', line 75

def archive_url_base
  configuration.archive_url_base
end

.archive_url_for(path) ⇒ Object

Archive url for the given path Forces /contents/ to be prepended to the path, unless the path begins with /



111
112
113
# File 'lib/openstax/cnx/v1.rb', line 111

def archive_url_for(path)
  Addressable::URI.join(configuration.archive_url_base, '/contents/', path).to_s
end

.book(**options) ⇒ Object



132
133
134
# File 'lib/openstax/cnx/v1.rb', line 132

def book(**options)
  OpenStax::Cnx::V1::Book.new(**options)
end

.configurationObject



24
25
26
# File 'lib/openstax/cnx/v1.rb', line 24

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



20
21
22
# File 'lib/openstax/cnx/v1.rb', line 20

def self.configure
  yield configuration
end

.fetch(url) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/openstax/cnx/v1.rb', line 121

def fetch(url)
  begin
    OpenStax::Cnx::V1.logger.debug { "Fetching #{url}" }
    data = JSON.parse URI.open(url, 'ACCEPT' => 'text/json').read
    data.delete("history") if configuration.ignore_history
    data
  rescue OpenURI::HTTPError => exception
    raise OpenStax::HTTPError, "#{exception.message} for URL #{url}"
  end
end

.loggerObject



83
84
85
# File 'lib/openstax/cnx/v1.rb', line 83

def logger
  configuration.logger
end

.new_configurationObject



71
72
73
# File 'lib/openstax/cnx/v1.rb', line 71

def new_configuration
  OpenStax::Cnx::V1::Configuration.new
end

.webview_url_baseObject



79
80
81
# File 'lib/openstax/cnx/v1.rb', line 79

def webview_url_base
  configuration.webview_url_base
end

.webview_url_for(path) ⇒ Object

Webview url for the given path Forces /contents/ to be prepended to the path, unless the path begins with /



117
118
119
# File 'lib/openstax/cnx/v1.rb', line 117

def webview_url_for(path)
  Addressable::URI.join(configuration.webview_url_base, '/contents/', path).to_s
end

.with_archive_url(url) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/openstax/cnx/v1.rb', line 87

def with_archive_url(url)
  begin
    old_url = archive_url_base
    self.configuration.archive_url_base = url

    yield
  ensure
    self.configuration.archive_url_base = old_url
  end
end

.with_webview_url(url) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/openstax/cnx/v1.rb', line 98

def with_webview_url(url)
  begin
    old_url = webview_url_base
    self.configuration.webview_url_base = url

    yield
  ensure
    self.configuration.webview_url_base = old_url
  end
end