Class: Resync::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/resync/client/client.rb,
lib/resync/client/version.rb

Overview

Utility class for retrieving HTTP content and parsing it as ResourceSync documents.

Constant Summary collapse

VERSION =

The version of this gem.

'0.1.2'

Instance Method Summary collapse

Constructor Details

#initialize(helper: HTTPHelper.new(user_agent: "resync-client #{VERSION}")) ⇒ Client

Creates a new Client

Parameters:

  • helper (HTTPHelper) (defaults to: HTTPHelper.new(user_agent: "resync-client #{VERSION}")) —

    the HTTP helper. Defaults to a new HTTP helper with resync-client VERSION as the User-Agent string.



15
16
17
# File 'lib/resync/client/client.rb', line 15

def initialize(helper: HTTPHelper.new(user_agent: "resync-client #{VERSION}"))
  @helper = helper
end

Instance Method Details

#download_to_file(uri:, path:) ⇒ String

Gets the content of the specified URI and saves it to the specified file, overwriting it if it exists.

Parameters:

  • uri (URI, String) —

    the URI to download

  • path (String) —

    the path to save the download to

Returns:

  • (String) —

    the path to the downloaded file



52
53
54
55
# File 'lib/resync/client/client.rb', line 52

def download_to_file(uri:, path:)
  uri = Resync::XML.to_uri(uri)
  @helper.fetch_to_file(path: path, uri: uri)
end

#download_to_temp_file(uri) ⇒ String

Gets the content of the specified URI and saves it to a temporary file.

Parameters:

  • uri (URI, String) —

    the URI to download

Returns:

  • (String) —

    the path to the downloaded file



42
43
44
45
# File 'lib/resync/client/client.rb', line 42

def download_to_temp_file(uri)
  uri = Resync::XML.to_uri(uri)
  @helper.fetch_to_file(uri: uri)
end

#get(uri) ⇒ String

Gets the content of the specified URI as a string.

Parameters:

  • uri (URI, String) —

    the URI to download

Returns:

  • (String) —

    the content of the URI



34
35
36
37
# File 'lib/resync/client/client.rb', line 34

def get(uri)
  uri = Resync::XML.to_uri(uri)
  @helper.fetch(uri: uri)
end

#get_and_parse(uri) ⇒ Object

Gets the content of the specified URI and parses it as a ResourceSync document.



23
24
25
26
27
28
29
# File 'lib/resync/client/client.rb', line 23

def get_and_parse(uri)
  uri = Resync::XML.to_uri(uri)
  raw_contents = get(uri)
  doc = XMLParser.parse(raw_contents)
  doc.client = self
  doc
end