Skip to content

Commit 3978746

Browse files
committed
Basic structure.
1 parent 7e868c3 commit 3978746

File tree

7 files changed

+133
-4
lines changed

7 files changed

+133
-4
lines changed

Rakefile

+8
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
#!/usr/bin/env rake
22
require "bundler/gem_tasks"
3+
4+
task :default => [:spec]
5+
6+
require 'rspec/core/rake_task'
7+
desc "Run specs"
8+
RSpec::Core::RakeTask.new do |t|
9+
t.pattern = 'spec/**/*_spec.rb'
10+
end

lib/restforce.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
require "restforce/version"
1+
require 'faraday'
2+
require 'faraday_middleware'
23

3-
module Restforce
4-
# Your code goes here...
5-
end
4+
require 'restforce/version'
5+
require 'restforce/config'
6+
require 'restforce/client'

lib/restforce/client.rb

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Restforce
2+
class Client
3+
4+
def initialize(options)
5+
@options = {
6+
:username => Restforce.configuration.username,
7+
:password => Restforce.configuration.password,
8+
:security_token => Restforce.configuration.security_token,
9+
:client_id => Restforce.configuration.client_id,
10+
:client_secret => Restforce.configuration.client_secret,
11+
:host => Restforce.configuration.host,
12+
:api_version => Restforce.configuration.api_version,
13+
:oauth_token => Restforce.configuration.oauth_token,
14+
:refresh_token => Restforce.configuration.refresh_token,
15+
:instance_url => Restforce.configuration.instance_url
16+
}.merge(options) if options.is_a?(Hash)
17+
end
18+
19+
private
20+
21+
def connection
22+
@connection ||= Faraday.new(:url => "https://#{@options[:host]}") do |builder|
23+
builder.request :json
24+
builder.response :json
25+
builder.adapter Faraday.default_adapter
26+
end
27+
@connection.headers['Authorization'] = "OAuth #{oauth_token}" if oauth_token
28+
end
29+
30+
def oauth_token
31+
@options[:oauth_token]
32+
end
33+
34+
end
35+
end

lib/restforce/config.rb

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
module Restforce
2+
class << self
3+
attr_writer :log
4+
5+
# Returns the current Configuration
6+
#
7+
# Example
8+
#
9+
# Restforce.configuration.username = "username"
10+
# Restforce.configuration.password = "password"
11+
def configuration
12+
@configuration ||= Configuration.new
13+
end
14+
15+
# Yields the Configuration
16+
#
17+
# Example
18+
#
19+
# Restforce.configure do |config|
20+
# config.username = "username"
21+
# config.password = "password"
22+
# end
23+
def configure
24+
yield configuration
25+
end
26+
27+
def log?
28+
@log ||= false
29+
end
30+
31+
def log(message)
32+
return unless Restforce.log?
33+
Restforce.configuration.logger.send :debug, message
34+
end
35+
end
36+
37+
class Configuration
38+
attr_accessor :api_version
39+
# The username to use during login.
40+
attr_accessor :username
41+
# The password to use during login.
42+
attr_accessor :password
43+
# The security token to use during login.
44+
attr_accessor :security_token
45+
# The OAuth client id
46+
attr_accessor :client_id
47+
# The OAuth client secret
48+
attr_accessor :client_secret
49+
# Set this to true if you're authenticating with a Sandbox instance.
50+
# Defaults to false.
51+
attr_accessor :host
52+
53+
attr_accessor :oauth_token
54+
attr_accessor :refresh_token
55+
attr_accessor :instance_url
56+
57+
def initialize
58+
@api_version ||= '24.0'
59+
@host ||= 'login.salesforce.com'
60+
end
61+
62+
def logger
63+
@logger ||= ::Logger.new STDOUT
64+
end
65+
end
66+
end

restforce.gemspec

+3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ Gem::Specification.new do |gem|
1515
gem.require_paths = ["lib"]
1616
gem.version = Restforce::VERSION
1717

18+
gem.add_dependency 'rake'
1819
gem.add_dependency 'faraday', '~> 0.8.4'
20+
gem.add_dependency 'faraday_middleware', '~> 0.8.8'
21+
gem.add_dependency 'json', '~> 1.7.5'
1922

2023
gem.add_development_dependency 'rspec'
2124
gem.add_development_dependency 'webmock'

spec/lib/client_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'spec_helper'
2+
3+
describe Restforce::Client do
4+
let(:client_options) { { :oauth_token => 'token' } }
5+
let(:client) { Restforce::Client.new client_options }
6+
7+
it do
8+
puts client.send(:connection)
9+
end
10+
end

spec/spec_helper.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'bundler/setup'
2+
Bundler.require :default, :test
3+
4+
require 'webmock/rspec'
5+
6+
WebMock.disable_net_connect!

0 commit comments

Comments
 (0)