Class: Resizing::Client

Inherits:
Object
  • Object
show all
Includes:
Configurable, Constants, HttpClientable
Defined in:
lib/resizing/client.rb

Overview

Client class for Resizing

– usage.

options = {
  image_host: 'https://img.resizing.net',
  video_host: 'https://video.resizing.net',
  project_id: '098a2a0d-0000-0000-0000-000000000000',
  secret_token: '4g1cshg......rbs6'
}
client = Resizing::Client.new(options)
file = File.open('sample.jpg', 'r')
response = client.post(file, content_type: 'image/jpeg')
{
  "id"=>"fde443bb-0b29-4be2-a04e-2da8f19716ac",
  "project_id"=>"098a2a0d-0000-0000-0000-000000000000",
  "content_type"=>"image/jpeg",
  "latest_version_id"=>"Ot0NL4rptk6XxQNFP2kVojn5yKG44cYH",
  "latest_etag"=>"\"069ec178a367089c3f0306dd716facf2\"",
  "created_at"=>"2020-05-17T15:02:30.548Z",
  "updated_at"=>"2020-05-17T15:02:30.548Z"
}

++

Constant Summary

Constants included from Constants

Resizing::Constants::HTTP_STATUS_CREATED, Resizing::Constants::HTTP_STATUS_NOT_FOUND, Resizing::Constants::HTTP_STATUS_OK

Instance Method Summary collapse

Methods included from HttpClientable

#handle_faraday_error, #handle_timeout_error, #http_client

Methods included from Configurable

included, #initialize_config

Constructor Details

#initialize(*attrs) ⇒ Client

Returns a new instance of Client.



32
33
34
# File 'lib/resizing/client.rb', line 32

def initialize(*attrs)
  initialize_config(*attrs)
end

Instance Method Details

#delete(image_id) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/resizing/client.rb', line 84

def delete(image_id)
  url = build_delete_url(image_id)

  response = handle_faraday_error do
    http_client.delete(url) do |request|
      request.headers['X-ResizingToken'] = config.generate_auth_header
    end
  end

  result = handle_delete_response(response)
  result
end

#get(image_id) ⇒ Object

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/resizing/client.rb', line 36

def get(image_id)
  raise NotImplementedError
end

#metadata(image_id, options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/resizing/client.rb', line 97

def (image_id, options = {})
  url = (image_id)

  response = handle_faraday_error do
    http_client.get(url) do |request|
      request.headers['X-ResizingToken'] = config.generate_auth_header
    end
  end

  result = (response)
  result
end

#post(file_or_binary, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/resizing/client.rb', line 40

def post(file_or_binary, options = {})
  ensure_content_type(options)

  url = build_post_url

  filename = gather_filename file_or_binary, options

  body = to_io(file_or_binary)
  params = {
    image: Faraday::UploadIO.new(body, options[:content_type], filename)
  }

  response = handle_faraday_error do
    http_client.post(url, params) do |request|
      request.headers['X-ResizingToken'] = config.generate_auth_header
    end
  end

  result = handle_create_response(response)
  result
end

#put(image_id, file_or_binary, options) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/resizing/client.rb', line 62

def put(image_id, file_or_binary, options)
  ensure_content_type(options)

  url = build_put_url(image_id)

  filename = gather_filename file_or_binary, options

  body = to_io(file_or_binary)
  params = {
    image: Faraday::UploadIO.new(body, options[:content_type], filename)
  }

  response = handle_faraday_error do
    http_client.put(url, params) do |request|
      request.headers['X-ResizingToken'] = config.generate_auth_header
    end
  end

  result = handle_create_response(response)
  result
end