snooze_force 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 markbates
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,25 @@
1
+ module SnoozeForce
2
+ class Base
3
+
4
+ attr_accessor :client
5
+ attr_accessor :options
6
+ attr_accessor :base
7
+ class << self
8
+ attr_accessor :sobject
9
+ end
10
+
11
+ def initialize(client, options = {})
12
+ self.client = client
13
+ self.options = {:base => ''}.merge(options)
14
+ self.base = self.options[:base]
15
+ end
16
+
17
+ %w{get post put delete head}.each do |verb|
18
+ define_method(verb) do |path = '/', options = {}|
19
+ path = File.join('sobjects', self.base, path)
20
+ self.client.send(verb, path, self.options.merge(options))
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,91 @@
1
+ module SnoozeForce
2
+ class Client
3
+ include HTTParty
4
+
5
+ attr_accessor :instance_url
6
+ attr_accessor :token
7
+ attr_accessor :uid
8
+ attr_accessor :refresh_token
9
+ attr_accessor :client_id
10
+ attr_accessor :client_secret
11
+ attr_accessor :options
12
+
13
+ def initialize(options = {})
14
+ self.instance_url = options[:instance_url]
15
+ self.token = options[:token]
16
+ self.uid = options[:uid]
17
+ self.refresh_token = options[:refresh_token]
18
+ self.client_id = options[:client_id]
19
+ self.client_secret = options[:client_secret]
20
+ self.options = {:headers => {'Content-Type' => 'application/json'}}.merge(options)
21
+
22
+ if self.uid && !SnoozeForce.const_defined?("U#{self.uid}")
23
+ self.rebuild_sobjects!
24
+ end
25
+
26
+ end
27
+
28
+ def rebuild_sobjects!
29
+ # Let's build all the available classes for this uid:
30
+ res = self.get('sobjects')
31
+ res['sobjects'].each do |sobject|
32
+ name = sobject['name']
33
+ eval <<-EOF
34
+ module ::SnoozeForce
35
+ module U#{self.uid}
36
+ class #{name} < ::SnoozeForce::Base
37
+ def initialize(client, options = {})
38
+ super
39
+ self.base = '#{name}'
40
+ end
41
+ end
42
+ end
43
+ end
44
+ def self.#{name.underscore}
45
+ unless @_#{name.underscore}
46
+ @_#{name.underscore} = ::SnoozeForce::U#{self.uid}::#{name}.new(self)
47
+ end
48
+ return @_#{name.underscore}
49
+ end
50
+ EOF
51
+ "::SnoozeForce::U#{self.uid}::#{name}".constantize.sobject = sobject
52
+ "::SnoozeForce::U#{self.uid}::#{name}".constantize.send(:include, "::SnoozeForce::#{name}".constantize) if SnoozeForce.const_defined?(name)
53
+ end
54
+ end
55
+
56
+ def refresh!
57
+ 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}")
58
+ raise SnoozeForce::ResponseError.new(response.code, response['error_description']) if response.code != 200
59
+ self.token = response['access_token']
60
+ self.instance_url = response['instance_url']
61
+ response
62
+ end
63
+
64
+ def query(q = '', options = {})
65
+ self.get("/query?q=#{URI.escape(q)}", options)
66
+ end
67
+
68
+ def search(q = '', options = {})
69
+ self.get("/search?q=#{URI.escape(q)}", options)
70
+ end
71
+
72
+ %w{get post put delete head}.each do |verb|
73
+ define_method(verb) do |path = '/', options = {}|
74
+ spath = File.join(self.instance_url, '/services/data/v22.0', path)
75
+ puts "#{verb.upcase} #{spath}"
76
+ opts = self.options.merge(options)
77
+ opts[:headers]['Authorization'] = "OAuth #{self.token}"
78
+ opts[:body] = opts[:body].to_json if opts[:body]
79
+ response = self.class.send(verb, spath, opts)
80
+ if !(200..299).include?(response.code) && response.code != 401
81
+ raise SnoozeForce::ResponseError.new(response.code, response.first['message'])
82
+ elsif response.code == 401
83
+ self.refresh!
84
+ return self.send(verb, path, options)
85
+ end
86
+ return response
87
+ end
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,9 @@
1
+ module SnoozeForce
2
+ class ResponseError < StandardError
3
+
4
+ def initialize(code, message)
5
+ super("#{code}: #{message}")
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module SnoozeForce
2
+ module User
3
+
4
+ def me
5
+ self.get("#{self.client.uid}")
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require 'i18n'
4
+ require 'active_support/core_ext'
5
+
6
+ module SnoozeForce
7
+ end
8
+
9
+ Dir.glob(File.join(File.dirname(__FILE__), 'snooze_force', '**/*.rb')).each do |f|
10
+ require File.expand_path(f)
11
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snooze_force
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - markbates
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-07 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: httparty
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: i18n
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: activesupport
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 3.0.0
46
+ type: :runtime
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ description: "snooze_force was developed by: markbates"
50
+ email: ""
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - LICENSE
57
+ files:
58
+ - lib/snooze_force/base.rb
59
+ - lib/snooze_force/client.rb
60
+ - lib/snooze_force/errors.rb
61
+ - lib/snooze_force/user.rb
62
+ - lib/snooze_force.rb
63
+ - LICENSE
64
+ has_rdoc: true
65
+ homepage: ""
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options: []
70
+
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 4075874534427719343
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ requirements: []
89
+
90
+ rubyforge_project:
91
+ rubygems_version: 1.6.2
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: snooze_force
95
+ test_files: []
96
+