Giter Site home page Giter Site logo

restforce's Introduction

Restforce travis-ci Code Climate

Restforce is a ruby gem for the Salesforce REST api. It's meant to be a lighter weight alternative to the databasedotcom gem.

It attempts to solve a couple of key issues that the databasedotcom gem has been unable to solve:

  • Support for interacting with multiple users from different orgs.
  • Support for parent-to-child relationships.
  • Support for aggregate queries.
  • Remove the need to materialize constants.
  • Support for the Streaming API
  • Support for blob data types.
  • A clean and modular architecture using Faraday middleware

Installation

Add this line to your application's Gemfile:

gem 'restforce'

And then execute:

$ bundle

Or install it yourself as:

$ gem install restforce

Usage

Restforce is designed with flexibility and ease of use in mind. By default, all api calls will return Hashie::Mash objects, so you can do things like client.query('select Id, (select Name from Children__r) from Account').Children__r.first.Name.

Initialization

Which authentication method you use really depends on your use case. If you're building an application where many users from different orgs are authenticated through oauth and you need to interact with data in their org on their behalf, you should use the OAuth token authentication method.

If you're using the gem to interact with a single org (maybe you're building some salesforce integration internally?) then you should use the username/password authentication method.

OAuth token authentication

client = Restforce::Client.new :oauth_token => 'oauth token',
  :instance_url  => 'instance url'

Although the above will work, you'll probably want to take advantage of the (re)authentication middleware by specifying a refresh token, client id and client secret:

client = Restforce::Client.new :oauth_token => 'oauth token',
  :refresh_token => 'refresh token',
  :instance_url  => 'instance url',
  :client_id     => 'client_id',
  :client_secret => 'client_secret'

Username/Password authentication

If you prefer to use a username and password to authenticate:

client = Restforce::Client.new :username => 'foo',
  :password       => 'bar',
  :security_token => 'security token'
  :client_id      => 'client_id',
  :client_secret  => 'client_secret'

Sandbox Orgs

You can connect to sandbox orgs by specifying a host. The default host is 'login.salesforce.com':

client = Restforce::Client.new :host => 'test.salesforce.com'

Global configuration

You can set any of the options passed into Restforce::Client.new globally:

Restforce.configure do |config|
    config.client_id     = ENV['SALESFORCE_CLIENT_ID']
    config.client_secret = ENV['SALESFORCE_CLIENT_SECRET']
end

Query

accounts = client.query("select Id, Something__c from Account where Id = 'someid'")
# => #<Restforce::Collection >

account = records.first
# => #<Restforce::SObject >

account.Id
# => "someid"

account.Name = 'Foobar'
account.save
# => true

account.destroy
# => true

Search

# Find all occurrences of 'bar'
client.search('FIND {bar}')
# => #<Restforce::Collection >

# Find accounts match the term 'genepoint' and return the Name field
client.search('FIND {genepoint} RETURNING Account (Name)').map(&:Name)
# => ['GenePoint']

Create

# Add a new account
client.create('Account', Name: 'Foobar Inc.')
# => '0016000000MRatd'

Update

# Update the Account with Id '0016000000MRatd'
client.update('Account', Id: '0016000000MRatd', Name: 'Whizbang Corp')
# => true

Destroy

# Delete the Account with Id '0016000000MRatd'
client.destroy('Account', '0016000000MRatd')
# => true

File Uploads

Using the new Blob Data api feature (500mb limit):

client.create 'Document', FolderId: '00lE0000000FJ6H',
  Description: 'Document test',
  Name: 'My image',
  Body: Restforce::UploadIO.new(File.expand_path('image.jpg', __FILE__), 'image/jpeg'))

Using base64 encoded data (37.5mb limit):

client.create 'Document', FolderId: '00lE0000000FJ6H',
  Description: 'Document test',
  Name: 'My image',
  Body: Base64::encode64(File.read('image.jpg'))

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

restforce's People

Contributors

ejholmes avatar

Watchers

James Cloos avatar  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.