Wednesday, 25 February 2015

class UserHelper 

 
def self.sd_sftp_user  SftpHelper.obj.connect_sftp FigNewton.sftp.mass.sftp_host,
                              FigNewton.sftp.mass.sftp_uname,
                              FigNewton.sftp.mass.sftp_key
  SftpHelper.obj
end
 
 
def self.dynamo_user  @dynamo_db = AWS::DynamoDB.new(access_key_id: FigNewton.dynamodb.access_key_id,
                                 secret_access_key: FigNewton.dynamodb.secret_access_key,
                                 region: FigNewton.dynamodb.region)
  @dynamo_dbend 

end


class Dynamo  include Singleton  def initialize 
 # @dynamo_db = AWS::DynamoDB.new(access_key_id: FigNewton.dynamodb.access_key_id,    #                                secret_access_key: FigNewton.dynamodb.secret_access_key,    #                                region: FigNewton.dynamodb.region)
    @dynamo_db = AWS::DynamoDB.new(access_key_id: 'enter access key id ',
                                   secret_access_key: 'enter secret key here,
                                   region: 'enter region id')
  end  

  
  # {'email' => 'mai@email.com', 'rollno' => 3},'name')   
def get_col_values(table_name, ref_column_hash, col_name)
    items = []
    table = @dynamo_db.tables[table_name]
    table.load_schema
    rows = table.items
    ref_column_hash.each do |key, value|
      rows = rows.where(key.to_sym=>value)
    end    rows.each do |row|
      items.push row.attributes[col_name]
    end    items 
 end
  def get_rows(table_name, ref_column_name, ref_col_value, col_name)
    items = []
    table = @dynamo_db.tables[table_name]
    table.load_schema
    table.items.select(ref_column_name.to_sym,col_name.to_sym ) do |row|
      if row.attributes[ref_column_name].to_i.equal? ref_col_value.to_i
        items.push row      end    end    items  end
  # returns the table object  def get_table(table_name)
    table = @dynamo_db.tables[table_name]
    table.load_schema
    table  end
  
end
 
# A helper class to access information stored in S3.
 class S3Helper   
include Singleton 
 # creates the singleton object   
def self.obj 
 S3Helper.instance
end
  
 # Initialize the helper and get the AWS S3 object. 
 def initialize     
# @s3_object = AWS::S3.new access_key_id: FigNewton.s3.access_key_id,    #                          secret_access_key: FigNewton.s3.secret_access_key    
 end 
 
 # Read a file from an S3 bucket.  #  # ==== Attributes  #  #   +fname+ - The name of the file to read from S3.  #  # ==== Examples  #  #   # To read a file from an S3 bucket, call the .read_file method from  #   # your code in the following way:  #   @s3_user.read_file 'test.xml'  def read_file(fname)
    @s3_object.buckets[@bucket].objects[fname].read
  end
 
end   


UserHelper.sd_sftp_user.read_file('folder/test.csv')
 

How to fail a test in selenium with Ruby and Cucumber

fail 'message to display in console'

This will fail the test when executed.

Saturday, 14 February 2015

Filesystem in Ruby - accessing parent files based on current folder/file path


puts File.expand_path(File.dirname(__FILE__) + "/../src/google_finance.rb")
puts File.dirname(__FILE__)

puts File.expand_path(File.dirname(__FILE__) + "/../../data")

#Webser            -- Root folder
#  +--data          --folder
#  +--sample         --folder
#    +--lib
#    +--src
#         +--google.rb
#         +--testing.rb
#    +--config.rb 
 

#to access data folder:
puts File.expand_path(File.dirname(__FILE__) + "/../../data")
#Output: C:/Users/user/workspace/WebservicesTest/data

#to access current folder:
puts File.dirname(__FILE__)
#C:/Users/user/workspace/WebservicesTest/sample/src

#to access google.rb file from testing.rb file:
puts File.expand_path(File.dirname(__FILE__) + "/../src/google_finance.rb")
#C:/Users/user/workspace/WebservicesTest/sample/src/google_finance.rb

#WE CAN REQUIRE LIKE THIS AS WELL
#require File.expand_path(File.dirname(__FILE__) + "/../lib/wrest")

Thursday, 12 February 2015

get parent or sibling folder/file based on current file location

def create_temp_file()
  @temp_fpath= ''  begin    @temp_fpath = File.absolute_path('..\src\features\data\tempfile.txt')
    create_file(@temp_fpath, 'Sample text')
  rescue Exception => e    puts e.message
  end  @temp_fpathend

rest client chrome/firefox plugins

Advance Rest Client
DHC Rest client
RestClient

Ruby Rest Service: get all the tags - api.del.icio.us api

require 'net/https'
http= Net::HTTP.new('api.del.icio.us', 443)
http.use_ssl = true
http.start do |http|
  req=Net::HTTP::Get.new('/v1/tags/get') 
  req.basic_auth 'myusername', 'password'
    @resp= http.request(req)
end

puts @resp.body

Output:
<?xml version="1.0" encoding="UTF-8"?>
<tags>
  <tag count="1" tag="!fromtwitter"/>
  <tag count="1" tag="social"/>
  <tag count="1" tag="ecommerce"/>
   <tag count="1" tag="flipkart"/>
</tags>


links:
http://www.rubyinside.com/nethttp-cheat-sheet-2940.html
https://github.com/augustl/net-http-cheat-sheet

Tuesday, 13 January 2015

Dir.pwd command

class RnD1
  puts 'testing'
  puts Dir.pwd
  base_path =  File.expand_path('basics/', Dir.pwd)
  x= File.open(base_path + '/RnD1.rb')
  puts x.inspect
end


testing
C:/Users/user/workspace/RubyRnD
#<File:C:/Users/user/workspace/RubyRnD/basics/RnD1.rb>