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/book_part.rb

Defined Under Namespace

Modules: Baked Classes: Book, BookPart, Configuration, NullLogger, Page

Class Method Summary collapse

Class Method Details

.archive_url_baseObject



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

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 /



107
108
109
# File 'lib/openstax/cnx/v1.rb', line 107

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

.book(options = {}) ⇒ Object



128
129
130
# File 'lib/openstax/cnx/v1.rb', line 128

def book(options = {})
  OpenStax::Cnx::V1::Book.new(options)
end

.configurationObject



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

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

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

Yields:



16
17
18
# File 'lib/openstax/cnx/v1.rb', line 16

def self.configure
  yield configuration
end

.fetch(url) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/openstax/cnx/v1.rb', line 117

def fetch(url)
  begin
    OpenStax::Cnx::V1.logger.debug { "Fetching #{url}" }
    data = JSON.parse 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



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

def logger
  configuration.logger
end

.new_configurationObject



67
68
69
# File 'lib/openstax/cnx/v1.rb', line 67

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

.webview_url_baseObject



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

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 /



113
114
115
# File 'lib/openstax/cnx/v1.rb', line 113

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

.with_archive_url(url) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/openstax/cnx/v1.rb', line 83

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



94
95
96
97
98
99
100
101
102
103
# File 'lib/openstax/cnx/v1.rb', line 94

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