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" }

++ rubocop:disable Metrics/ClassLength

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.



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

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

Instance Method Details

#delete(image_id) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/resizing/client.rb', line 79

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

  handle_delete_response(response)
end

#get(image_id) ⇒ Object

Raises:

  • (NotImplementedError)


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

def get(image_id)
  raise NotImplementedError
end

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



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/resizing/client.rb', line 91

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

  (response, options)
end

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



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

def post(filename_or_io, options = {})
  ensure_content_type(options)
  ensure_filename_or_io(filename_or_io)
  filename = gather_filename filename_or_io, options

  url = build_post_url
  params = {
    image: Faraday::Multipart::FilePart.new(filename_or_io, 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

  handle_create_response(response)
end

#put(image_id, filename_or_io, options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/resizing/client.rb', line 60

def put(image_id, filename_or_io, options)
  ensure_content_type(options)
  ensure_filename_or_io(filename_or_io)
  filename = gather_filename filename_or_io, options

  url = build_put_url(image_id)
  params = {
    image: Faraday::Multipart::FilePart.new(filename_or_io, 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

  handle_create_response(response)
end