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 diff --git a/README.md b/README.md index 8b94b3d..e12af3c 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`, `all_queued`, `processing`, or `done` ``` ####Fetching the response of the requested screenshots ``` ruby -client.screenshots request_id +client.screenshots(request_id) ``` 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..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 = ["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 = "" +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($/) - 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.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..dfa984a 100644 --- a/lib/screenshot/client.rb +++ b/lib/screenshot/client.rb @@ -1,121 +1,111 @@ module Screenshot - class Client - - API = "https://www.browserstack.com/screenshots" - - def initialize(options={}) - options = symbolize_keys options + 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! 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 self end - def get_os_and_browsers - res = http_get_request :extend_uri => "browsers.json" - parse res + def os_and_browsers + response = http_get_request(extend_uri: 'browsers.json') + parse(response) 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 = {}) + 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 + def screenshots_done?(job_id) + screenshots_status(job_id) == 'done' 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) + job_details = fetch_job_details(job_id) + job_details[:state] end - def screenshots job_id - res = http_get_request :extend_uri => "#{job_id}.json" - responseJson = parse res - responseJson[:screenshots] + def screenshots(job_id) + 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.path = uri.path + "/#{options[:extend_uri].to_s}" if options[:extend_uri] - req = Net::HTTP::Get.new uri.request_uri - make_request req, options, uri + def http_get_request(options = {}, uri = API) + uri = URI.parse uri + uri.path = uri.path + "/#{options[:extend_uri]}" if options[:extend_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, initheader = {'Content-Type' =>'application/json'} - req.body = options[:data] if options[:data] - make_request req, options, uri + def http_post_request(options = {}, uri = API) + 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 - req["Authorization"] = @authentication - 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 http_response_code_check res - case res.code.to_i + def add_authentication(req) + req['Authorization'] = @authentication + end + + 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 - raise AuthenticationError, encode({:code => res.code, :body => res.body}) + fail AuthenticationError, error_message when 403 - raise ScreenshotNotAllowedError, encode({:code => res.code, :body => res.body}) + fail ScreenshotNotAllowed, error_message when 422 - raise InvalidRequestError, encode({:code => res.code, :body => res.body}) + fail InvalidRequest, error_message else - raise 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 = 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} - end - - end #Client - - class AuthenticationError < StandardError - end - - class InvalidRequestError < StandardError - end - - class ScreenshotNotAllowedError < StandardError - end - - class UnexpectedError < StandardError - end - -end #Screenshots + end # Client +end # Screenshot diff --git a/lib/screenshot/version.rb b/lib/screenshot/version.rb index fd515d1..cb6c500 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.1.0' +end