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.1"
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`.
36 37 38 |
# File 'lib/htmlcsstoimage.rb', line 36 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
56 57 58 59 60 61 |
# File 'lib/htmlcsstoimage.rb', line 56 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.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/htmlcsstoimage.rb', line 86 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.generate).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
147 148 149 150 151 152 |
# File 'lib/htmlcsstoimage.rb', line 147 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
68 69 70 71 72 73 74 |
# File 'lib/htmlcsstoimage.rb', line 68 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
124 125 126 127 |
# File 'lib/htmlcsstoimage.rb', line 124 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
114 115 116 117 118 119 |
# File 'lib/htmlcsstoimage.rb', line 114 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 |