Tag Archives: Ruby on Rails

Adding a Contact to Infusionsoft in a Ruby on Rails App ( or: Why I’ll Never do Business with “Confusionsoft” Again)

Infusionsoft is a popular CRM and that is unfortunate.

I had a specification that required new user’s emails to get sent to an Infusionsoft list.

This appears to be a relatively easy task, right? They even have a Ruby gem, which I added to my Gemfile:

gem 'infusionsoft'

Then I performed the required config:

# Added to your config\initializers file
Infusionsoft.configure do |config|
  config.api_url = ENV['INFUSIONSOFT_URL'] # example infused.infusionsoft.com
  config.api_key = ENV['INFUSIONSOFT_API_KEY']
  config.api_logger = Logger.new("#{Rails.root}/log/infusionsoft_api.log") # optional logger file
end

And to test, I ran the recommended scripts in my rails console:

# Add a new Contact
Infusionsoft.contact_add({:FirstName => 'first_name', :LastName => 'last_name', :Email => 'test@test.com'})

No dice. I get this error:

SocketError: getaddrinfo: nodename nor servname provided, or not known
from /Users/Justus/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:879:in `initialize'

Hmm. I wonder why? I do a bit of research. Let’s check the API docs for infusionsoft:

Infusionsoft API Getting Started Documentation

 

Hmm. The ruby gem’s documentation make’s no mention of client_id or redirect_uri. Maybe they mean API key?

I continue digging and find that Infusionsoft has a “developer portal” that requires a separate registration. Maybe I’ll get what I need from there…

Nope. I register my application and receive an “application key” and a “secret”.

What is a secret? Nobody’s mentioned a secret. Why do I need a separate account to use the API? Will be able to access my original account’s campaign and how?

I have a lot of questions, so I call Infusionsoft’s technical support:

“Hi Infusionsoft, I’d like to speak with someone about integrating your API with an existing Rails App.”

“Please hold.”*

*intolerably long hold*

“I’m sorry sir, we don’t have technical support services, please checkout our community forum for assistance with the API.”

Oh, ok then.

Let’s talk about their community forums.

I register for an account. I confirm my email…

I should be able to create a thread, no?

No.

I have to wait for a moderator to confirm my registration.

Fast-forward 24 hours.

I have a deadline and need to get this card completed. My forum registration is still restricted. StackOverflow and GitHub issues have yielded no useful responses.

I make a simple email form in the infusionsoft campaign builder and examine the HTML it provides:

Webform” />

Email *

Time for a hack.

Let’s dissect this HTML. We have form data being passed via POST request. Here are the pertinent key- value pairs:

inf_form_xid : SUPER_SECRET_NUMBER

inf_form_name : ‘Webform’

infusionsoft_version : ‘1.34.0.35’

inf_field_Email : “#{user.email}”

Ah now we’re getting somewhere. I test the POST request in Postman and receive HTML responses. If the email is invalid or a duplicate, the response is a form with error alerts. I already validate those in the rails app so I shouldn’t have to worry.

I use HTTParty to facilitate the request in a Rails service object called AddContactToInfusionsoft.rb

class AddContactToInfusionsoft
 def self.call(user)
   HTTParty.post("https://ns166.infusionsoft.com/app/form/process/961b755cc1de68e2d549824cf11890d5",
     query: 
     {
       inf_form_xid: '961b755cc1de68e2d549824cf11890d5',
       inf_form_name: 'Webform in Content App',
       infusionsoft_version: '1.34.0.35',
       inf_field_Email: '#{user.email}'
     }
   )
 end
end

I test the code in my rails console and it works fine. Push it and you should be all set.

*Paraphrasing obviously.

My Favorite Free Ruby and Rails Resources

Metaprogramming Ruby by Paolo Perrotta

This excellent book is worth buying. Great for beginner and intermediate programmers. He makes the important distinction between metaprogramming: writing code that writes code, e.g. generators, and metaprogramming: writing code into the language in order to solve problems.

Rails Guides

I was lucky to have a mentor that stressed the importance of the Rails Guides to me as I learned. As someone who now teaches others about Ruby and Rails. I can’t stress this resource enough. Read them from start to finish and you’ll already know as much as a lot of Jr. Rails Developers.

Chris Pine’s Learning to Program (Ruby)

The best beginner tutorial for Ruby that I’m aware of. Less than a year ago, I burnt through this book in just a few hours of dedicated study. At the time the only experience I’ve had with programming is what I had done on Code Academy: less than half the Ruby course. Which leads me to my next amazing resource…

Codecademy

The platform has bugs sometimes, and isn’t always the most intuitive, but the game theory behind it’s badge system is solid. I owe a lot to Codecademy. I’ve spent many hours working through tutorials in many of their paths and I am proud to have a 69-point one-day record.

StackOverflow

SO is a phenomenal community and resource for programmers studying just about any language. If you haven’t signed up yet, do so!

GitHub

I’m not even sure I should mention this because it’s so obvious to me now, but it would be silly to think novices are universally aware of it. GitHub is the coder’s social network. It’s where we share our code-bases and help each other with projects. There are other places that are kind of like it, but GitHub is really the de facto industry standard and incredibly effective at what it strives to achieve. I’ve learned more from looking at other people’s code than I could possibly quantify.

PROTIPS: When looking at new source code for the first time, start in the specs(test) folder. The acceptance tests should give you some sort of idea what value the application provides to the user. The unit tests will tell you what the different parts are supposed to do. As Rails developers we’re extremely lucky to be a part of a community that values testing and puts an emphasis on it. I once read that great tests make good documentation. From there I usually examine the schema.db (in a rails app) and begin poking around the Gemfile and the routes and the MVC architecture.

MORE PROTIPS: Follow me on GitHub, and contribute to SelfGovern!

[YOUR CITY] Ruby Group

I try to go to at least a couple Ruby meetups every month. Honestly, that’s probably not even enough. If I was really smart, I’d go to one every weekend. If you’re in Boston, the meetup here is insane! Well over a hundred developers frequent the BostonRB meetups every month and you’d be hard pressed to find a friendlier group of people.

Ruby Weekly

Sign. Up. For. This. Newsletter. I’m serious. Every week I receive this wonderful compilation of relevant and late-breaking study material. If you’re serious about diving into Ruby and the surround community YOU WILL SIGN UP FOR THIS NEWSLETTER. I promise you, I would not lead you to a spammy, useless, annoying, data-mining, marketing ploy that so many newsletters tend to be. These gems (haha get it?) are packed with articles and blog posts from developers across the skill spectrum and across the country (world?).

Crafting Rails 4 Applications

Definitely for developers who’ve been coding a bit longer and have a solid grasp on Rails. Check it out, it’s current, Rails 4 isn’t going anywhere until at least this summer.

I’ll probably add more to this list as I see fit…

Integrating Github OAuth into an existing Rails application

The students at Launch Academy love BostonRB. A couple of times a month, about half of my forty-person cohort floods into some local tech company’s office to take in some educational talks and low-pressure networking. It’s a really good time, and a few of us want to give back to the organization that’s welcomed us so warmly.

So we decided to overhaul the website. Together with a select few of my other cohort members, we tackled the administrative interface of the website and took it by storm. Confession: this isn’t actually me talking. This is my friend, Lorry, in a surprise blog invasion! Hi!

Show me your O-Face

If you want to follow along with this tutorial. Fork this branch and start from the same file that we did!

We’re going to be walking through the process of adding Omniauth Github authorization to the BostonRB site, but the series of events should apply to most Rails apps. So here we go!

First, go into your Github settings and create an application. This is where you get your Client ID and Client Secret token from Github, which you will need very soon. Fill in the new Application form, and you should be taken to a page that has your information.

IMPORTANT

Make sure your Authorization callback URL:
YOUR URL/auth/github/callback

For the purposes of development, do:
http://localhost:3000/auth/github/callback

If you don’t set it up in this manner, there is a strong possibility it wont work.

Next thing you do is add these gems to the Gemfile in the root directory:

gem 'omniauth-github'

group :test, :development do
gem 'dotenv-rails'
end

Bundle Install, and you’re ready to go!

You need to create a .env file in the root directory of your app. In that file,  set environment variables for your secret key like so:

export GITHUB_KEY= your github key here
export GITHUB-SECRET= your github secret token here

We don’t want people to know what your secret token is otherwise they can feign authorization. So add .env to your .gitignore file before you commit!

Next, you’ll want to perform a rails generate model user github_uid:string name:string display_name:string

^[Justus Note:] This generation will make your User model and migration, which you will edit a bit later.

Before you migrate, go into the migration file and set github_uid and name to null:false. Also, add the current index after you created the table.

class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :github_uid, null: false
t.string :name, null: false
t.string :display_name
t.timestamps
end
add_index "users", ["github_uid"], name: "index_users_on_github_uid", unique: true, using: :btree
end
end

After that, migrate. Look at you, you’re adding omniauth like a pro!

Create a new file in config/initializers called omniauth.rb:

Rails.application.config.middleware.use OmniAuth::Builder do
provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET']
end

That’s calling upon the environment variables that you had just set in .env. We’ve come full circle.

Then, still in /config, enter the routes.rb file. There, add:

get '/auth/:provider/callback', to: 'sessions#create'

This URL needs to match the github callback url you had set when creating your Github client ID and token.

The configuration is done. Now to implement it.

Create a controller to handle your users when they have been signed in called ‘SessionsController’ It should be found in your app/controllers/ directory and called sessions_controller.rb

class SessionsController < ApplicationController
def create
redirect_to root_path, notice: 'You have successfully signed in!'
end
end

You’re just about done! Remember to validate the presence and uniqueness of github_uid, :name, and only the presence of github_token in your users model. An example of what your model should look like is

class User < ActiveRecord::Base
validates_presence_of :github_uid, :name, :github_token
validates_uniqueness_of :github_uid, :display_name
end

And, tada! You’re all set! Just remember to add the link to the your view, like so,

<%= link_to 'Sign In With GitHub', '/auth/github' %>

And you should be all set! Launch that server with Rails S and test that sucker out!

Distracker is Live

“SQUIRREL!”

If you’re anything like me, the internet has destroyed your focus.

BRB, need to make eggs in a basket smothered in cheese and mirepoix.

What was I saying?

Oh yes, distractions.

So as readers of this blog might know (all three of you), Launch Academy curriculum mandates the design and execution of a web app known to Launchers as a “breakable toy”. The idea is to teach students the Ruby on Rails framework through doing. Personally doing is my preferred mode of skill acquisition, thus, my favorite part of the program.

I decided early on that Distracker would be my breakable toy (though it’s gone through many names over the last few months).

It began as a terminal program (ask me if you want it) and then, to qualify as a breakable toy, I expanded the concept into an (almost)full-fledged web application. I’m still considering releasing the terminal program as a gem for developers.

In all, I restarted development on Distracker at least half a dozen times. It’s been a great learning experience and I intend to continue building it and adding features.

If you’re a developer, please fork the github repo and submit a pull request. I’m still a new developer and there is only so much I know how to do.

I welcome comments, suggestions and critique. Either here in the comments or through Github as an issue.

Your help would be greatly appreciated in my effort to track and eliminate this:
Image