Class: Browshot

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

New client

Parameters:

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

    API key

  • debug (Boolean) (defaults to: false)

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

  • 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.



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

def initialize(key='', debug=false, base='https://api.browshot.com/api/v1/')
	@key = key || ''
	@base = base || 'https://api.browshot.com/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.



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

def base
  @base
end

#debugObject (readonly)

print debug output to the standard output



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

def debug
  @debug
end

#keyObject (readonly)

API key



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

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.



273
274
275
# File 'lib/browshot.rb', line 273

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.



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

def api_version()
	return "1.29"
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



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

def batch_create(id=0, file='', parameters={})
	parameters[:instance_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



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

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

	return return_reply('batch/info', 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



100
101
102
# File 'lib/browshot.rb', line 100

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.



93
94
95
# File 'lib/browshot.rb', line 93

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

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

Crawl a domain. See browshot.com/api/documentation#bcrawl_create for the response format.

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

Parameters:

  • id (Integer) (defaults to: 0)

    Instance ID

  • domain (String)

    Domain to crawl

  • url (String)

    URL to start with



252
253
254
255
256
257
258
# File 'lib/browshot.rb', line 252

def crawl_create(id=0, file='', parameters={})
	parameters[:instance_id] = id
	parameters[:domain] = domain
	parameters[:url] = url

	return return_reply('crawl/create', parameters)
end

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

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

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

Parameters:

  • id (Integer) (defaults to: 0)

    Crawl ID



265
266
267
268
269
# File 'lib/browshot.rb', line 265

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

	return return_reply('crawl/info', 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



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

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.



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

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.



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

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



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

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



152
153
154
155
156
# File 'lib/browshot.rb', line 152

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



207
208
209
210
211
# File 'lib/browshot.rb', line 207

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



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

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.



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

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

#screenshot_multiple(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.



217
218
219
# File 'lib/browshot.rb', line 217

def screenshot_multiple(parameters={})
	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



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

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



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

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/browshot.rb', line 130

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.



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/browshot.rb', line 189

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



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

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



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

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