Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-1.8.7
2.2.2
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ require 'screenshot'
Creates a new client instance.

* `settings`: A hash of settings that apply to all requests for the new client.
* `:username`: The username for the BrowserStack account.
* `:password`: The password for the BrowserStack account.
* `username`: The username for the BrowserStack account.
* `password`: The password for the BrowserStack account.

``` ruby
settings = {:username => "foo", :password => "foobar"}
settings = { username: 'foo', password: 'foobar' }
client = Screenshot::Client.new(settings)
```

Expand All @@ -42,7 +42,7 @@ client = Screenshot::Client.new(settings)
Fetches all available browsers. [API info](http://www.browserstack.com/screenshots/api#list-os-browsers)

``` ruby
client.get_os_and_browsers #returns a hash
client.os_and_browsers # returns a hash
```

####Generating Screenshots
Expand All @@ -51,48 +51,48 @@ Frame the config object according to the format given. [Format info](http://www.
Eg settings object:
``` ruby
params = {
:url => "www.google.com",
:callback_url => "http://example.com/pingback_url",
:win_res => "1024x768", #Options : "1024x768", "1280x1024"
:mac_res => "1920x1080", #Options : "1024x768", "1280x960", "1280x1024", "1600x1200", "1920x1080"
:quality => "compressed", #Options : "compressed", "original"
:wait_time => 5, #Options: 2, 5, 10, 15, 20, 60
:orientation => "portrait", #Options: "portrait", "landscape"
:tunnel => false,
:browsers => [
{:os=>"Windows",:os_version=>"7",:browser=>"ie",:browser_version=>"8.0"},
{:os=>"Windows",:os_version=>"XP",:browser=>"ie",:browser_version=>"7.0"}
]
url: "www.google.com",
callback_url: "http://example.com/pingback_url",
win_res: "1024x768", # Options : "1024x768", "1280x1024"
mac_res: "1920x1080", # Options : "1024x768", "1280x960", "1280x1024", "1600x1200", "1920x1080"
quality: "compressed", # Options : "compressed", "original"
wait_time: 5, # Options : 2, 5, 10, 15, 20, 60
orientation: "portrait", # Options : "portrait", "landscape"
tunnel: false,
browsers: [
{ os: "Windows", os_version: "7", browser: "ie", browser_version: "8.0"},
{ os: "Windows", os_version: "XP", browser: "ie", browser_version: "7.0"}
]
}
```
`callback_url`, `win_res`, `mac_res`, `quality`, `wait_time`, `orientation` and `tunnel` being optional parameters.

#####For testing Local/Internal Server setup
* First setup local tunnel using the command line method as mentioned [here](http://www.browserstack.com/local-testing#setup)
* Pass `:tunnel => true` in the params object
* Pass `tunnel: true` in the params object



A request id is returned when a valid request is made.

``` ruby
request_id = client.generate_screenshots params
request_id = client.generate_screenshots(params)
```

####Checking/Polling the status of the request
Use this method to check if the requested screenshots are complete.
``` ruby
client.screenshots_done? request_id #returns `true` or `false`
client.screenshots_done?(request_id) # returns `true` or `false`
```

Or you can fetch the request state
``` ruby
client.screenshots_status request_id #returns `queue` or `processing` or `done`
client.screenshots_status(request_id) # returns `queue` or `processing` or `done`
```

####Fetching the response of the requested screenshots
``` ruby
client.screenshots request_id
client.screenshots(request_id)
```


Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require "bundler/gem_tasks"
require 'bundler/gem_tasks'
22 changes: 11 additions & 11 deletions browserstack-screenshot.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'screenshot/version'

Gem::Specification.new do |spec|
spec.name = "browserstack-screenshot"
spec.name = 'browserstack-screenshot'
spec.version = Screenshot::VERSION
spec.authors = ["ahmed1490", "utsavkesharwani"]
spec.email = ["ahmed1490@gmail.com", "utsav.kesharwani@gmail.com"]
spec.description = %q{Ruby wrapper for Browserstack screenshots API}
spec.summary = %q{Get screenshots from live browsers using this gem}
spec.homepage = "https://github.com/browserstack/ruby-screenshots"
spec.license = ""
spec.authors = %w(ahmed1490 utsavkesharwani)
spec.email = ['ahmed1490@gmail.com', 'utsav.kesharwani@gmail.com']
spec.description = 'Ruby wrapper for Browserstack screenshots API'
spec.summary = 'Get screenshots from live browsers using this gem'
spec.homepage = 'https://github.com/browserstack/ruby-screenshots'
spec.license = ''

spec.files = `git ls-files`.split($/)
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

spec.add_development_dependency "rake"
spec.add_dependency("yajl-ruby", "1.1.0")
spec.add_development_dependency 'rake'
spec.add_dependency('yajl-ruby', '1.1.0')
end
12 changes: 6 additions & 6 deletions lib/screenshot.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "base64"
require "net/https"
require "openssl"
require "yajl"
require "screenshot/client"
require "screenshot/version"
require 'base64'
require 'net/https'
require 'openssl'
require 'yajl'
require 'screenshot/client'
require 'screenshot/version'
104 changes: 51 additions & 53 deletions lib/screenshot/client.rb
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
module Screenshot
class Client

API = "https://www.browserstack.com/screenshots"

def initialize(options={})
class Client
API = 'https://www.browserstack.com/screenshots'

def initialize(options = {})
options = symbolize_keys options
unless options[:username] && options[:password]
raise "Expecting Parameters: username and password in the options Hash!"
fail 'Expecting Parameters: username and password in the options Hash!'
end
@authentication = "Basic " + Base64.encode64("#{options[:username]}:#{options[:password]}").strip
#authenticate options, AUTH_URI
@authentication = 'Basic ' + Base64.encode64("#{options[:username]}:#{options[:password]}").strip
# authenticate options, AUTH_URI
self
end

def get_os_and_browsers
res = http_get_request :extend_uri => "browsers.json"
def os_and_browsers
res = http_get_request extend_uri: 'browsers.json'
parse res
end
def generate_screenshots configHash={}
res = http_post_request :data => Yajl::Encoder.encode(configHash)
responseJson = parse res
request = responseJson[:job_id]

def generate_screenshots(config_hash = {})
res = http_post_request data: Yajl::Encoder.encode(config_hash)
response_json = parse res
response_json[:job_id]
end

def screenshots_done? job_id
(screenshots_status job_id) == "done" ? true : false
def screenshots_done?(job_id)
(screenshots_status job_id) == 'done' ? true : false
end

def screenshots_status job_id
res = http_get_request :extend_uri => "#{job_id}.json"
responseJson = parse res
responseJson[:state]
def screenshots_status(job_id)
res = http_get_request extend_uri: "#{job_id}.json"
response_json = parse res
response_json[:state]
end

def screenshots job_id
res = http_get_request :extend_uri => "#{job_id}.json"
responseJson = parse res
responseJson[:screenshots]
def screenshots(job_id)
res = http_get_request extend_uri: "#{job_id}.json"
response_json = parse res
response_json[:screenshots]
end

private
def authenticate options, uri=API

def authenticate(options, uri = API)
http_get_request options, uri
end

def http_get_request options={}, uri=API
def http_get_request(options = {}, uri = API)
uri = URI.parse uri if uri
uri.path = uri.path + "/#{options[:extend_uri].to_s}" if options[:extend_uri]
uri.path = uri.path + "/#{options[:extend_uri]}" if options[:extend_uri]
req = Net::HTTP::Get.new uri.request_uri
make_request req, options, uri
end

def http_post_request options={}, uri=API
def http_post_request(options = {}, uri = API)
uri = URI.parse uri if uri
req = Net::HTTP::Post.new uri.request_uri, initheader = {'Content-Type' =>'application/json'}
req = Net::HTTP::Post.new uri.request_uri, 'Content-Type' => 'application/json'
req.body = options[:data] if options[:data]
make_request req, options, uri
end

def make_request req, options={}, uri=API
def make_request(req, options = {}, uri = API)
conn = Net::HTTP.new uri.host, uri.port
conn.use_ssl = uri.scheme == 'https'
conn.verify_mode = OpenSSL::SSL::VERIFY_PEER
Expand All @@ -70,52 +70,50 @@ def make_request req, options={}, uri=API
http_response_code_check res
res
end
def add_authentication options, req
req["Authorization"] = @authentication

def add_authentication(_options, req)
req['Authorization'] = @authentication
req
end

def http_response_code_check res
def http_response_code_check(res)
case res.code.to_i
when 200
res
when 401
raise AuthenticationError, encode({:code => res.code, :body => res.body})
fail AuthenticationError, encode(code: res.code, body: res.body)
when 403
raise ScreenshotNotAllowedError, encode({:code => res.code, :body => res.body})
fail ScreenshotNotAllowedError, encode(code: res.code, body: res.body)
when 422
raise InvalidRequestError, encode({:code => res.code, :body => res.body})
fail InvalidRequestError, encode(code: res.code, body: res.body)
else
raise UnexpectedError, encode({:code => res.code, :body => res.body})
fail UnexpectedError, encode(code: res.code, body: res.body)
end
end

def parse(response)
parser = Yajl::Parser.new(:symbolize_keys => true)
parser = Yajl::Parser.new(symbolize_keys: true)
parser.parse(response.body)
end

def encode(hash)
Yajl::Encoder.encode(hash)
end

def symbolize_keys hash
hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
def symbolize_keys(hash)
hash.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo }
end
end # Client

class AuthenticationError < StandardError
end

end #Client

class AuthenticationError < StandardError
end
class InvalidRequestError < StandardError
end

class InvalidRequestError < StandardError
end

class ScreenshotNotAllowedError < StandardError
end

class UnexpectedError < StandardError
class UnexpectedError < StandardError
end

end #Screenshots
end # Screenshots
4 changes: 2 additions & 2 deletions lib/screenshot/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Screenshot
VERSION = "0.0.2"
end
VERSION = '0.1.0'
end