Class: InkFilePicker::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ink_file_picker/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client

Returns a new instance of Client.



7
8
9
# File 'lib/ink_file_picker/client.rb', line 7

def initialize(configuration)
  self.configuration = Configuration.new configuration
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



5
6
7
# File 'lib/ink_file_picker/client.rb', line 5

def configuration
  @configuration
end

Instance Method Details

#convert_url(handle_or_url, params = {}, policy_attributes = {}) ⇒ Object

Public: Generates a convert URL for given file.

handle_or_url - The handle or URL to the file params - Convert params, like 100, h:100 policy_attributes - If you use security policies you may send in for instance (Time.now + 60 * 10) here

Returns a URL to the converted image



96
97
98
# File 'lib/ink_file_picker/client.rb', line 96

def convert_url(handle_or_url, params = {}, policy_attributes = {})
  generate_url handle_or_url, params, policy_attributes, call: 'convert', url_action: 'convert'
end

#http_connectionObject



147
148
149
150
151
152
153
# File 'lib/ink_file_picker/client.rb', line 147

def http_connection
  @http_connection ||= Faraday.new(url: configuration.filepicker_url) do |builder|
    builder.request :multipart
    builder.request :url_encoded
    builder.adapter configuration.http_adapter || Faraday.default_adapter
  end
end

#policy(attributes) ⇒ Object

Public: Creates a policy with default configuration set in this client.

Returns Policy object



132
133
134
135
136
137
138
139
140
141
# File 'lib/ink_file_picker/client.rb', line 132

def policy(attributes)
  defaults = {
    secret: configuration.secret,
    expiry: Time.now.to_i + configuration.default_expiry
  }.freeze

  attributes = defaults.merge attributes

  Policy.new attributes
end

#remove(handle_or_url, policy_attributes = {}) ⇒ Object

Public: Removes a file from file picker.

handle_or_url - The handle or URL to the file policy_attributes - If you use security policies you may send in for instance (Time.now + 60 * 10) here

Returns boolean value



59
60
61
62
63
# File 'lib/ink_file_picker/client.rb', line 59

def remove(handle_or_url, policy_attributes = {})
  response = http_connection.delete remove_url(handle_or_url, policy_attributes)

  wrap_response_or_fail_unless_success! response
end

#remove_url(handle_or_url, policy_attributes = {}) ⇒ Object

Public: Generates a you can use for removing an asset on file picker.

handle_or_url - The handle or URL to the file policy_attributes - If you use security policies you may send in for instance (Time.now + 60 * 10) here

Returns a URL to the converted image



85
86
87
# File 'lib/ink_file_picker/client.rb', line 85

def remove_url(handle_or_url, policy_attributes = {})
  generate_url handle_or_url, {key: configuration.key}, policy_attributes, call: 'remove'
end

#retrieve_url(handle_or_url, params = {}, policy_attributes = {}) ⇒ Object

Public: Generates a URL for a given file

handle_or_url - The handle or URL to the file params - Params to be added as get params, like true policy_attributes - If you use security policies you may send in for instance (Time.now + 60 * 10) here

This method is not that usefull unless you have enabled security policy

Returns a URL to the image



110
111
112
# File 'lib/ink_file_picker/client.rb', line 110

def retrieve_url(handle_or_url, params = {}, policy_attributes = {})
  generate_url handle_or_url, params, policy_attributes, call: 'read'
end

#stat(handle_or_url, params = {}, policy_attributes = {}) ⇒ Object

Public: Returns short stat for a file

handle_or_url - The handle or URL to the file params - Request params, like true, height: true to get width and height info. May be empty for default response policy_attributes - If you use security policies you may send in for instance (Time.now + 60 * 10) here

Returns hash of headers returned from file picker or false if request was unsuccessful



73
74
75
76
77
# File 'lib/ink_file_picker/client.rb', line 73

def stat(handle_or_url, params = {}, policy_attributes = {})
  response = http_connection.get stat_url(handle_or_url, params, policy_attributes)

  wrap_response_or_fail_unless_success! response
end

#stat_url(handle_or_url, params, policy_attributes = {}) ⇒ Object

Public: Generates a stat URL for a given file

handle_or_url - The handle or URL to the file policy_attributes - If you use security policies you may send in for instance (Time.now + 60 * 10) here

Returns a URL to the image you can do a HEAD request to in order to get stats



121
122
123
# File 'lib/ink_file_picker/client.rb', line 121

def stat_url(handle_or_url, params, policy_attributes = {})
  generate_url handle_or_url, params, policy_attributes, call: 'stat', url_action: 'metadata'
end

#store_file(file_or_path, content_type, filename = nil, policy_attributes = {}) ⇒ Object

Public: Store a file from given local file or path.

file_or_path - File or path to file content_type - The file’s content type filename - The file’s name, optional policy_attributes - If you use security policies you may send in for instance (Time.now + 60 * 10) here

Returns a hash representing the response where you can read for instance ‘url’



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ink_file_picker/client.rb', line 39

def store_file(file_or_path, content_type, filename = nil, policy_attributes = {})
  file_upload = Faraday::UploadIO.new file_or_path, content_type, filename
  params = {key: configuration.key}

  add_policy_to params, from: policy_attributes, ensure_included: {call: 'store'}

  response = http_connection.post configuration.store_path do |request|
    request.params = params
    request.body = {fileUpload: file_upload}
  end

  wrap_response_or_fail_unless_success! response
end

#store_url(url, policy_attributes = {}) ⇒ Object

Public: Store a file from given URL.

url - URL to resource policy_attributes - If you use security policies you may send in for instance (Time.now + 60 * 10) here

Returns a hash representing the response where you can read for instance ‘url’



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ink_file_picker/client.rb', line 18

def store_url(url, policy_attributes = {})
  params = {key: configuration.key}

  add_policy_to params, from: policy_attributes, ensure_included: {call: 'store'}

  response = http_connection.post configuration.store_path do |request|
    request.params = params
    request.body = {url: url}
  end

  wrap_response_or_fail_unless_success! response
end