Giter VIP home page Giter VIP logo

inbox-ruby's Introduction

Inbox Ruby bindings

Installation

Add this line to your application's Gemfile:

gem 'inbox'

And then execute:

bundle

You don't need to use this repo unless you're planning to modify the gem. If you just want to use the Inbox SDK with Ruby bindings, you should run:

gem install inbox

##Requirements

  • Ruby 1.8.7 or above. (Ruby 1.8.6 may work if you load ActiveSupport.)

  • rest-client, json

Example Rails App

A small example Rails app is included in the example directory. You can run the sample app to see how an authentication flow might be implemented.

cd example

RESTCLIENT_LOG=stdout rails s

Note that you will need to replace the Inbox App ID and Secret in config/environments/development.rb to use the sample app.

Usage

App ID and Secret

Before you can interact with the Inbox API, you need to register for the Inbox Developer Program at http://www.inboxapp.com/. After you've created a developer account, you can create a new application to generate an App ID / Secret pair.

Generally, you should store your App ID and Secret into environment variables to avoid adding them to source control. That said, in the example project and code snippets below, the values were added to config/environments/development.rb for convenience.

Authentication

The Inbox API uses server-side (three-legged) OAuth, and the Ruby gem provides convenience methods that simplify the OAuth process. For more information about authenticating with Inbox, visit the Developer Documentation.

Step 1: Redirect the user to Inbox:

require 'inbox'

def login
    inbox = Inbox::API.new(config.inbox_app_id, config.inbox_app_secret, nil)
    # The email address of the user you want to authenticate
    user_email = '[email protected]'

    # This URL must be registered with your application in the developer portal
    callback_url = url_for(:action => 'login_callback')
    
    redirect_to inbox.url_for_authentication(callback_url, user_email)
end

Step 2: Handle the Authentication Response:

def login_callback 
    inbox = Inbox::API.new(config.inbox_app_id, config.inbox_app_secret, nil)
    inbox_token = inbox.auth_token_for_code(params[:code])

    # Save the inbox_token to the current session, save it to the user model, etc.
end

Fetching Namespaces

inbox = Inbox::API.new(config.inbox_app_id, config.inbox_app_secret, inbox_token)

# Get the first namespace
namespace = inbox.namespaces.first

# Print out the email address and provider (Gmail, Exchange)
puts namespace.email_address
puts namespace.provider

Fetching Threads

# Fetch the first thread
thread = namespace.threads.first

# Fetch a specific thread
thread = namespace.threads.find('ac123acd123ef123')

# List all threads tagged `inbox`
# (paginating 50 at a time until no more are returned.)
namespace.threads.where(:tag => 'inbox').each do |thread|
  puts thread.subject
end    

# List the 5 most recent unread threads
namespace.threads.where(:tag => 'unread').range(0,4).each do |thread|
  puts thread.subject
end    

# List all threads with '[email protected]'
namespace.threads.where(:any_email => '[email protected]').each do |thread|
  puts thread.subject
end    

# Collect all threads with '[email protected]' into an array.
# Note: for large numbers of threads, this is not advised.
threads = namespace.threads.where(:any_email => '[email protected]').all

Working with Threads

# List thread participants
thread.participants.each do |participant|
    puts participant['email']
end

# Mark as read
thread.mark_as_read!

# Archive
thread.archive!

# Add or remove arbitrary tags
tagsToAdd = ['inbox', 'cfa1233ef123acd12']
tagsToRemove = []
thread.update_tags!(tagsToAd, tagsToRemove)

# List messages
thread.messages.each do |message|
    puts message.subject
end

Working with Files

# List files
namespace.files.each do |file|
    puts file.filename
end

# Create a new file
file = namespace.files.build(:file => File.new("./public/favicon.ico", 'rb'))
file.save!

Working with Messages, Contacts, etc.

Each of the primary collections (contacts, messages, etc.) behave the same way as threads. For example, finding messages with a filter is similar to finding threads:

messages = namespace.messages.where(:to => 'ben@inboxapp.com`).all

The where method accepts a hash of filters, as documented in the Inbox Filters Documentation.

Creating and Sending Drafts

# Create a new draft
draft = namespace.drafts.build(
  :to => [{:name => 'Ben Gotow', :email => '[email protected]'}],
  :subject => "Sent by Ruby",
  :body => "Hi there!<strong>This is HTML</strong>"
)

# Modify attributes as necessary
draft.cc = [{:name => 'Michael', :email => '[email protected]'}]

# Add the file we uploaded as an attachment
draft.attach(file)

# Save the draft
draft.save!

# Send the draft. This method returns immediately and queues the message
# with Inbox for delivery through the user's SMTP gateway.
draft.send!

Open-Source Sync Engine

The Inbox Sync Engine is open-source, and you can also use the Ruby gem with the open-source API. Since the open-source API provides no authentication or security, connecting to it is simple. When you instantiate the Inbox object, provide nil for the App ID, App Secret, and API Token, and pass the fully-qualified address to your copy of the sync engine:

require 'inbox'
inbox = Inbox::API.new(nil, nil, nil, 'http://localhost:5555/')

Contributing

We'd love your help making the Inbox ruby gem better. Join the Google Group for project updates and feature discussion. We also hang out in ##inbox on irc.freenode.net, or you can email [email protected].

Please sign the Contributor License Agreement before submitting pull requests. (It's similar to other projects, like NodeJS or Meteor.)

The Inbox ruby gem uses Jeweler for release management. When you're ready to release a new version, do something like this:

rake version:bump:minor
rake release

Tests can be run with:

rspec spec

inbox-ruby's People

Contributors

bengotow avatar grinich avatar

Stargazers

Akin Owopetu avatar

Watchers

James Cloos avatar Akin Owopetu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.