Class: HTMLCSSToImage
- Inherits:
-
Object
- Object
- HTMLCSSToImage
- Includes:
- HTTParty
- Defined in:
- lib/htmlcsstoimage.rb,
lib/htmlcsstoimage/version.rb
Defined Under Namespace
Classes: ApiResponse
Constant Summary collapse
- SIGNED_URL_TEMPLATE =
Addressable::Template.new("https://hcti.io/v1/image/{template_id}/{signed_token}{/format*}{?query*}")
- VERSION =
"0.1.4"
Instance Method Summary collapse
-
#create_image(html, params = {}) ⇒ HTMLCSSToImage::ApiResponse
Converts HTML/CSS to an image with the API.
-
#create_image_from_template(template_id, template_values = {}, params = {}) ⇒ Object
Creates a signed URL for generating an image from a template This URL contains the template_values in it.
-
#create_template(html, params = {}) ⇒ HTMLCSSToImage::ApiResponse
Creates an image template.
-
#delete_image(image_id) ⇒ Object
Deletes an image.
-
#initialize(user_id: ENV["HCTI_USER_ID"], api_key: ENV["HCTI_API_KEY"]) ⇒ HTMLCSSToImage
constructor
Creates an instance of HTMLCSSToImage with API credentials.
-
#templates(params = {}) ⇒ Object
Retrieves all available templates.
-
#url_to_image(url, params = {}) ⇒ Object
Generate a screenshot of a URL.
Constructor Details
#initialize(user_id: ENV["HCTI_USER_ID"], api_key: ENV["HCTI_API_KEY"]) ⇒ HTMLCSSToImage
Creates an instance of HTMLCSSToImage with API credentials.
If credentials are not provided, will try to use environment variables.
`HCTI_USER_ID` and `HCTI_API_KEY`.
37 38 39 |
# File 'lib/htmlcsstoimage.rb', line 37 def initialize(user_id: ENV["HCTI_USER_ID"], api_key: ENV["HCTI_API_KEY"]) @auth = { username: user_id, password: api_key } end |
Instance Method Details
#create_image(html, params = {}) ⇒ HTMLCSSToImage::ApiResponse
Converts HTML/CSS to an image with the API
57 58 59 60 61 62 |
# File 'lib/htmlcsstoimage.rb', line 57 def create_image(html, params = {}) body = { html: html }.merge(params).to_json = { basic_auth: @auth, body: body, query: { includeId: true } } self.class.post("/v1/image", ) end |
#create_image_from_template(template_id, template_values = {}, params = {}) ⇒ Object
Creates a signed URL for generating an image from a template This URL contains the template_values in it. It is signed with HMAC so that it cannot be changed by anyone without the API Key.
Does not make any network requests.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/htmlcsstoimage.rb', line 87 def create_image_from_template(template_id, template_values = {}, params = {}) template = SIGNED_URL_TEMPLATE.({ template_id: template_id, query: template_values }) query = Addressable::URI.parse(template.(signed_token: nil).to_s).query digest = OpenSSL::Digest.new('sha256') signed_token = OpenSSL::HMAC.hexdigest(digest, @auth[:password], CGI.unescape(query)) url = template.({ signed_token: signed_token }).to_s ApiResponse.new(url: url) end |
#create_template(html, params = {}) ⇒ HTMLCSSToImage::ApiResponse
Creates an image template
149 150 151 152 153 154 |
# File 'lib/htmlcsstoimage.rb', line 149 def create_template(html, params = {}) body = { html: html }.merge(params).to_json = { basic_auth: @auth, body: body } self.class.post("/v1/template", ) end |
#delete_image(image_id) ⇒ Object
Deletes an image
69 70 71 72 73 74 75 |
# File 'lib/htmlcsstoimage.rb', line 69 def delete_image(image_id) response = self.class.delete("/v1/image/#{image_id}", basic_auth: @auth) return true if response.success? response end |
#templates(params = {}) ⇒ Object
Retrieves all available templates
126 127 128 129 |
# File 'lib/htmlcsstoimage.rb', line 126 def templates(params = {}) = params.merge({ basic_auth: @auth }) self.class.get("/v1/template", ) end |
#url_to_image(url, params = {}) ⇒ Object
Generate a screenshot of a URL
116 117 118 119 120 121 |
# File 'lib/htmlcsstoimage.rb', line 116 def url_to_image(url, params = {}) body = { url: url }.merge(params).to_json = { basic_auth: @auth, body: body, query: { includeId: true } } self.class.post("/v1/image", ) end |