Tuesday 27 December 2016

Fetching top 50 sold products and their hit count of ebay's seller through Ruby On Rails

For fetching top 50 sold products of ebay's seller, we will call GetSellerEvents api of Trading Api.

As we know that ebay provide Trading Api's.Trading API follows eBay’s traditional listing model, where all aspects of a listing (description, product details, quantity and price, etc.) are created and managed.

So, we will integrate Trading Api's in our application. For this please follow my previous blog :

Authentication for ebay's seller and use Trading Api for getting seller information.

From above mentioned blog you can get ebay client. Which is necessary to call any api's of Trading Api.

Now, we will call GetSellerEvents api through this ebay client.

def get_top_selling_items(limit= 50)
      begin
        @ebay_client.get_seller_events(:StartTimeFrom => (Time.now.utc - 120.days).to_s).payload[:item_array][:item]
            .collect { |item| {item_id: item[:item_id],
                               sold_quantity: item[:selling_status][:quantity_sold],
                               item_title: item[:title],
                               hit_count: item[:hit_count]} }
            .sort { |x, y| y[:sold_quantity] <=> x[:sold_quantity] }
            .take(limit)
      rescue => e
        Rails.logger.debug("Exception occurred while fetching/get_top_selling_items: #{e.message}")
        nil
      end
    end

In above code you can set any Time of within last four month. Because ebay provides information of only last four month.

As you can see that in above code we are fetching seller events of last four month. After that we collecting sold quantity and hit count of that particular item. After collecting we are sorting it on the basis of sold quantity.

On the behalf of this, it will provide top 50 sold products and their hit count of that seller.

Sunday 4 December 2016

Track changes to your Rails models with PaperTrail


Sometime, it's become necessary to keep the record of how a model looked at any stage in its lifecycle and may be we want to revert it to previous version, or restore it after it has been destroyed.

For this situation PaperTrail comes with great solution. PaperTrail stores the pre-change version of the model, unlike some other auditing/versioning plugins, so you can retrieve the original version. This is useful when you start keeping a paper trail for models that already have records in the database.

So, in Gemfile: -

gem 'paper_trail'

Run command below to generate and migrate paper_trail which will add a versions table to your database and an initializer file for configuration

bundle exec rails generate paper_trail:install
bundle exec rake db:migrate

Add the following line to your model (app/models/post.rb):

class Post < ActiveRecord::Base
  has_paper_trail
end


If your controllers have a current_user method, you can easily track who is responsible for changes by adding a controller callback.

class ApplicationController
  before_action :set_paper_trail_whodunnit
end

You can specified it on action

class Post < ActiveRecord::Base
  has_paper_trail on: [:update, :create]
end


You can also specify which changes should not be tracked.

In model:-

has_paper_trail ignore: [:column_name]

Some fields can be skipped, they will neither be tracked nor included in the serialized version of the object.

has_paper_trail skip: [:column_name]

Now, you can display the audited versions, It’s easy, just, add the following route-

get '/posts/history', to: 'posts#history', as: :posts_history

Add a history method to the controller (app/controllers/posts_controller.rb):-

def history
  @versions = PaperTrail::Version.order('created_at DESC')
end

Rendering the view history (app/views/posts/history.html.erb):-

<div class="container">
  <h1>History</h1>

  <ul>
    <% @versions.each do |version| %>
      <li>
        <%= l(version.created_at, format: "%-d.%m.%Y %H:%M:%S %Z") %><br/>
        Event ID: <%= version.id %><br/>
        <b>Target:</b> <%= version.item_type %>
        <small>(id: <%= version.item_id %>)</small>; <b>action</b> <%= version.event %>;<br/>
        <div>
          More info:
          <pre><%= version.object %></pre>
        </div>
      </li>
    <% end %>
  </ul>
</div>

Sunday 27 November 2016

Infinite or Endless Scrolling pagination in Rails with Kaminari

In this blog we will implement infinite scrolling with kaminari gem. Infinite scrolling appends more content to the end of the page before the viewer gets to it, so they can just keep scrolling unabated and the application doesn't need to initially load a bunch of records only a small percent of viewers will ever scroll down to.

Add 'kaminari' to your Gemfile -


gem 'kaminari'

Run the jQuery generator to download the necessary files and setup your Rails app to use jQuery.

$ rails generate jquery:install

In Controller -

def index
  @projects = Project.order(:created_at).page(params[:page])
end


In index.html.erb

<h1>Listing projects</h1>

<table id="projects">
  <thead>
    <tr>
      <th>Title</th>
    </tr>
  </thead>

  <tbody class="project-snippet">
    <%= render @projects %>
  </tbody>
</table>

<br>

<%= paginate @projects %>

<script>

paginator = new ScrollPaginator({
       item: $('.project-snippet'),
       items_source_url: '<%= projects_path %>',
       total_pages: <%= @projects.count/(Project::PAGINATION_SIZE) %>
   }).enable();

</script>


In assets/javascripts/scrollpaginator.js -

ScrollPaginator.VISIBILITIES = ['TOP', 'COMPLETE'];

function ScrollPaginator(options) {
   this.options = {
       object_visibility: 'TOP',
       total_pages: 0
   };
   $.extend(this.options, options);

   this.sample_element = $('<div id="sample_object" class="sample_object"></div>');
   $(this.sample_element)
       .height(this.options.item.height()).width(this.options.item.width())
       .attr('class', $(this.sample_element).attr('class') + this.options.item.attr('class'));

   this.last_loaded_page = 1;
   this.request_next = true;
}

ScrollPaginator.prototype = {
   provision: function () {
       this.sample_element.attr('class', this.options.item.attr('class'));
       this.options.item.parent().append(this.sample_element)
   },

   validVisibility: function () {
       $.inArray(this.options.object_visibility, ScrollPaginator.VISIBILITIES)
   },

   increment_page: function () {
       this.last_loaded_page += 1;
       if (this.last_loaded_page >= this.options.total_pages) {
           this.last_loaded_page = this.options.total_pages;
       }
   },

   decrement_page: function () {
       this.last_loaded_page -= 1;
       if (this.last_loaded_page <= 1) {
           this.last_loaded_page = 1;
       }
   },

   activateOnScrollPagination: function () {
       var _this = this;
       $(window).scroll(function () {
           _this.loadItems();
       });
   },

   isSampleVisible: function () {
       var docViewTop = $(window).scrollTop();
       var docViewBottom = docViewTop + $(window).height();
       var elemTop = $(this.sample_element).offset().top;
       var elemBottom = elemTop + $(this.sample_element).height();
       switch (this.options.object_visibility) {
           case 'TOP':
               return ((elemTop <= docViewBottom) && (elemTop >= docViewTop));
               break;
           case 'COMPLETE':
               return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
               break;
           default:
               console.log('Visibility option is not valid.');
               return false;
       }
   },

   loadItems: function () {
       var _this = this;
       if (this.isSampleVisible()) {
           if (this.request_next) {
               if (this.last_loaded_page < this.options.total_pages) {
                   this.increment_page();

                   /*--- For preventing multiple requests ---*/
                   this.request_next = false;

                   $.ajax({
                       method: 'GET',
                       dataType: 'script',
                       url: this.options.items_source_url + '?page=' + _this.last_loaded_page
                   }).success(function () {
                       _this.request_next = true;
                       _this.loadItems();
                   }).fail(function () {
                       _this.decrement_page();
                   });
               } else {
                   this.request_next = false;
               }
           }
       }
   },

   enable: function () {
       this.provision();
       this.activateOnScrollPagination();
       this.loadItems();
   }
};

Sunday 20 November 2016

Web-interface for Resque's worker with resque-web

In previous blog we have implemented background job with Resque. Now we will implement web interface for these background workers.

For this we will use resque-web gem. Resque’s web interface is a great way to get a high-level understanding of your background workers, and its pluggable design has made it easy for others to contribute a number of really useful plugins.

So, in Gemfile -

gem 'resque-web',


Now, mount it in your config/routes.rb -

require "resque_web"

MyApp::Application.routes.draw do
  mount Resque::Server.new, at: "/resque"
end

In almost cases we certainly want to limit access when using resque-web in production. We can achieve this with routes constraints. Here we are allow access only to admin user.

So, edit your routes.rb - 

authenticate :admin_user do
    mount Resque::Server.new, :at => '/resque'
 end

Now, we have done with resque-web configuration. So,restart your Rails server.
Open up

http://localhost:3000/resque
in a browser to check out the web backend.


On the Overview tab you can see a list of the queues and workers. Each queue shows a count of pending jobs. The list of workers displays what queue(s) each worker is working on, and the job currently being processed (if any).

Sunday 13 November 2016

Check whether stripe's source is already exist or not in Ruby on Rails application

Stripe is a very popular payment solution that integrates really well into a Rails application. In this blog, we’ll guide you through how to check whether stripe's source is already exist or not.

Stripe allows storing of duplicate sources if you are using same card. In this case, if customer have provided same card multiple time. Stripe will create same card details storing multiple times as multiple cards.

For overcome from this situation and if you want make less calls to stripe, it is recommended that you store the fingerprints of all the souces locally and use them for checking uniqueness. Storing fingerprints of sources locally is secure and it uniquely identifies a source.

first we will fetch customer details from stripe -


#fetch the customer
customer = Stripe::Customer.retrieve(stripe_customer_token)

#Retrieve the card fingerprint using the stripe_card_token 
card_fingerprint = Stripe::Token.retrieve(stripe_card_token).try(:card).try(:fingerprint)

# check whether a card with that fingerprint already exists

default_card = customer.sources.all.data.select{|card| card.fingerprint ==  card_fingerprint}.last if card_fingerprint
#create new card if do not already exists

default_card = customer.sources.create({:card => stripe_card_token}) unless default_card

#set the default card of the customer to be this card, as this is the last card provided by User and probably he want this card to be used for further transactions

customer.default_card = default_card.id

# save the customer
customer.save


Sunday 6 November 2016

Background Jobs with Resque gem in Rails

It is very important in website building to keep your response times less. Long-running requests may be degrade server resource. In this situation we can perform some task in background using another resources.

Here we will use Resque for background job. Resque is a Redis-backed library for creating background jobs, placing those jobs on multiple queues, and processing them later.

Setting up Resque -

Hence, Resque is using Redis-backed library so we have to install first redis server -

Please follow the digital ocean link for installing redis https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-redis-on-ubuntu-16-04

Set Up Redis -

config/initializers/resque.rb -


ENV["REDISTOGO_URL"] ||= "redis://username:password@host:1234/"

uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password, :thread_safe => true)


In Gemfile -


gem 'resque'
gem 'resque-web', require: 'resque_web'

Run bundle

Next include it in your application -

require 'resque'

Now we will create a queue for appointment receipt sender. For this create a folder of workers in app directory.

Create a file appointment_receipt_sender.rb -

class AppointmentReceiptSender
  @queue = :appointment_receipt_sender_queue
 
  def self.perform(appt_id)
    appt = Appointment.find appt_id
    client = appt.client

    AppointmentMailer.receipt_email(client).deliver
    appt.receipt_sent_at = Time.now
    appt.save
  end
end

In controller -

def appointment_receipt_sender
  Resque.enqueue(AppointmentReceiptSender, appt_id)
end

To start a worker, create a Rakefile in your app's root (or add this to an existing Rakefile):

require 'your/app'
require 'resque/tasks'

This will load the Resque tasks and load the environment which is required for doing any work.

To start a worker that will pull work off of all queues run the command:

$ rake resque:work QUEUE=*


Monitoring the Resque Queue

Open your config/routes.rb and mount the application like this:

require 'resque/server'

MyApp::Application.routes.draw do
  mount Resque::Server.new, at: "/resque"
end


Then restart your Rails server. Open up
http://localhost:3000/resque
in a browser to check out the web backend.

On the Overview tab you can see a list of the queues and workers. Each queue shows a count of pending jobs. The list of workers displays what queue(s) each worker is working on, and the job currently being processed (if any).





Sunday 23 October 2016

Google authentication with 'signet' ruby gem

We will implement google authentication with 'signet' gem. It is official ruby gem of google. It support  OAuth 1.0 / OAuth 2.0 implementation.

So, in Gemfile.


  gem 'signet'

run bundle.

Now, we have to create a google application on google developer console. This will provide us app_id, client_id and client_secret.

After that we will initialize the client of google auth for particular user.

In User.rb

  def initialize_google_client
    client = Signet::OAuth2::Client.new(
        :authorization_uri => 'https://accounts.google.com/o/oauth2/auth',
        :token_credential_uri =>  'https://www.googleapis.com/oauth2/v3/token',
        :client_id => 'Your client Id is here',
        :client_secret => 'Your client secret is here',
        :scope => 'email profile',
        :redirect_uri => 'redirect url same as you have put in your google     application'
    )
  end


In above initializer you can add scope as per your requirement.

Now, we will create routes for authentication path and callback redirect path.

In routes.rb

  get '/google_auth'=> 'some_controller#google_auth', as: 'google_authentication'
  get 'google_callback' => 'some_controller#google_callback'

Now, in controller -

  def google_auth
    client = current_user.initialize_google_client
    redirect_to(client.authorization_uri)
  end

  def google_callback
    client = current_user.initialize_google_client
    client.code = request['code']
    token = client.fetch_access_token!
  end


Above method provide you, access_token, refresh_token and id_token for particular user.


Saturday 8 October 2016

Twitter authentication and Posting url and message on Twitter through RoR

In this blog, we will implement authentication with twitter and posting on twitter.

First, we will implement twitter authentication with omniauth-twitter.

So, in Gemfile -



gem 'omniauth-twitter'

After that in config/intializer/omniauth.b -


Rails.application.config.middleware.use OmniAuth::Builder do
# get consumer_key and consumer_secret from creating a developer account in twitter
    provider :twitter, ['consumer_key'], ['consumer_secret'], {:display => "popup",:x_auth_access_type => 'Read, Write & Direct Messages'}

end



In routes.rb -




# Here we are using oauths controller. Set callback url in twitter developer account as https://localhost:3000/auth/twitter/callback
get '/auth/twitter/callback' => 'oauths#twitter_callback', as: 'twitter-callback'


Now in controller -


  def twitter_callback
    token = request.env['omniauth.auth']['extra']['access_token'].params[:oauth_token]
    token_secret = request.env['omniauth.auth']['extra']['access_token'].params[:oauth_token_secret]
    twitter_id = request.env['omniauth.auth']['extra']['access_token'].params[:user_id]
  end


Above method return token, token_secret and twitter_id of twitter user.

Now, we have all done with twitter authentication.

Now for posting we will use twitter gem.

In Gemfile -

gem 'twitter', '~> 5.16'

We have to initialize the twitter cleint. Below method intialize
In model - # you can put anywhere initializer method as per your convenience.


def initialize_twitter_client
    data = YAML::load_file(File.join(Rails.root, 'config', 'api_keys.yml'))[Rails.env]
    client = Twitter::REST::Client.new do |config|
      config.consumer_key = ['consumer_key'] # put here consumer_key of twitter app.
      config.consumer_secret = ['consumer_secret'] # put here consumer_secret of twitter app.
      config.access_token = ['token'] # We have alraedy got token from twitter authentication for perticular twitter user.
      config.access_token_secret = ['token_secret'] # It is also provided by twitter authentication for perticular twitter user.
    end
end


Now, we can post on twitter from below method



def post_to_twitter(product)
    client = initialize_twitter_client
    message = "Put your message here. Be sure it will not more than 140 character"[0..139]
    client.update("#{message} #{url}")
end

Sunday 2 October 2016

Posting on pinterest through RoR

In last blog we have implemented pinterest authentication. Now, we will implement posting on pinterest.

2.) We will do post on pinterest with pinterest-api gem 

So, in Gemfile -


gem 'pinterest-api'

run bundle

From last blog we can get token from pinterest_callback with the help of this method we can initialize the client for pinterest api-

In Model pinterest.rb -


def initialize_pinterest_client
  @client = Pinterest::Client.new(token) #Get token from pinterest authentication.
end

For posting on pintesert we have to create pinterest board.


def create_pinterset_board(board_name)
  @client = initialize_pinterest_client
  board_id = @client.create_board({name: board_name}).data.id
end

Now, we can create pin on pinterest -


def post_to_pinterest(product)
  @client = initialize_pinterest_client
  @client.create_pin({board: board_id, note: post_name, link: url, image_url: image_url})
end

Sunday 25 September 2016

Pinterest aunthentication and Posting on pinterest through RoR


In this blog, we will implement authentication with pinterest and posting on pinterset board.


We can divide it i  two parts. In first part we will do pinterest authentication and in second part we will do posting on pinterest -

1.) We can do pinterest authentication with gem 'omniauth-pinterest'

So, in Gemfile -


gem 'omniauth-pinterest'

After that in config/intializer/omniauth.b -


Rails.application.config.middleware.use OmniAuth::Builder do
# get appid and app_seceret from creating a developer account in pinterest
  provider :pinterest, ['app_id'], ['app_secret'], {:display => "popup"}
end

In routes.rb -


get '/auth/pinterest', as: 'pinterest-authentication'
# Here we are using oauths controller. Set callback url in pinterest developer account as https://localhost:3000/auth/pinterest/callback
get '/auth/pinterest/callback' => 'oauths#pinterest_callback', as: 'pinterest-callback'

Now in controller -


  def pinterest_callback
    token = request.env['omniauth.auth']['credentials']['token']
    pinterest_id = request.env['omniauth.auth'].uid
  end

Above method return token and pinterest id of pinterset user.

Now, we have all done with pinterest authentication.

In second part of the blog we will do pinterest posting.

To be continued ......

Sunday 18 September 2016

Checkbox tag with kaminari pagination



If you are using kaminari pagination with ajax and using check-box tag for selecting product there may be a problem in selection of product from different page because of pagination ajax it will select only products of last page.

In this situation, we can use jquery for updating the selected products.

In view page -
Here we have created a view of product listing with check-box tag and ajax pagination -


    <div class="product_list">
      <% if products.size > 0 %>
          <% products.each do |product| %>
              <div class="form-group">
                <%= check_box_tag "products[]", product.id, false, class: 'selected_product' -%>
                <%= image_tag(product.image_url, class: "list_pro_img") %>
                <%= link_to product.item_title, product.item_url, target: '_blank' %>
              </div>
          <% end %>
      <% else %>
          <p>Product is not found</p>
      <% end %>
    </div>
    <div id="paginator">
      <%= paginate @products, :remote => true %>
    </div>
    <%= submit_tag "Next", :class => "btn btn_next selected_post_product" %> 

Now we will update all the selected product from different page in products global variable of jquery -

So in custom.js -

function post_selected_products(){


    if (!Array.prototype.remove) {
        Array.prototype.remove = function(val, all) {
            var i, removedItems = [];
            if (all) {
                for(i = this.length; i--;){
                    if (this[i] === val) removedItems.push(this.splice(i, 1));
                }
            }
            else {
                i = this.indexOf(val);
                if(i>-1) removedItems = this.splice(i, 1);
            }
            return removedItems;
        };
    }

# In the above jquery we are removing items from array if product was unchecked after checked.
    products = [];
    $('div.product_list').on('change','input.selected_product',function(){
        if($(this).is(':checked')){
            var product_id = $(this).val();
            products.push(product_id)
        }else{
            var product_id = $(this).val();
            products.remove(product_id)
        }
# Now, we will post all the selected product on products controller -
        $.post('/products/selected_products', { product_ids: products});
    });



In view include above js -


<script type="text/javascript" charset="utf-8">
    post_selected_products()
</script>


In products controller -


def selected_products
    if request.xhr?
      # Here we will get all the selected products
      @products = params[:product_ids]
    end
end


Thats it !!!

Saturday 10 September 2016

Belongs_to Association in Devise Model Rails 5

Here, we are going to create a relationship between Account has_many user and user belongs_to account. User can create through Devise gem sign up.

So, in Gemfile -

gem 'devise'

Run the bundle command to install it.

$ bundle install

Next, you need to run the devise generator -


$ rails generate devise:install

Now, create a User model and configure it with the default Devise modules -


$ rails generate devise User

Then run

$ rake db:migrate

We have done set up of devise for user model -

Now, we will do association between Account and User Model.

In account.rb


class Account < ApplicationRecord
  has_many :users, inverse_of: :account, dependent: :destroy
end

In user.rb


class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  ######### Association####################
  belongs_to :account, inverse_of: :users
 
end


For this association we have to add refrence field of account_id in users table -

$ rails g migration add_account_id_to_users account_id:integer

Then run

$ rake db:migrate

Now, we have to override devise registeration controller for permit account_id params -

$ rails generate devise:controllers users registrations

In routes.rb

devise_for :users, controllers: {registrations: 'users/registrations'}

Now, we can permit the account_id params in registrations controllers -

In uncomment registrations controllers the follwing lines -

before_action :configure_sign_up_params, only: [:create]


 def create
    super
  end

 # account_id params to permit, append them to the sanitizer.
  def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up, keys: [:account_id])
  end

Till now, we have ovveride the registrations controller and permit the extra params but when we will try to sign up a user it will through a exception of account id can't be blank.

For the handle above situation we have to assign the value of account_id in user table before creating user but before_create callback doesn't work in this condition due to validation of devise model. So we have to use before_validation.

In user.rb

 before_validation :create_account
 # Here we creating account and assing account_id to user
  def create_account
    account = Account.new(name: 'name')
    if account.save
      self.account_id = account.id
    end
  end




Friday 2 September 2016

Display a loading gif during Ajax request processing within rails

In this bog we will implement display a loading gif during Ajax request processing within rails.

HTML - In the view we will do product listing of the perticular user. While product will fetched from ajax request loading gif will be display.


<div class=""><h1>Product Listing</h1></div>
   <%= hidden_field_tag("user", current_user.id) %>
   <div class="loader">
     <i class="fa fa-refresh fa-spin fa-3x fa-fw"></i>
        <span>Loading...</span>
        <h2>Loading...</h2>
    </div>
  <div id="product_listing_div"></div>

Js - Now, we will send a ajax request to a controller's method for getting products for perticular user here we will send user params for that.

$(document).ready(function() {
    # Here ajax request is started on page load. You can create a condition for ajax request(ex- on click method).
        var user = $('#user').val();
        $.get(('/product_listing'), {user: user}, function(response) {
            if (response !== null) {
                $('.loader').hide();
            }
        });
    });


Controller -

def product_listing
    if request.xhr?
      user = params[:user]
      @products = Product.where(user: user).take
    end
  end

In view- Create product_listing.js.erb

$('#product_listing_div').html("<%= escape_javascript(render :partial => "product_listing", :locals => {:products => @products}) %>")

Create a partial view - _product_listing.html.erb


   
      <% if products.size > 0 %>
          <% products.each do |product| %>
              <div class="form-group">
                <%= image_tag(product.image) %>
                <%= product.title%>
              </div>
          <% end %>
      <% else %>
          <p>Product is not found</p>
      <% end %>

Css - for loading gif

.loader{
    text-align:center;
}
.loader .fa{
    font-size:100px;
    color:#25aae1 ;
}


That's it !!!!

Saturday 27 August 2016

Authentication for ebay's seller and use Trading Api for getting seller information

In this blog i am going to describe how to use Trading Api of Ebay for getting seller information.

We will proceed it in two parts -
1. Ebay Authentication.
2. Fetch seller information from Treding Api.

1. Ebay Authentication.

We have to do ebay authentication for verifying the seller. For this we will use omniauth-ebay gem.


So, in Gemfile :-

gem 'omniauth-ebay'

Now add omniauth initializer in config/initializers/omniauth.rb -

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :ebay, "runame", "devid", "appid", "certid", "siteid", "environment", "auth_type"
end


"runame", "devid", "appid", "certid" these details you can find by going into your developer's account at eBay DevZone.

"siteid" - "siteid" represent your country or zone. You can obatain this from ebay.

"environment" - Here you can defined your environment as :production or :sandbox .

"auth_type" - If you want authenticate user every time than you have to define it as 'SignIn' .

After these set up you have to set routes path for ebay oauth callback. So, in routes.rb -

get '/auth/ebay/callback' => 'your_controller#your_method', as: 'ebay_callback'


Now you will be able to access the omniauth session data by accessing

request.env['omniauth.auth']

For token - request.env["omniauth.auth"]['credentials']['token']

2. Fetch seller information from Treding Api.


For getting seller data ebay provides Treding Api. We can use ebay_client gem. it provide lightweight eBay Trading API Client -

In Gemfile :-

 gem 'ebay_client', '~> 0.2.0'

In config/ebay_client.yml :-

development: &sandbox
  api_keys:
    - token: '<YOUR SANDBOX AUTHENTICATION TOKEN>' # we will set token later accourding to User
      devid: '<YOUR SANDBOX DEV ID>'
      appid: '<YOUR SANDBOX APP ID>'
      certid: '<YOUR SANDBOX CERT ID>'

test:
  <<: *sandbox

production:
  api_keys:
    - token: '<YOUR LIVE AUTHENTICATION TOKEN>' # we will set token later accourding to User
      devid: '<YOUR LIVE DEV ID>'
      appid: '<YOUR LIVE APP ID>'
      certid: '<YOUR LIVE CERT ID>'


Now, we have to make user specific yml file so we can set user's token. For this we will generate a Model EbayConfig -

In app/model/ebay_config

require 'yaml'
class EbayConfig < ActiveRecord::Base
  belongs_to :user, inverse_of: :ebay_config

  validates :app_id, :dev_id, :cert_id, :token, presence: true

  ######################### CallBacks##########################
  after_create :create_user_yml

  def self.user_ebay_data(user, user_token) #Pass the value of user as current_user and user_token of request.env["omniauth.auth"]['credentials']['token']
    data = YAML::load_file('config/ebay_client.yml')
    app_id = data["production"]["api_keys"].first["appid"]
    dev_id = data["production"]["api_keys"].first["devid"]
    cert_id = data["production"]["api_keys"].first["certid"]
    token = user_token
    @ebay_config = user.build_ebay_config(app_id: app_id, dev_id: dev_id, cert_id: cert_id, token: token)
    @ebay_config.save
  end

  def create_user_yml
    _user_yml = {"production" => create_user_config_hash}

    _yml_file = File.join(Rails.root, 'ebay_ymls', "#{self.user_id}_ebay_config.yml")
    File.open(_yml_file, "wb") do |file|
      file.write(_user_yml.to_yaml)
    end
  end

  def create_user_config_hash
    {
        api_keys: [{
                       appid: app_id,
                       devid: dev_id,
                       certid: cert_id,
                       token: token
                   }]
    }
  end

end


Now we have to intialize the ebay_client for specific user, For this we will create a module

require 'ebay_client'
module EbayGlue
  class EbayConnector
    attr_accessor :ebay_client

    def initialize(user_id)
      configurations = EbayClient::Configuration.load Rails.root.join('ebay_ymls', "#{user_id}_ebay_config.yml")
      configuration = configurations['production']
      @ebay_client = EbayClient::Api.new configuration
    end

  end
end


Now you can get @ebay_client and it will provide all the information

ex- @ebay_client.get_store.payload
    @ebay_client.get_user



Thats it !!!!!


Friday 19 August 2016

Posting on Facebook Through Ruby On Rails

Here we go to post a link on Facebook with th help of Koala gem.

Koala is a Facebook library for Ruby, supporting the Graph API.

Installation - 

gem "koala", "~> 2.2"

Graph API

The Graph API is the simple, slick new interface to Facebook's data.
Using it with Koala is quite straightforward.  First, you'll need an access token, which you can get through
Facebook's Graph API Explorer (click on 'Get Access Token').

Then, go exploring:

@user = Koala::Facebook::API.new(access_token)


Use that access_token to set this new @user. Now whenever you mention "me" in any Graph API request on a @user, you'll be mentioning this user (@user) as the target for that request. "me" is just the facebook ID for the user itself.

Now you can post to your user's feed.

@user.put_connections("me", "feed", :message => "I am writing on my wall!")

For posting a link -

@user.put_wall_post("message",{link: item_url}, "me").

Thats it.

Cheer Up !!!!

Friday 12 August 2016

ebay Trading API's integration with RoR

For all the information of Seller we have to use Trading API's of ebay.

Here i am going to fetch seller's item with the help of ebay_client gem.

EbayClient

Simple, lightweight eBay Trading API Client.

Installation

Rails

Gemfile:
gem 'ebay_client', '~> 0.2.0'
config/ebay_client.yml:
development: &sandbox
  api_keys:
    - token: '<YOUR SANDBOX AUTHENTICATION TOKEN>'
      devid: '<YOUR SANDBOX DEV ID>'
      appid: '<YOUR SANDBOX APP ID>'
      certid: '<YOUR SANDBOX CERT ID>'

test:
  <<: *sandbox

production:
  api_keys:
    - token: '<YOUR LIVE AUTHENTICATION TOKEN>'
      devid: '<YOUR LIVE DEV ID>'
      appid: '<YOUR LIVE APP ID>'
      certid: '<YOUR LIVE CERT ID>'
You can check it by - 
e.g. rails console:
EbayClient.api.get_ebay_official_time!
# => {:timestamp=>Fri, 22 Nov 2013 12:31:02 +0000}
Now you can get information from below methods - 
EbayClient.api.get_access_token                EbayClient.api.get_constant                    EbayClient.api.get_primary_key
EbayClient.api.get_access_token_info           EbayClient.api.get_encoding                    EbayClient.api.get_print
EbayClient.api.get_all                         EbayClient.api.get_errors                      EbayClient.api.get_proxies
EbayClient.api.get_all_gem_names               EbayClient.api.get_fields                      EbayClient.api.get_proxy
EbayClient.api.get_all_gem_names_and_versions  EbayClient.api.get_first_row                   EbayClient.api.get_proxy_from_env
EbayClient.api.get_all_versions                EbayClient.api.get_first_value                 EbayClient.api.get_question
EbayClient.api.get_and_set                     EbayClient.api.get_key_string                  EbayClient.api.get_queue
EbayClient.api.get_app_access_token            EbayClient.api.get_label                       EbayClient.api.get_relative_path
EbayClient.api.get_app_access_token_info       EbayClient.api.get_labels                      EbayClient.api.get_response
EbayClient.api.get_arguments_from              EbayClient.api.get_length16                    EbayClient.api.get_rr
EbayClient.api.get_attribute                   EbayClient.api.get_name                        EbayClient.api.get_screen_size
EbayClient.api.get_best_compatible             EbayClient.api.get_object                      EbayClient.api.get_string
EbayClient.api.get_best_encoding               EbayClient.api.get_objects                     EbayClient.api.get_string_list
EbayClient.api.get_byte                        EbayClient.api.get_one_gem_name                EbayClient.api.get_time
EbayClient.api.get_bytes                       EbayClient.api.get_one_optional_argument       EbayClient.api.get_token_from_server
EbayClient.api.get_callbacks                   EbayClient.api.get_or_default                  EbayClient.api.get_unpack
EbayClient.api.get_cert_files                  EbayClient.api.get_page                        EbayClient.api.get_user_info_from_cookie
EbayClient.api.get_class                       EbayClient.api.get_page_access_token           EbayClient.api.get_user_info_from_cookies
EbayClient.api.get_comments_for_urls           EbayClient.api.get_parameters                  EbayClient.api.get_user_picture_data
EbayClient.api.get_connection                  EbayClient.api.get_picture                     EbayClient.api.get_value
EbayClient.api.get_connections                 EbayClient.api.get_picture_data

In some of above method you may get error of -
Error - The time range has exceeded.
ex- EbayClient.api.get_seller_list
may through a exception. In this case you have to pass that params in argument.
ex - EbayClient.api.get_seller_list(StartTimeFrom: '2016-07-01T06:38:48.420Z', StartTimeTo: '2016-08-11T06:38:48.420Z')

Friday 5 August 2016

Setup of Ruby On Rails

Hi,

This is my first blog so i am going to start with setup of ruby on rails.

We will be setting up a Ruby on Rails development environment on Ubuntu 15.10 Wily Werewolf.
The reason we're going to be using Ubuntu is because the majority of code you write will run on a Linux server. Ubuntu is one of the easiest Linux distributions to use with lots of documentation so it's a great one to start with.

1. The first step is to install some dependencies for Ruby.

sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev

2. The installation for rvm is pretty simple:

sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm install 2.3.1
rvm use 2.3.1 --default
ruby -v
 
3. The last step is to install Bundler 
 
gem install bundler 


git config --global color.ui true
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR@EMAIL.com"
ssh-keygen -t rsa -b 4096 -C "YOUR@EMAIL.com"
 
The next step is to take the newly generated SSH key and add it to your Github account
 
cat ~/.ssh/id_rsa.pub
 
Once you've done this, you can check and see if it worked:  
 
ssh -T git@github.com
 
You should get a message like this:   
  

Hi excid3! You've successfully authenticated, but GitHub does not provide shell access.
 
 
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodej
 
gem install rails