Class: MrMurano::ProductContent

Inherits:
ProductBase show all
Defined in:
lib/MrMurano/Product.rb

Overview

Manage the uploadable content for products.

Instance Method Summary collapse

Methods inherited from ProductBase

#endPoint

Methods included from Verbose

#debug, #verbose

Methods included from Http

#curldebug, #delete, #get, #http, #http_reset, #json_opts, #post, #postf, #put, #set_def_headers, #showHttpError, #token, #tryToPrettyJSON, #workit

Constructor Details

#initializeProductContent

Returns a new instance of ProductContent.



65
66
67
68
69
70
71
72
# File 'lib/MrMurano/Product.rb', line 65

def initialize
  super
  @uriparts << :proxy
  @uriparts << :provision
  @uriparts << :manage
  @uriparts << :content
  @uriparts << @pid
end

Instance Method Details

#create(id, meta = '', protect = false) ⇒ Object

Create a new content item



89
90
91
92
93
94
# File 'lib/MrMurano/Product.rb', line 89

def create(id, meta='', protect=false)
  http_reset
  data = {:id=>id, :meta=>meta}
  data[:protected] = true if protect
  postf('/', data)
end

#download(id, &block) ⇒ Object

Download data for content item



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/MrMurano/Product.rb', line 116

def download(id, &block)
  get("/#{id}?download=true") do |request, http|
    http.request(request) do |resp|
      case resp
      when Net::HTTPSuccess
        if block_given? then
          resp.read_body &block
        else
          resp.read_body do |chunk|
            $stdout.write chunk
          end
        end
      else
        showHttpError(request, response)
        raise resp
      end
    end
    nil
  end
end

#info(id) ⇒ Object

Get info for content item



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/MrMurano/Product.rb', line 102

def info(id)
  get("/#{id}") do |request, http|
    http.request(request) do |resp|
      case resp
      when Net::HTTPSuccess
        return CSV.parse(resp.body)
      else
        return nil
      end
    end
  end
end

#listObject

List all things in content area



75
76
77
78
79
# File 'lib/MrMurano/Product.rb', line 75

def list
  ret = get('/')
  return [] if ret.kind_of?(Hash)
  ret.lines.map{|i|i.chomp}
end

#list_for(sn) ⇒ Object

List all contents allowed for sn



82
83
84
85
86
# File 'lib/MrMurano/Product.rb', line 82

def list_for(sn)
  ret = get("/?sn=#{sn}")
  return [] if ret.kind_of?(Hash)
  ret.lines.map{|i|i.chomp}
end

#remove(id) ⇒ Object

Remove Content item



97
98
99
# File 'lib/MrMurano/Product.rb', line 97

def remove(id)
  postf('/', {:id=>id, :delete=>true})
end

#remove_content(id) ⇒ Object

Delete data for content item Note that the content item is still present and listed.



159
160
161
# File 'lib/MrMurano/Product.rb', line 159

def remove_content(id)
  delete("/#{id}")
end

#upload(id, path) ⇒ Object

Upload data for content item TODO: add support for passing in IOStream



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/MrMurano/Product.rb', line 139

def upload(id, path)
  path = Pathname.new(path) unless path.kind_of? Pathname

  mime = MIME::Types.type_for(path.to_s)[0] || MIME::Types["application/octet-stream"][0]
  uri = endPoint("/#{id}")
  request = Net::HTTP::Post.new(uri)
  ret = nil

  path.open do |io|
    request.body_stream = io
    set_def_headers(request)
    request.content_length = path.size
    request.content_type = mime.simplified
    ret = workit(request)
  end
  ret
end