Class: Browshot

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = '', base = 'https://api.browshot.com/api/v1/', debug = false) ⇒ Browshot

New client

Parameters:

  • key (String) (defaults to: '')

    API key

  • base (String) (defaults to: 'https://api.browshot.com/api/v1/')

    Base URL for all API requests. You should use the default base provided by the library. Be careful if you decide to use HTTP instead of HTTPS as your API key could be sniffed and your account could be used without your consent.

  • debug (Boolean) (defaults to: false)

    Set to true to print debug output to the standard output. false (disabled) by default.



33
34
35
36
37
# File 'lib/browshot.rb', line 33

def initialize(key='', base='https://api.browshot.com/api/v1/', debug=false)
	@key = key || ''
	@base = base || 'http://127.0.0.1:3000/api/v1/'
	@debug = debug || false
end

Instance Attribute Details

#baseObject (readonly)

Base URL for all API requests. You should use the default base provided by the library. Be careful if you decide to use HTTP instead of HTTPS as your API key could be sniffed and your account could be used without your consent.



23
24
25
# File 'lib/browshot.rb', line 23

def base
  @base
end

#debugObject (readonly)

print debug output to the standard output



26
27
28
# File 'lib/browshot.rb', line 26

def debug
  @debug
end

#keyObject (readonly)

API key



20
21
22
# File 'lib/browshot.rb', line 20

def key
  @key
end

Instance Method Details

#account_info(parameters = {}) ⇒ Object

Return information about the user account. See https://browshot.com/api/documentation#account_info for the response format.



258
259
260
# File 'lib/browshot.rb', line 258

def (parameters={})
	return return_reply('account/info', parameters)
end

#api_versionObject

Return the API version handled by the library. Note that this library can usually handle new arguments in requests without requiring an update.



40
41
42
# File 'lib/browshot.rb', line 40

def api_version()
	return "1.14"
end

#batch_create(id = 0, file = '', parameters = {}) ⇒ Object

Request multiple screenshots from a text file. See browshot.com/api/documentation#batch_create for the response format.

See browshot.com/api/documentation#batch_create for the full list of possible arguments.

Parameters:

  • id (Integer) (defaults to: 0)

    Instance ID

  • file (String) (defaults to: '')

    Path to the text file which contains the list of URLs



238
239
240
241
242
243
# File 'lib/browshot.rb', line 238

def batch_create(id=0, file='', parameters={})
	parameters[:id] = id
	parameters[:file] = file

	return return_post_reply('batch/create', parameters)
end

#batch_info(id = 0, parameters = {}) ⇒ Object

Get information about a screenshot batch requested previously. See https://browshot.com/api/documentation#batch_info for the response format.

See browshot.com/api/documentation#batch_info for the full list of possible arguments.

Parameters:

  • id (Integer) (defaults to: 0)

    Batch ID



250
251
252
253
254
# File 'lib/browshot.rb', line 250

def batch_info(id=0,  parameters={})
	parameters[:id] = id

	return return_reply('batch/info', parameters)
end

#browser_create(parameters = {}) ⇒ Object

Create a custom browser. See browshot.com/api/documentation#browser_create for the response format.



108
109
110
# File 'lib/browshot.rb', line 108

def browser_create(parameters={})
	return return_reply('browser/create', parameters)
end

#browser_info(id = 0) ⇒ Object

Return the details of a browser. See browshot.com/api/documentation#browser_info for the response format.

Parameters:

  • id (Integer) (defaults to: 0)

    Browser ID



103
104
105
# File 'lib/browshot.rb', line 103

def browser_info(id=0)
	return return_reply('browser/info', { 'id' => id })
end

#browser_listObject

Return the list of browsers. See browshot.com/api/documentation#browser_list for the response format.



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

def browser_list()
	return return_reply('browser/list')
end

#instance_create(parameters = {}) ⇒ Object

Create a private instance. See browshot.com/api/documentation#instance_create for the response format.



91
92
93
# File 'lib/browshot.rb', line 91

def instance_create(parameters={})
	return return_reply('instance/create', parameters)
end

#instance_info(id = 0) ⇒ Object

Return the details of an instance. See browshot.com/api/documentation#instance_info for the response format.

Parameters:

  • id (Integer) (defaults to: 0)

    Instance ID



86
87
88
# File 'lib/browshot.rb', line 86

def instance_info(id=0)
	return return_reply('instance/info', { 'id' => id })
end

#instance_listObject

Return the list of instances. See browshot.com/api/documentation#instance_list for the response format.



79
80
81
# File 'lib/browshot.rb', line 79

def instance_list()
	return return_reply('instance/list')
end

#screenshot_create(url = '', parameters = {}) ⇒ Object

Request a screenshot. See browshot.com/api/documentation#screenshot_create for the response format.

Parameters:

  • url (String) (defaults to: '')

    URL of the website to create a screenshot of.



115
116
117
118
# File 'lib/browshot.rb', line 115

def screenshot_create(url='', parameters={})
	parameters[:url] = url
	return return_reply('screenshot/create', parameters)
end

#screenshot_delete(id = 0, parameters = {}) ⇒ Object

Delete details of a screenshot. See browshot.com/api/documentation#screenshot_delete for the response format.

Parameters:

  • id (Integer) (defaults to: 0)

    screenshot ID



176
177
178
179
# File 'lib/browshot.rb', line 176

def screenshot_delete(id=0, parameters={})
	parameters[:id] = id
	return return_reply('screenshot/delete', parameters)
end

#screenshot_host(id = 0, hosting = 'browshot', parameters = {}) ⇒ Object

Hot a screenshot or thumbnail. See browshot.com/api/documentation#screenshot_host for the response format.

Parameters:

  • id (Integer) (defaults to: 0)

    screenshot ID

  • hosting ('s3', 'browshot') (defaults to: 'browshot')

    hosting option: s3 or browshot



159
160
161
162
163
# File 'lib/browshot.rb', line 159

def screenshot_host(id=0, hosting='browshot', parameters={})
	parameters[:id] = id
	parameters[:hosting] = hosting
	return return_reply('screenshot/host', parameters)
end

#screenshot_html(id = 0, parameters = {}) ⇒ Object

Get the HTML code of the rendered page. See browshot.com/api/documentation#screenshot_html for the response format.

See browshot.com/api/documentation#screenshot_html for the full list of possible arguments.

Parameters:

  • id (Integer) (defaults to: 0)

    screenshot ID



214
215
216
217
218
# File 'lib/browshot.rb', line 214

def screenshot_html(id=0, parameters={})
	parameters[:id] = id

	return return_string('screenshot/html', parameters)
end

#screenshot_info(id = 0, parameters = {}) ⇒ Object

Get information about a screenshot requested previously. See browshot.com/api/documentation#screenshot_info for the response format.

Parameters:

  • id (Integer) (defaults to: 0)

    screenshot ID



123
124
125
126
# File 'lib/browshot.rb', line 123

def screenshot_info(id=0, parameters={})
	parameters[:id] = id
	return return_reply('screenshot/info', parameters)
end

#screenshot_list(parameters = {}) ⇒ Object

Get details about screenshots requested. See browshot.com/api/documentation#screenshot_list for the response format.



129
130
131
# File 'lib/browshot.rb', line 129

def screenshot_list(parameters={})
	return return_reply('screenshot/list', parameters)
end

#screenshot_multiple(url = '', parameters = {}) ⇒ Object

Request multiple screenshots. See browshot.com/api/documentation#screenshot_multiple for the response format.

See browshot.com/api/documentation#screenshot_multiple for the full list of possible arguments.

Parameters:

  • url (String) (defaults to: '')

    URL of the website to create a screenshot of



226
227
228
229
230
# File 'lib/browshot.rb', line 226

def screenshot_multiple(url='', parameters={})
	parameters[:url] = url

	return return_reply('screenshot/multiple', parameters)
end

#screenshot_search(url = '', parameters = {}) ⇒ Object

Get details about screenshots requested. See browshot.com/api/documentation#screenshot_search for the response format.

Parameters:

  • url (String) (defaults to: '')

    URL string to match



184
185
186
187
# File 'lib/browshot.rb', line 184

def screenshot_search(url='', parameters={})
	parameters[:url] = url
	return return_reply('screenshot/search', parameters)
end

#screenshot_share(id = 0, parameters = {}) ⇒ Object

Share a screenshot. See browshot.com/api/documentation#screenshot_share for the response format.

Parameters:

  • id (Integer) (defaults to: 0)

    screenshot ID



168
169
170
171
# File 'lib/browshot.rb', line 168

def screenshot_share(id=0, parameters={})
	parameters[:id] = id
	return return_reply('screenshot/share', parameters)
end

#screenshot_thumbnail(id = 0, parameters = {}) ⇒ Object

Retrieve the screenshot, or a thumbnail. See browshot.com/api/documentation#screenshot_thumbnails for the response format.

Parameters:

  • id (Integer) (defaults to: 0)

    screenshot ID



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/browshot.rb', line 137

def screenshot_thumbnail(id=0, parameters={})
	parameters[:id] = id

	begin
           url = make_url('screenshot/thumbnail', parameters)
           response = fetch(url.to_s)
           case response
               when Net::HTTPSuccess     then 
                   return response.response.body
               else
                   return ''
           end
       rescue Exception => e
           puts "{e.message}" if (@debug)
           raise e
       end
end

#screenshot_thumbnail_file(id = 0, file = '', parameters = {}) ⇒ Object

Retrieve the screenshot, or a thumbnail, and save it to a file. See browshot.com/api/documentation#screenshot_thumbnails for the response format.

See browshot.com/api/documentation#screenshot_thumbnails for the full list of possible arguments.

Parameters:

  • id (Integer) (defaults to: 0)

    screenshot ID

  • file (String) (defaults to: '')

    Local file name to write to.



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/browshot.rb', line 196

def screenshot_thumbnail_file(id=0, file='', parameters={})
	content = screenshot_thumbnail(id, parameters);

	if (content != '')
		File.open(file, 'w') {|f| f.write(content) }
		return file
	else
		puts "No thumbnail retrieved\n" if (@debug)
		return ''
	end
end

#simple(parameters = {}) ⇒ Array<Symbol, Symbol>

Retrieve a screenshot with one call. See https://browshot.com/api/documentation#simple for the full list of possible arguments.

Returns:

  • (Array<Symbol, Symbol>)

    {:code => 200, :png => <content>} in case of success



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/browshot.rb', line 47

def simple(parameters={})
    begin
        url = make_url('simple', parameters)
        response = fetch(url.to_s)
        case response
            when Net::HTTPSuccess     then 
                return {:code => response.code, :png => response.response.body}
            else
                return {:code => response.code, :png => ''}
        end
    rescue Exception => e
        puts "{e.message}" if (@debug)
        raise e
    end
end

#simple_file(file = '', parameters = {}) ⇒ Array<Symbol, Symbol>

Save a screenshot to a file with one call, and save it to a file. See https://browshot.com/api/documentation#simple for the full list of possible arguments.

Parameters:

  • file (String) (defaults to: '')

    Local file name to write to.

  • parameters (Array<Symbol, Symbol>) (defaults to: {})

    Additional options

Returns:

  • (Array<Symbol, Symbol>)

    {:code => 200, :file => <file_name>} in case of success



68
69
70
71
72
73
74
75
76
# File 'lib/browshot.rb', line 68

def simple_file(file='', parameters={})
    data = self.simple(parameters)
    if (data[:png].length > 0)
        File.open(file, 'w') {|f| f.write(data[:png]) }
        return {:code => data[:code], :file => file}
    else
        return {:code => data[:code], :file => ''}
    end
end