Class: Kooaba::UploadRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/kooaba/upload_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, bucket_id) ⇒ UploadRequest

Returns a new instance of UploadRequest.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kooaba/upload_request.rb', line 17

def initialize(item, bucket_id)
  @item = item
  @bucket_id = bucket_id
  @message = MultipartMessage.new
  item.image_files.each do |image_path|
    content_type = `file --mime-type -b #{image_path}`
    @message.add_file_part('images', image_path, content_type)
  end
  @message.add_text_part('reference_id', item.reference_id) if item.reference_id
  @message.add_text_part('title', item.title) if item.title
  @message.add_text_part('enabled', item.enabled)
  @message.add_text_part('metadata', item.) if item.
  @message.add_text_part('kind', item.kind) if item.kind
  @message.add_text_part('locale', item.locale) if item.locale
end

Instance Attribute Details

#bucket_idObject

Returns the value of attribute bucket_id.



13
14
15
# File 'lib/kooaba/upload_request.rb', line 13

def bucket_id
  @bucket_id
end

#itemObject

Returns the value of attribute item.



15
16
17
# File 'lib/kooaba/upload_request.rb', line 15

def item
  @item
end

#messageObject

Returns the value of attribute message.



12
13
14
# File 'lib/kooaba/upload_request.rb', line 12

def message
  @message
end

Instance Method Details

#make_request(url) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kooaba/upload_request.rb', line 45

def make_request(url)
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http.read_timeout = 500

  req = Net::HTTP::Post.new(url.path)

  req.body = @message.body
  req['date'] = Time.new.httpdate
  req['content-type'] = @message.content_type
  req['authorization'] = "Token #{Kooaba.data_key}"

  http.request(req)
end

#parse_2xx(http_resp) ⇒ Object



74
75
76
77
78
79
# File 'lib/kooaba/upload_request.rb', line 74

def parse_2xx(http_resp)
  json = JSON.parse(http_resp.body)
  @item.uuid = json["uuid"]
  @item.enabled = json["enabled"]
  @item.images_sha1 = json["images"].empty? ? [] : json["images"].map {|a| a["sha1"]}
end

#parse_4xx(http_resp) ⇒ Object

Raises:

  • (ArgumentError)


81
82
83
# File 'lib/kooaba/upload_request.rb', line 81

def parse_4xx(http_resp)
  raise ArgumentError.new("Kooaba Response: #{http_resp.code} #{http_resp.body}.")
end

#parse_5xx(http_resp) ⇒ Object

Raises:

  • (StandardError)


85
86
87
# File 'lib/kooaba/upload_request.rb', line 85

def parse_5xx(http_resp)
  raise StandardError.new("Internal Server Error: #{http_resp.code} #{http_resp.body}")
end

#parse_request(http_resp) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kooaba/upload_request.rb', line 61

def parse_request(http_resp)
  case http_resp
  when Net::HTTPSuccess
    parse_2xx(http_resp)
  when Net::HTTPClientError
    parse_4xx(http_resp)
  when Net::HTTPServerError
    parse_5xx(http_resp)
  else
    unknown_response(http_resp)
  end
end

#startObject

Returns the http response from the Kooaba servers.



36
37
38
39
40
41
42
43
# File 'lib/kooaba/upload_request.rb', line 36

def start
  url = URI.parse(Kooaba::UPLOAD_URL + "buckets/" + bucket_id + "/items")

  resp = make_request(url)
  parse_request(resp)

  return resp
end

#unknown_response(http_resp) ⇒ Object

Raises:

  • (StandardError)


89
90
91
# File 'lib/kooaba/upload_request.rb', line 89

def unknown_response(http_resp)
  raise StandardError, "Unknown response: #{http_resp.code} #{http_resp.body} "
end