Class: MyJohnDeere::FileResource

Inherits:
OrganizationOwnedResource show all
Defined in:
lib/myjohndeere/file_resource.rb

Instance Attribute Summary

Attributes inherited from OrganizationOwnedResource

#organization_id

Attributes inherited from SingleResource

#deleted

Attributes inherited from Requestable

#access_token, #links

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OrganizationOwnedResource

owning_organization_link_item

Methods included from JSONAttributes

included

Methods included from RESTMethods

included

Methods inherited from Requestable

#extract_link_with_rel_from_list, get_created_id_from_response_headers

Constructor Details

#initialize(json_object, access_token = nil) ⇒ FileResource

Returns a new instance of FileResource.



9
10
11
# File 'lib/myjohndeere/file_resource.rb', line 9

def initialize(json_object, access_token = nil)
  super(json_object, access_token)
end

Class Method Details

.create(access_token, organization_id, map_layer_id, file_type: nil, metadata: []) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/myjohndeere/file_resource.rb', line 13

def self.create(access_token, organization_id, map_layer_id, file_type: nil,
    metadata: [])
  raise ArgumentError.new("You must pass a file_type") if file_type.nil?
  raise ArgumentError.new("You must pass a valid organization id") if organization_id.nil?
  raise ArgumentError.new("You must pass a valid map layer id") if map_layer_id.nil?

  case file_type
  when :png
    mime_type = 'image/png'
  when :zip
    mime_type = 'application/zip'
  else
    raise ArgumentError.new("You must specify either a zip or a png")
  end

  body = {
    links: [
        self.owning_organization_link_item(organization_id)
    ],
    mimeType: mime_type,
    metadata: .map { |m| m.to_hash }
  }
  
  return send_create(access_token, body, {map_layer_id: map_layer_id})
end

.upload_file(access_token, file_resource_id, file_path) ⇒ Object

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/myjohndeere/file_resource.rb', line 39

def self.upload_file(access_token, file_resource_id, file_path)
  raise ArgumentError.new("You must pass an existing file") if !File.exists?(file_path)
  raise ArgumentError.new("You must pass a valid file_resource_id") if file_resource_id.nil?

  File.open(file_path, "rb:UTF-8") do |f|
      body = f.read()
      response = access_token.execute_request(:put,
        "#{self.base_jd_resource}/#{file_resource_id}",
        body: body,
        headers: { 
          'accept'=> 'application/vnd.deere.axiom.v3+json',
          "Content-Type"=>'application/octet-stream' ,
          "Content-Length" => body.bytesize.to_s
        })
      return response.code == 204
  end
end