From cc7c02dd71a63001da9b666f047a9cb6a7e0767f Mon Sep 17 00:00:00 2001 From: Ron Smith Date: Wed, 1 Jul 2015 13:38:22 -0500 Subject: [PATCH 1/4] Update ruby version to 2.2.2 --- .ruby-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ruby-version b/.ruby-version index fc373ec..b1b25a5 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -ruby-1.8.7 +2.2.2 From b9325c932c4e757214a86f81a93fe72a5673d29d Mon Sep 17 00:00:00 2001 From: Ron Smith Date: Wed, 1 Jul 2015 13:38:33 -0500 Subject: [PATCH 2/4] Rubocop autofixes --- Rakefile | 2 +- browserstack-screenshot.gemspec | 22 ++++---- lib/screenshot.rb | 12 ++--- lib/screenshot/client.rb | 92 ++++++++++++++++----------------- lib/screenshot/version.rb | 4 +- 5 files changed, 65 insertions(+), 67 deletions(-) diff --git a/Rakefile b/Rakefile index 2995527..c702cfc 100644 --- a/Rakefile +++ b/Rakefile @@ -1 +1 @@ -require "bundler/gem_tasks" +require 'bundler/gem_tasks' diff --git a/browserstack-screenshot.gemspec b/browserstack-screenshot.gemspec index 88ccd33..c77c168 100644 --- a/browserstack-screenshot.gemspec +++ b/browserstack-screenshot.gemspec @@ -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 diff --git a/lib/screenshot.rb b/lib/screenshot.rb index 1ea8403..536dfdb 100644 --- a/lib/screenshot.rb +++ b/lib/screenshot.rb @@ -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' diff --git a/lib/screenshot/client.rb b/lib/screenshot/client.rb index 06f0c95..368ab84 100644 --- a/lib/screenshot/client.rb +++ b/lib/screenshot/client.rb @@ -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" + res = http_get_request extend_uri: 'browsers.json' parse res end - - def generate_screenshots configHash={} - res = http_post_request :data => Yajl::Encoder.encode(configHash) + + def generate_screenshots(configHash = {}) + res = http_post_request data: Yajl::Encoder.encode(configHash) responseJson = parse res request = responseJson[: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" + def screenshots_status(job_id) + res = http_get_request extend_uri: "#{job_id}.json" responseJson = parse res responseJson[:state] - end + end - def screenshots job_id - res = http_get_request :extend_uri => "#{job_id}.json" + def screenshots(job_id) + res = http_get_request extend_uri: "#{job_id}.json" responseJson = parse res responseJson[: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, initheader = { '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 @@ -70,29 +70,29 @@ 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 @@ -100,22 +100,20 @@ 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 diff --git a/lib/screenshot/version.rb b/lib/screenshot/version.rb index fd515d1..6897172 100644 --- a/lib/screenshot/version.rb +++ b/lib/screenshot/version.rb @@ -1,3 +1,3 @@ module Screenshot - VERSION = "0.0.2" -end \ No newline at end of file + VERSION = '0.0.2' +end From 806c5ac728cedc1e5a5d549753cbb76cba12abfa Mon Sep 17 00:00:00 2001 From: Ron Smith Date: Wed, 1 Jul 2015 13:55:33 -0500 Subject: [PATCH 3/4] Manual rubocop-suggested fixes, bump version for method change --- README.md | 42 +++++++++++++++++++-------------------- lib/screenshot/client.rb | 22 ++++++++++---------- lib/screenshot/version.rb | 2 +- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 8b94b3d..c10b0a1 100644 --- a/README.md +++ b/README.md @@ -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) ``` @@ -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 @@ -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) ``` diff --git a/lib/screenshot/client.rb b/lib/screenshot/client.rb index 368ab84..8617d8b 100644 --- a/lib/screenshot/client.rb +++ b/lib/screenshot/client.rb @@ -12,15 +12,15 @@ def initialize(options = {}) self end - def get_os_and_browsers + 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) @@ -29,14 +29,14 @@ def screenshots_done?(job_id) def screenshots_status(job_id) res = http_get_request extend_uri: "#{job_id}.json" - responseJson = parse res - responseJson[:state] - end + 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] + response_json = parse res + response_json[:screenshots] end private @@ -54,7 +54,7 @@ def http_get_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 diff --git a/lib/screenshot/version.rb b/lib/screenshot/version.rb index 6897172..cb6c500 100644 --- a/lib/screenshot/version.rb +++ b/lib/screenshot/version.rb @@ -1,3 +1,3 @@ module Screenshot - VERSION = '0.0.2' + VERSION = '0.1.0' end From 0d65b01d6122e13723bf1b015dfa489e46aba92b Mon Sep 17 00:00:00 2001 From: Ron Smith Date: Wed, 1 Jul 2015 21:15:33 -0500 Subject: [PATCH 4/4] Refactor screenshot client --- README.md | 6 +- browserstack-screenshot.gemspec | 30 +++++----- lib/screenshot/client.rb | 102 +++++++++++++++----------------- 3 files changed, 65 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index c10b0a1..e12af3c 100644 --- a/README.md +++ b/README.md @@ -60,8 +60,8 @@ params = { 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"} + { os: "Windows", os_version: "7", browser: "ie", browser_version: "8.0" }, + { os: "Windows", os_version: "XP", browser: "ie", browser_version: "7.0" } ] } ``` @@ -87,7 +87,7 @@ 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`, `all_queued`, `processing`, or `done` ``` ####Fetching the response of the requested screenshots diff --git a/browserstack-screenshot.gemspec b/browserstack-screenshot.gemspec index c77c168..5d4b63d 100644 --- a/browserstack-screenshot.gemspec +++ b/browserstack-screenshot.gemspec @@ -3,21 +3,21 @@ lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'screenshot/version' -Gem::Specification.new do |spec| - spec.name = 'browserstack-screenshot' - spec.version = Screenshot::VERSION - 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 = '' +Gem::Specification.new do |gem| + gem.name = 'browserstack-screenshot' + gem.version = Screenshot::VERSION + gem.authors = %w(ahmed1490 utsavkesharwani) + gem.email = ['ahmed1490@gmail.com', 'utsav.kesharwani@gmail.com'] + gem.description = 'Ruby wrapper for Browserstack screenshots API' + gem.summary = 'Get screenshots from live browsers using this gem' + gem.homepage = 'https://github.com/browserstack/ruby-screenshots' + gem.license = '' - 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'] + gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) + gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) } + gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) + gem.require_paths = ['lib'] - spec.add_development_dependency 'rake' - spec.add_dependency('yajl-ruby', '1.1.0') + gem.add_development_dependency 'rake' + gem.add_runtime_dependency('yajl-ruby') end diff --git a/lib/screenshot/client.rb b/lib/screenshot/client.rb index 8617d8b..dfa984a 100644 --- a/lib/screenshot/client.rb +++ b/lib/screenshot/client.rb @@ -1,96 +1,104 @@ module Screenshot + AuthenticationError = Class.new(StandardError) + InvalidRequest = Class.new(StandardError) + ScreenshotNotAllowed = Class.new(StandardError) + UnexpectedError = Class.new(StandardError) + class Client API = 'https://www.browserstack.com/screenshots' def initialize(options = {}) - options = symbolize_keys options + options.symbolize_keys! unless options[:username] && options[:password] fail 'Expecting Parameters: username and password in the options Hash!' end @authentication = 'Basic ' + Base64.encode64("#{options[:username]}:#{options[:password]}").strip - # authenticate options, AUTH_URI self end def os_and_browsers - res = http_get_request extend_uri: 'browsers.json' - parse res + response = http_get_request(extend_uri: 'browsers.json') + parse(response) end def generate_screenshots(config_hash = {}) - res = http_post_request data: Yajl::Encoder.encode(config_hash) - response_json = parse res + response = http_post_request(data: Yajl::Encoder.encode(config_hash)) + response_json = parse(response) response_json[:job_id] end def screenshots_done?(job_id) - (screenshots_status job_id) == 'done' ? true : false + screenshots_status(job_id) == 'done' end def screenshots_status(job_id) - res = http_get_request extend_uri: "#{job_id}.json" - response_json = parse res - response_json[:state] + job_details = fetch_job_details(job_id) + job_details[:state] end def screenshots(job_id) - res = http_get_request extend_uri: "#{job_id}.json" - response_json = parse res - response_json[:screenshots] + job_details = fetch_job_details(job_id) + job_details[:screenshots] end private - def authenticate(options, uri = API) - http_get_request options, uri - end - def http_get_request(options = {}, uri = API) - uri = URI.parse uri if uri + uri = URI.parse 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 + request = Net::HTTP::Get.new uri.request_uri + make_request(uri, request) end def http_post_request(options = {}, uri = API) - uri = URI.parse uri if uri - req = Net::HTTP::Post.new uri.request_uri, 'Content-Type' => 'application/json' - req.body = options[:data] if options[:data] - make_request req, options, uri + uri = URI.parse uri + request = Net::HTTP::Post.new uri.request_uri, 'Content-Type' => 'application/json' + request.body = options[:data] if options[:data] + make_request(uri, request) end - def make_request(req, options = {}, uri = API) + def setup_connection(uri) conn = Net::HTTP.new uri.host, uri.port conn.use_ssl = uri.scheme == 'https' conn.verify_mode = OpenSSL::SSL::VERIFY_PEER conn.cert_store = OpenSSL::X509::Store.new conn.cert_store.set_default_paths - add_authentication options, req - res = conn.request req - http_response_code_check res - res + conn end - def add_authentication(_options, req) + def make_request(uri, request) + connection = setup_connection(uri) + add_authentication(request) + response = connection.request(request) + http_response_code_check(response) + response + end + + def add_authentication(req) req['Authorization'] = @authentication - req end - def http_response_code_check(res) - case res.code.to_i + def http_response_code_check(response) + error_message = encode(code: response.code, body: response.body) + case response.code.to_i when 200 - res + response when 401 - fail AuthenticationError, encode(code: res.code, body: res.body) + fail AuthenticationError, error_message when 403 - fail ScreenshotNotAllowedError, encode(code: res.code, body: res.body) + fail ScreenshotNotAllowed, error_message when 422 - fail InvalidRequestError, encode(code: res.code, body: res.body) + fail InvalidRequest, error_message else - fail UnexpectedError, encode(code: res.code, body: res.body) + fail UnexpectedError, error_message end end + def fetch_job_details(job_id) + response = http_get_request(extend_uri: "#{job_id}.json") + parse(response) + end + def parse(response) parser = Yajl::Parser.new(symbolize_keys: true) parser.parse(response.body) @@ -99,21 +107,5 @@ def parse(response) def encode(hash) Yajl::Encoder.encode(hash) end - - def symbolize_keys(hash) - hash.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo } - end end # Client - - class AuthenticationError < StandardError - end - - class InvalidRequestError < StandardError - end - - class ScreenshotNotAllowedError < StandardError - end - - class UnexpectedError < StandardError - end -end # Screenshots +end # Screenshot