Class: SnoozeForce::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/snooze_force/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/snooze_force/client.rb', line 13

def initialize(options = {})
  self.instance_url = options[:instance_url]
  self.token = options[:token]
  self.uid = options[:uid]
  self.refresh_token = options[:refresh_token]
  self.client_id = options[:client_id]
  self.client_secret = options[:client_secret]
  self.options = {:headers => {'Content-Type' => 'application/json'}}.merge(options)
  
  self.rebuild_sobjects! if self.uid
  
end

Instance Attribute Details

#client_id ⇒ Object

Returns the value of attribute client_id.



9
10
11
# File 'lib/snooze_force/client.rb', line 9

def client_id
  @client_id
end

#client_secret ⇒ Object

Returns the value of attribute client_secret.



10
11
12
# File 'lib/snooze_force/client.rb', line 10

def client_secret
  @client_secret
end

#instance_url ⇒ Object

Returns the value of attribute instance_url.



5
6
7
# File 'lib/snooze_force/client.rb', line 5

def instance_url
  @instance_url
end

#options ⇒ Object

Returns the value of attribute options.



11
12
13
# File 'lib/snooze_force/client.rb', line 11

def options
  @options
end

#refresh_token ⇒ Object

Returns the value of attribute refresh_token.



8
9
10
# File 'lib/snooze_force/client.rb', line 8

def refresh_token
  @refresh_token
end

#token ⇒ Object

Returns the value of attribute token.



6
7
8
# File 'lib/snooze_force/client.rb', line 6

def token
  @token
end

#uid ⇒ Object

Returns the value of attribute uid.



7
8
9
# File 'lib/snooze_force/client.rb', line 7

def uid
  @uid
end

Instance Method Details

#query(q = '', options = {}) ⇒ Object



62
63
64
# File 'lib/snooze_force/client.rb', line 62

def query(q = '', options = {})
  self.get("/query?q=#{URI.escape(q)}", options)
end

#rebuild_sobjects! ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/snooze_force/client.rb', line 26

def rebuild_sobjects!
  # Let's build all the available classes for this uid:
  res = self.get('sobjects')
  res['sobjects'].each do |sobject|
    name = sobject['name']
    eval "      module ::SnoozeForce\n        module U\#{self.uid}\n          class \#{name} < ::SnoozeForce::Base\n            def initialize(client, options = {})\n              super\n              self.base = '\#{name}'\n            end\n          end\n        end\n      end\n      def self.\#{name.underscore}\n        unless @_\#{name.underscore}\n          @_\#{name.underscore} = ::SnoozeForce::U\#{self.uid}::\#{name}.new(self)\n        end\n        return @_\#{name.underscore}\n      end\n    EOF\n    \"::SnoozeForce::U\#{self.uid}::\#{name}\".constantize.sobject = sobject\n    \"::SnoozeForce::U\#{self.uid}::\#{name}\".constantize.send(:include, \"::SnoozeForce::\#{name}\".constantize) if SnoozeForce.const_defined?(name)\n  end\nend\n"

#refresh! ⇒ Object



54
55
56
57
58
59
60
# File 'lib/snooze_force/client.rb', line 54

def refresh!
  response = HTTParty.post("https://login.salesforce.com/services/oauth2/token?grant_type=refresh_token&client_id=#{self.client_id}&client_secret=#{self.client_secret}&refresh_token=#{self.refresh_token}")
  raise SnoozeForce::ResponseError.new(response.code, response['error_description']) if response.code != 200
  self.token = response['access_token']
  self.instance_url = response['instance_url']
  response
end

#search(q = '', options = {}) ⇒ Object



66
67
68
# File 'lib/snooze_force/client.rb', line 66

def search(q = '', options = {})
  self.get("/search?q=#{URI.escape(q)}", options)
end