Getting oAuth1 access token and saving it in the DB (ruby)

Here is how I connected to an external API using oAuth, got access token and saved it in DB for future use. Each time I can now access the API with the saved access token as long as it doesn’t expire.
The flow of oAuth is: client(myapp) tries to get access to user’s service, and the service provider(SP) in the middle will handle the authentication and authorization. In our case, our app first connects to the SP, get the request token, use the request token to go to authorize url(or give that url to the user), where the user(in our case, we are also user in that SP) can log in and get the verification code, give it to SP, which generates a access token and return it to us for direct communication between us and the user data.

I am using oAuth(0.4.7) for this development. (https://rubygems.org/gems/oauth/versions/0.4.7). My code snippet below:

First will prepare the consumer object:

consumer_key = ‘thekeyyougetfromexternalapiparty’

consumer_secret = ‘thesecretyougetfromexternalapiparty'

base_url = ‘externalapiurl.com/’

request_token_path = base_url + ‘oauth/request-token'

access_token_path = base_url + 'oauth/access-token'

call_back_url = "oob"




consumer = OAuth::Consumer.new(consumer_key, consumer_secret, {
    
          :scheme => :header,
    
          :http_method => :post,
    
          :request_token_path => request_token_path,
    
          :access_token_path  => access_token_path,
    
          :oauth_callback => call_back_url,
    
          :signature_method   => 'HMAC-SHA1',
    
          :version => '1.0A'

Then, get the request token from the service provider.

request_token = consumer.get_request_token()

After I got the request token, I used the oauth_token from the request token and use it to construct the authorize url, which user can use to log-in in a browser and reads in the verification code from user:

auth_url = "#{base_url}#{request_token.authorize_url}"
verifier = STDIN.gets.strip

Once we have the verification code, we can now get access token:

access_token=request_token.get_access_token(:oauth_verifier => verifier)

This access token now can be used to access the API with the user’s info:

get_info_url = "theurltogetinfoapi"
response = access_token.get(get_info_url)
result = JSON.parse(response.body)

So, now I have the access token and I can access all the services SP gives for the user that logged in!

Sometimes, some SP allows you to use the access token for longer period of time, and you may want to save the access token in you environment or in DB. Here is how I did that:

act = AccessToken.create(token: access_token.token, secret: access_token.secret, expires_in: 10.days, is_expired: false )
act.save

Once this is saved in DB, here is how to rebuild it later:

db_access_token = AccessToken.find_by_is_expired(false)
token = db_access_token.token
secret = db_access_token.secret
access_token = OAuth::AccessToken.new(consumer)
access_token.token = token
access_token.secret = secret

Please note that, we are assuming there is a separate task to update is_expired in DB once the access token expires. Also, the consumer object is the same one we used in the beginning.

So, this is it! Now you have a full fledged oauth communication in ruby/rails!

Some Rails Deployment Errors & Fixes

Error #1:
libssl.so.10: cannot open shared object file: No such file or directory – /efs/www/rprod/sites/digitalreadingroom.nypl.org/shared/bundle/ruby/1.9.1/gems/mysql2-0.3.11/lib/mysql2/mysql2.so

Solution:
The fix was to symlink it from a old version to this new one or update mysql client in the QA.

Error #2:
Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

Solution:
Adding the gems ‘execjs’ and ‘therubyracer’ – most of the time that works. Or installing node.js.  But in my case it did not work, ‘therubyracer’ gem depends on the gem ‘libv8’, which has newer version that was not working, so  forced ‘rubyracer’ to downgrade to version ‘0.10.2’ which made ‘libv8’ version to be downgraded and that solved it 🙂

Error #3:
We had two asset precompile issues: 1) Error compiling the assets because of css import issues. 2) Rendering issue after deployment.

Solution:
For #1, resolve the css issues. For #2, added the following to prod env file and made sure to run precompile in deploy.rb script:

config.assets.enabled = true

config.assets.compress = true

config.assets.precompile += %w( *.js *.css )

Error #5:
Mistakenly commented the line Bundler.require(:default, :assets, Rails.env), and totally forgot to uncomment it :/ And spend a good time investigating why none of the gems were available to the app! It took me a while to realize without that line an app has no clue to look for it gems in bundle path!!

Solution:
Uncomment that line!!!!

Projects & Libraries – Begining Ruby Guide!

  • Load and require will load the file mentioned, the difference is, load will every time its called, where as require will load once within the scope. When we use this, it looks into the current and some other directories to search for the file. The place where it has the directory list is on $LOAD_PATH. We can check it:
    $:.each {|fname| puts fname}

    To add more file to it:

     $:.push '/your/directory/here'
     require 'yourfile'

PART 2: Classes/Objects/Modules – Begining Ruby Guide!

  • Classes: A class is a collection of methods and data that are used as a blueprint to create multiple objects relating to that class.
  • Objects: An object is a single instance of a class. An object of class is a single person. An object of class is a single dog. If you think of objects as real-life objects, a class is the classification, whereas an object is the actual object or “thing” itself.
  • Local variable: A variable that can only be accessed and used from the current scope. Instance/object variable: A variable that can be accessed and used from the scope of a single object. An object’s methods can all access that object’s object variables.
  • Global variable: A variable that can be accessed and used from anywhere within the current program.
  • Class variable: A variable that can be accessed and used within the scope of a class and all of its child objects.
  • Encapsulation: The concept of allowing methods to have differing degrees of visibility outside of their class or associated object.
  • Polymorphism: The concept of methods being able to deal with different classes of data and offering a more generic implementation (as with the and methods offered by your and classes).
  • Module: An organizational element that collects together any number of classes, methods, and constants into a single namespace.
  • Namespace: A named element of organization that keeps classes, methods, and constants from clashing.
  • Mix-in: A module that can mix its methods in to a class to extend that class’s functionality.
  • Enumerable: A mix-in module, provided as standard with Ruby, that implements iterators and list-related methods for other classes, such as , , , and . Ruby uses this module by default with the and classes.
  • Comparable: A mix-in module, provided as standard with Ruby, that implements comparison operators (such as , , and ) on classes that implement the generic comparison operator .

PART 1: Basics – Begining Ruby Guide!

I started reading the book “Beginning Ruby Guide” today, here are some important points to keep in mind(from PART 1 section of the book ):

  • Everything in Ruby is a object: ‘When you write a simple sum such as 2 + 2, you expect the computer to add two numbers together to make 4. In its object-oriented way, Ruby considers the two numbers (2 and 2) to be number objects. 2 + 2 is then merely shorthand for asking the first number object to add the second number object to itself. In fact, the + sign is actually an addition method! (It’s true, 2.+(2) will work just fine!). You can prove that everything in Ruby is an object by asking the things which class they’re a member of.

  • Kernel is a special class whose methods are made available in every class and scope throughout Ruby. You’ve used a key method provided by Kernel already, for ex: puts.

  • Concept of subroutine: even though almost everything in Ruby is an object, you can use Ruby in the same way as a non–object-oriented language if you like, even if it’s less than ideal. Like, you can just define a method and use it without any association of a  object.
  • Interesting comparison operator: x <=>y (returns 0 if x and y are equal, 1 if x is higher, and -1 if y is higher).
  • You’ll be using this style( 5.times { puts “Test” } ) for single lines of code from here on, but will be using do and end for longer blocks of code. This is a good habit to pick up, as it’s the style nearly all professional Ruby developers follow.
  • Constants start with capital and can not be changed once declared.
  • Text: to include multiple lines of texts in the span you do: x = %q{This is multiple line}, another way:
    x = <<END_MY_STRING_PLEASE
    This is the string
    And a second line
    END_MY_STRING_PLEASE
  • You can embed expressions (and even logic) directly into strings. This process is called interpolation. In this situation, interpolation refers to the process of inserting the result of an expression into a string literal. The way to interpolate within a string is to place the expression within #{ and } symbols. An even more basic example demonstrates:

           puts "100 * 5 = #{100 * 5}"
  • sub method substitute once, whereas gsub substitutes multiple occurrences. EX: puts “this is a test”.gsub(‘i’, ”). Read more on regular expression later******.
  • Defining x << 4 and x.push(4) for an array is same!
  • [1, 2, 3, 4].collect { |element| element * 2 } : collect iterates through an array element by element, and assigns to that element the result of any expression within the code block. In this example, you multiply the value of the element by 2.
  • Hash: x.each { |key, value| puts “#{key} equals #{value}” }. Note**: In Ruby 1.8, there is no guarantee that elements will be returned in a specific order. In Ruby 1.9, however, the order in which the elements were inserted into the hash will be remembered, and each will return them in that order.
  • Code Blocks: each_vowel is a method that accepts a code block, as designated by the ampersand (&) before the variable name code_block in the method definition. It then iterates over each vowel in the literal array %w{a e i o u} and uses the call method on code_block to execute the code block once for each vowel, passing in the vowel variable as a parameter each time.
    Note** Code blocks passed in this way result in objects that have many methods of their own, such as call. Remember, almost everything in Ruby is an object! (Many elements of syntax are not objects, nor are code blocks in their literal form.)

    def each_vowel(&code_block)
     %w{a e i o u}.each { |vowel| code_block.call(vowel) }
    end
    each_vowel { |vowel| puts vowel }
    
    Another alternative is to use yield:
    def each_vowel
     %w{a e i o u}.each { |vowel| yield vowel }
    end
  • It’s also possible to store code blocks within variables, using the lambda method:

           print_parameter_to_screen = lambda { |x| puts x }
           print_parameter_to_screen.call(100)
  • Other languages often have limitations on the size of numbers that can be represented. Commonly this is 32 binary bits, resulting in a limit on values to roughly 4.2 billion in lan- guages that enforce 32-bit integers. Most operating systems and computer architectures also have similar limitations. Ruby, on the other hand, seamlessly converts between numbers that the computer can handle natively (that is, with ease) and those that require more work. It does this with different classes, one called Fixnum that represents easily managed smaller num- bers, and another, aptly called Bignum, that represents “big” numbers.

Proc/Lambda and Symbol/Strings

I have been quite busy these days to get back to continue with the Rails book. Today am trying to understand Procs and Lambdas & Symbol and Strings. Found a wonderful details by Robert Sosinski

Proc/Lambdas/Blocks/Mehtods From His Notes:

Blocks, Procs, Lambdas, and Methods are an aspect of closures in Ruby.

– We can pass blockes to other methods, unlike attributes blocks doesn’t have a name.

– Proc is kind of block, the subtle difference is: proc has a name (unlike block), thus can be used multiple times easily. And another is: a proc can have multiple callbacks which a block can not.

– Lamda is like anonymous methods. The difference with proc is that unlike proc it checkd the number of arguments passed. Also, a return can not be used in the proc block, but can be used in lambda block.

– Methods are like lambdas. Also like regular methods which can be passed to other methods.

Strings/Symbols From His Notes:

– In ruby symbols are immutable, and strings are mutable.

puts "hello" << " world"  => hello world
puts :hello << :" world"  => hello world *.rb:4: undefined method `<<' for :hello:Symbol (NoMethodError)

– In symbol all memory are same as immutable.

puts :"hello world".object_id => 239518 puts :"hello world".object_id => 239518

– In string all memory are different as mutable.

puts "hello world".object_id => 3102960 puts "hello world".object_id => 3098410

Notes: Object Within Scope Of Another Object (Nesting)

Notes taken from the book: Rails 3 in Action

  • Nested Routing:
    resources :projects do
      resources :tickets
    end
  • This code tells the routing for Rails that you have a tickets resource nested inside the projects resource.
  • So a ticket controller is build which is of projects:
     rails g controller tickets
  • And the model is created:
    rails generate model ticket title:string description:text project:references

    The project:references part defines an integer column for the tickets table called project_id in the migration. This column represents the project this ticket links to and is called a foreign key.

  • Here is how a new defined for the ticket object
    def new
      @ticket = @project.tickets.build
    end
  • To make @project available always, we create a private method and call it on the top of the class: before_filter :find_project
  • NOTE: Project model will have has_many :tickets, :dependencies => :delete_all and Tickets have belongs_to :project in the models.
  • Tickets form created the following way:
      <%= form_for [@project, @ticket] do |f| %>
  • Find Ticket method in TicketsController:

      def find_ticket
         @ticket = @project.tickets.find(params[:id])
      end
  • Link to edit ticket: [which means: edit_project_ticket_path(@project, @ticket) ]
     <%= link_to "Edit Ticket", [:edit, @project, @ticket] %>

Notes: Create A Basic Rails App

Notes taken from the book: Rails 3 in Action

  • Routing:
    resources :projects

    This is called a resource route, and it defines the routes to the seven RESTful actions in your projects controller. When something is said to be RESTful, it means it conforms to the Representational State Transfer (REST) standard. In Rails, this means the related controller has seven actions:

    • index
    • show
    • new
    • create
    • edit
    • update
    • destroy
  • These seven actions match to just four request paths:

    – /projects
    – /projects/new
    – /projects/:id
    – /projects/:id/edit

  • To generate a controller, run this command:

    rails g controller projects

    This command produces output similar to the output produced when you ran rails new earlier, but this time it creates files just for the projects controller

  • Run the following command to generate your first model:

    rails g model project name:string
  • csrf_meta_tags in layout/applicaiton.html.erb: is for protecting your forms from cross-site request forgery (CSRF)11 attacks. It creates two meta tags, one called csrf-param and the other csrf- token. This unique token works by setting a specific key on forms that is then sent back to the server. The server checks this key, and if the key is valid, the form is sub- mitted. If the key is invalid, an ActionController::InvalidAuthenticityToken exception occurs.
  • A helper for getting title:
    module ApplicationHelper
      def title(*parts)
        unless parts.empty?
          content_for :title do
            (parts << "Ticketee").join(" - ") unless parts.empty?
          end
         end
       end
    end
  • Now you can fix it by replacing the line that sets @title in your show template with this one:

    <% title(@project.name, "Projects") %>
  • And update application.html.erb to reflect:
    <title>
      <% if content_for?(:title) %>
        <%= yield(:title) %>
      <% else %>
        Ticketee
      <% end %>
    </title>

Notes: Prepare Gems for the App

Notes taken from the book: Rails 3 in Action

  • Groups in the Gemfile are used to define gems that should be loaded in specific scenarios. When using Bundler with Rails, you can specify a gem group for each Rails environment, and by doing so, you specify which gems should be required by that environment.
  • This automatic requiring of gems inside the Rails environment groups is done by this line in config/application.rb:

            Bundler.require(:default, Rails.env) if defined?(Bundler)
  • To install these gems to your system, run bundle install –binstubs at the root of your application. It tells the Bundler gem to read your Gemfile and install the gems specified in it.
  • Next, you want to generate the skeleton for Cucumber. A generator can generate either static or dynamic content, depending on the generator. For the Cucumber skel- eton generator, it’s set content. To run this generator, you use the rails command

           rails generate cucumber:install
  • While you’re generating things, you may as well run the RSpec generator too:

            rails generate rspec:install

Notes: Ruby on Rails (Basics)

Notes taken from the book: Rails 3 in Action

Basics:

  • Ruby on Rails is a framework built on the Ruby language.
  • The Ruby language was created back in 1993 by Yukihiro “Matz” Matsumuto.
  • Ruby on Rails was created in 2004 by David Heinemeier Hansson during the devel- opment of 37signals’ flagship product: Basecamp. When Rails was needed for other 37signals projects, the team extracted the Rails code from it.
  • Ruby on Rails allows for rapid development of applications by using a concept known as convention over configuration.
  • The core features of Rails are a conglomerate of many different parts called Rail- ties (when said aloud it rhymes with “bowties”), such as Active Record, Active Support, Action Mailer, and Action Pack.
  • MVC in Rails is aided by REST, a routing paradigm. Representational State Transfer (REST) is the convention for routing in Rails. When something adheres to this conven- tion, it’s said to be RESTful. Routing in Rails refers to how requests are routed within the application itself.

First Application:

  • Use RVM (http://rvm.beginrescueend.com) to install Ruby and RubyGems.
  • Genrate an application: rails new things_i_bought
  • Starting the Application: cd things_i_bought -> bundle install -> rails server
  • Genrate scaffold: rails generate scaffold purchase name:string cost:float
  • Migrations: Migrations are used in Rails as a form of version control for the database, providing a way to implement incremental changes to the schema of the database. Each migration is timestamped right down to the second, which provides you (and anybody else devel- oping the application with you) an accurate timeline of your database.
  • respond_to method that defines what formats this action responds to. Here, the controller responds to the html and xml formats. The html method here isn’t given a block and so will render the template from app/views/ purchases/new.html.erb, whereas the xml method, which is given a block, will execute the code inside the block and return an XML version of the @purchase object.
  • A flash message is a message that can be displayed on the next request.
  • You can add validations to your model to ensure that the data conforms to certain rules or that data for a certain field must be present or that a number you enter must be above a certain other number. For example: validates_presence_of :name validates_numericality_of :cost, :greater_than => 0
  • Routing: The config/routes.rb file of every Rails application is where the application routes are defined. Inside the block for the draw method(in routes.rb) is the resources method. Collections of similar objects in Rails are referred to as resources. This method defines the routes and routing helpers.
  • By using the routing helpers introduced in Rails 2 and still available in Rails 3.1, you can have much shorter link_to calls in your application, increasing the readability of the code throughout.

Finally managed to finish the first chapter with a test application running on my iMac 🙂