Class: Puree::Download

Inherits:
Object
  • Object
show all
Defined in:
lib/puree/download.rb

Overview

Download

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: nil, username: nil, password: nil, basic_auth: nil) ⇒ Download

Returns a new instance of Download.

Parameters:

  • base_url (String) (defaults to: nil)
  • username (String) (defaults to: nil)
  • password (String) (defaults to: nil)
  • basic_auth (Boolean) (defaults to: nil)


12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/puree/download.rb', line 12

def initialize(base_url: nil,
               username: nil,
               password: nil,
               basic_auth: nil)
  @resource_type = :download
  @api_map = Puree::Map.new.get
  @base_url = base_url.nil? ? Puree.base_url : base_url
  @basic_auth = basic_auth.nil? ? Puree.basic_auth : basic_auth
  if @basic_auth === true
    @username = username.nil? ? Puree.username : username
    @password = password.nil? ? Puree.password : password
  end
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/puree/download.rb', line 6

def response
  @response
end

Instance Method Details

#get(limit: 20, offset: 0, resource: nil) ⇒ Array<Hash> Also known as: find

Get

Parameters:

  • limit (Integer) (defaults to: 20)
  • offset (Integer) (defaults to: 0)
  • resource (Symbol) (defaults to: nil)

Returns:

  • (Array<Hash>)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
# File 'lib/puree/download.rb', line 32

def get(limit: 20,
        offset: 0,
        resource: nil)
  missing = missing_credentials
  if !missing.empty?
    missing.each do |m|
      puts "#{self.class.name}" + '#' + "#{__method__} missing #{m}"
    end
    exit
  end

  # strip any trailing slash
  @base_url = @base_url.sub(/(\/)+$/, '')
  @auth = Base64::strict_encode64(@username + ':' + @password)

  @options = {
      basic_auth:     @basic_auth,
      latest_api:     true,
      resource_type:  @resource_type.to_sym,
      rendering:      :system,
      limit:          limit,
      offset:         offset,
      resource:       resource.to_sym
  }
  headers = {
      'Accept' => 'application/xml',
      'Authorization' => 'Basic ' + @auth
  }
  query = {}
  query['rendering'] = @options[:rendering]

  if @options[:limit]
    query['window.size'] = @options[:limit]
  end

  if @options[:offset]
    query['window.offset'] = @options[:offset]
  end

  if @options[:resource]
    query['contentType'] = service_family
  end

  begin
    url = build_url
    req = HTTP.headers accept: headers['Accept']
    if @options[:basic_auth]
      req = req.auth headers['Authorization']
    end
    @response = req.get(url, params: query)
    @doc = Nokogiri::XML @response.body
    @doc.remove_namespaces!
  rescue HTTP::Error => e
    puts 'HTTP::Error '+ e.message
  end

  get_data? ?  : []
end

#metadataArray<Hash>

All metadata

Returns:

  • (Array<Hash>)


97
98
99
# File 'lib/puree/download.rb', line 97

def 
  @metadata
end