restforce_mock 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 06ea14355b2b044432f0907fea9f7d1d420c227a
4
- data.tar.gz: 19cfc9b489c3c0bebac453c369e37d5fe8fd6a64
3
+ metadata.gz: d3ed3598ad940401022ec1701ec1c91b09ca5d09
4
+ data.tar.gz: c2a68ab7ce505b65bac4e2e2078b302b0a2fa902
5
5
  SHA512:
6
- metadata.gz: 998c8b961fc63decbc4818c0222c741448fe1ebda41143311172ef8130d78d813bd7801e157fb1ee63b48897a473b0b72a7397e8eb7e594088d724dbf4e161fa
7
- data.tar.gz: 432526d5f7cb41a78a3ccb5e1aa4981e62548ac1d80c4776996a76820215a5e019fdac62ca6d455be765c055d1fd984696ec529865974beb09e628a532ab6c8f
6
+ metadata.gz: 7f77758d6bfb940b068b0d0287a7a03846d250ac11fac45dbbe8c08ba6876c5b561f7a484ae1382bf2e5722ad6de3c474c2109e7515060df4bcfa3f39f396c2e
7
+ data.tar.gz: cc87ac19af54021bdae4298d6669b69d45e7ba884b016f9c760ed7a073f4e28e99b7fcba1a504981f41e21e07d35637e74bef25778bdc232d3d71f2c418ae09f
data/.gitignore CHANGED
@@ -7,3 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .env
11
+ restforce_mock-*.gem
data/.ruby-style.yml CHANGED
@@ -31,7 +31,7 @@ Style/AccessModifierIndentation:
31
31
  - indent
32
32
  Style/AlignHash:
33
33
  Description: Align the elements of a hash literal if they span more than one line.
34
- Enabled: true
34
+ Enabled: false
35
35
  EnforcedHashRocketStyle: key
36
36
  EnforcedColonStyle: key
37
37
  EnforcedLastArgumentHashStyle: always_inspect
@@ -51,7 +51,7 @@ Style/AlignParameters:
51
51
  Style/AndOr:
52
52
  Description: Use &&/|| instead of and/or.
53
53
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-and-or-or
54
- Enabled: true
54
+ Enabled: false
55
55
  EnforcedStyle: always
56
56
  SupportedStyles:
57
57
  - always
@@ -66,7 +66,7 @@ Style/BarePercentLiterals:
66
66
  - bare_percent
67
67
  Style/BracesAroundHashParameters:
68
68
  Description: Enforce braces style around hash parameters.
69
- Enabled: false
69
+ Enabled: true
70
70
  EnforcedStyle: no_braces
71
71
  SupportedStyles:
72
72
  - braces
@@ -98,7 +98,7 @@ Style/ClassCheck:
98
98
  Style/CollectionMethods:
99
99
  Description: Preferred collection methods.
100
100
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#map-find-select-reduce-size
101
- Enabled: true
101
+ Enabled: false
102
102
  PreferredMethods:
103
103
  collect: map
104
104
  collect!: map!
@@ -138,7 +138,7 @@ Style/EmptyLinesAroundBlockBody:
138
138
  - no_empty_lines
139
139
  Style/EmptyLinesAroundClassBody:
140
140
  Description: Keeps track of empty lines around class bodies.
141
- Enabled: true
141
+ Enabled: false
142
142
  EnforcedStyle: no_empty_lines
143
143
  SupportedStyles:
144
144
  - empty_lines
@@ -211,7 +211,7 @@ Style/IfUnlessModifier:
211
211
  Description: Favor modifier if/unless usage when you have a single-line body.
212
212
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#if-as-a-modifier
213
213
  Enabled: false
214
- MaxLineLength: 100
214
+ MaxLineLength: 80
215
215
  Style/IndentationWidth:
216
216
  Description: Use 2 spaces for indentation.
217
217
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#spaces-indentation
@@ -365,7 +365,7 @@ Style/SingleLineMethods:
365
365
  Style/StringLiterals:
366
366
  Description: Checks if uses of quotes match the configured preference.
367
367
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-string-literals
368
- Enabled: true
368
+ Enabled: false
369
369
  EnforcedStyle: double_quotes
370
370
  SupportedStyles:
371
371
  - single_quotes
@@ -373,14 +373,14 @@ Style/StringLiterals:
373
373
  Style/StringLiteralsInInterpolation:
374
374
  Description: Checks if uses of quotes inside expressions in interpolated strings
375
375
  match the configured preference.
376
- Enabled: true
376
+ Enabled: false
377
377
  EnforcedStyle: single_quotes
378
378
  SupportedStyles:
379
379
  - single_quotes
380
380
  - double_quotes
381
381
  Style/SpaceAroundBlockParameters:
382
382
  Description: Checks the spacing inside and after block parameters pipes.
383
- Enabled: true
383
+ Enabled: false
384
384
  EnforcedStyleInsidePipes: no_space
385
385
  SupportedStyles:
386
386
  - space
@@ -405,7 +405,7 @@ Style/SpaceInsideBlockBraces:
405
405
  Description: Checks that block braces have or don't have surrounding space. For
406
406
  blocks taking parameters, checks that the left brace has or doesn't have trailing
407
407
  space.
408
- Enabled: true
408
+ Enabled: false
409
409
  EnforcedStyle: space
410
410
  SupportedStyles:
411
411
  - space
@@ -415,7 +415,7 @@ Style/SpaceInsideBlockBraces:
415
415
  Style/SpaceInsideHashLiteralBraces:
416
416
  Description: Use spaces inside hash literal braces - or don't.
417
417
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#spaces-operators
418
- Enabled: true
418
+ Enabled: false
419
419
  EnforcedStyle: space
420
420
  EnforcedStyleForEmptyBraces: no_space
421
421
  SupportedStyles:
@@ -599,7 +599,7 @@ Style/InlineComment:
599
599
  Style/MethodCalledOnDoEndBlock:
600
600
  Description: Avoid chaining a method call on a do...end block.
601
601
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#single-line-blocks
602
- Enabled: false
602
+ Enabled: true
603
603
  Style/SymbolArray:
604
604
  Description: Use %i or %I for arrays of symbols.
605
605
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#percent-i
@@ -649,7 +649,7 @@ Style/Blocks:
649
649
  Description: Avoid using {...} for multi-line blocks (multiline chaining is always
650
650
  ugly). Prefer {...} over do...end for single-line blocks.
651
651
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#single-line-blocks
652
- Enabled: true
652
+ Enabled: false
653
653
  Style/CaseEquality:
654
654
  Description: Avoid explicit use of the case equality operator(===).
655
655
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-case-equality
@@ -782,7 +782,7 @@ Style/MultilineIfThen:
782
782
  Style/MultilineTernaryOperator:
783
783
  Description: 'Avoid multi-line ?: (the ternary operator); use if/unless instead.'
784
784
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-multiline-ternary
785
- Enabled: true
785
+ Enabled: false
786
786
  Style/NegatedIf:
787
787
  Description: Favor unless over if for negative conditions (or control flow or).
788
788
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#unless-for-negatives
@@ -843,7 +843,7 @@ Style/SelfAssignment:
843
843
  Style/SingleSpaceBeforeFirstArg:
844
844
  Description: Checks that exactly one space is used between a method name and the
845
845
  first argument for method calls without parentheses.
846
- Enabled: true
846
+ Enabled: false
847
847
  Style/SpaceAfterColon:
848
848
  Description: Use spaces after colons.
849
849
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#spaces-operators
@@ -873,7 +873,7 @@ Style/SpaceBeforeComma:
873
873
  Enabled: true
874
874
  Style/SpaceBeforeComment:
875
875
  Description: Checks for missing space between code and a comment on the same line.
876
- Enabled: true
876
+ Enabled: false
877
877
  Style/SpaceBeforeSemicolon:
878
878
  Description: No spaces before semicolons.
879
879
  Enabled: true
@@ -887,11 +887,11 @@ Style/SpaceBeforeModifierKeyword:
887
887
  Style/SpaceInsideBrackets:
888
888
  Description: No spaces after [ or before ].
889
889
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-spaces-braces
890
- Enabled: true
890
+ Enabled: false
891
891
  Style/SpaceInsideParens:
892
892
  Description: No spaces after ( or before ).
893
893
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-spaces-braces
894
- Enabled: true
894
+ Enabled: false
895
895
  Style/SpaceInsideRangeLiteral:
896
896
  Description: No spaces inside range literals.
897
897
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-space-inside-range-literals
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## 0.4.0
2
+
3
+ * Added ability to dump schema from Salesforce
4
+ * Use schema to determine which fields are required
5
+ * Removed `RestforceMock::Sandbox.add_required` method
data/README.md CHANGED
@@ -34,6 +34,16 @@ Restforce::Client = RestforceMock::Client
34
34
 
35
35
  This will direct all calls to `Restforce` to `RestforceMock`. Test as usual.
36
36
 
37
+ ### Configuration
38
+
39
+ ```ruby
40
+ RestforceMock.configure do |config|
41
+ config.schema_file = "spec/fixtures/schema.yml"
42
+ config.error_on_required = true # raise error if required field is not set
43
+ config.required_exclusions = [:LastModifiedById ...] # fields that should not be considered required
44
+ end
45
+ ```
46
+
37
47
  ### Mimicking Salesforce data
38
48
 
39
49
  To mimic Salesforce database, add some data to the `RestfoceMock` sandbox
@@ -55,13 +65,6 @@ after completion of tests make sure to clean up if necessary
55
65
  RestforceMock::Sandbox.reset!
56
66
  ```
57
67
 
58
- #### Required fields
59
-
60
- ```ruby
61
- RestforceMock::Sandbox.add_required(
62
- "Object__c", [:Section_Name__c, :Program__c ])
63
- ```
64
-
65
68
  ## Development
66
69
 
67
70
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,56 +1,23 @@
1
1
  require "restforce_mock/version"
2
+ require "restforce_mock/configuration"
3
+ require "restforce_mock/client"
2
4
  require "restforce_mock/sandbox"
3
- require "restforce"
5
+ require "restforce_mock/schema_manager"
4
6
 
5
7
  module RestforceMock
6
- class Client
7
-
8
- include ::Restforce::Concerns::API
9
- include RestforceMock::Sandbox
10
-
11
- def api_patch(url, attrs)
12
- url=~/sobjects\/(.+)\/(.+)/
13
- object=$1
14
- id=$2
15
- validate_presence!(object, id)
16
- update_object(object, id, attrs)
17
- end
18
-
19
- def api_post(url, attrs)
20
- url=~/sobjects\/(.+)/
21
- sobject = $1
22
- id = SecureRandom.urlsafe_base64(13) #duplicates possible
23
- validate_requires!(sobject, attrs)
24
- add_object(sobject, id, attrs)
25
- return Body.new(id)
26
- end
27
-
28
- def validate_requires!(sobject, attrs)
29
- return unless RestforceMock::Sandbox.storage[:required][sobject]
30
-
31
- missing = RestforceMock::Sandbox.storage[:required][sobject] - attrs.keys
32
- if missing.length > 0
33
- raise Faraday::Error::ResourceNotFound.new(
34
- "REQUIRED_FIELD_MISSING: Required fields are missing: #{missing}")
35
- end
36
- end
37
-
38
- def validate_presence!(object, id)
39
- unless RestforceMock::Sandbox.storage[object][id]
40
- raise Faraday::Error::ResourceNotFound.new("Provided external ID field does not exist or is not accessible: #{id}")
41
- end
42
- end
8
+ class << self
9
+ attr_writer :configuration
10
+ end
43
11
 
44
- private
12
+ def self.configuration
13
+ @configuration ||= RestforceMock::Configuration.new
14
+ end
45
15
 
46
- class Body
47
- def initialize(id)
48
- @body = {'id' => id}
49
- end
16
+ def self.reset
17
+ @configuration = RestforceMock::Configuration.new
18
+ end
50
19
 
51
- def body
52
- @body
53
- end
54
- end
20
+ def self.configure
21
+ yield(configuration)
55
22
  end
56
23
  end
@@ -0,0 +1,67 @@
1
+ require "restforce"
2
+ require "restforce_mock/sandbox"
3
+
4
+ module RestforceMock
5
+ class Client
6
+
7
+ include ::Restforce::Concerns::API
8
+ include RestforceMock::Sandbox
9
+
10
+ def api_patch(url, attrs)
11
+ url=~/sobjects\/(.+)\/(.+)/
12
+ object=$1
13
+ id=$2
14
+ validate_presence!(object, id)
15
+ update_object(object, id, attrs)
16
+ end
17
+
18
+ def api_post(url, attrs)
19
+ url=~/sobjects\/(.+)/
20
+ sobject = $1
21
+ id = SecureRandom.urlsafe_base64(13) #duplicates possible
22
+ validate_requires!(sobject, attrs)
23
+ add_object(sobject, id, attrs)
24
+ return Body.new(id)
25
+ end
26
+
27
+ def validate_requires!(sobject, attrs)
28
+ return unless RestforceMock.configuration.schema_file
29
+ return unless RestforceMock.configuration.error_on_required
30
+
31
+ object_schema = schema[sobject]
32
+ required = object_schema.
33
+ select{|k,v|v[:required]}.
34
+ collect{|k,v|k}.
35
+ collect(&:to_sym)
36
+
37
+ missing = required - attrs.keys - RestforceMock.configuration.required_exclusions
38
+ if missing.length > 0
39
+ raise Faraday::Error::ResourceNotFound.new(
40
+ "REQUIRED_FIELD_MISSING: Required fields are missing: #{missing}")
41
+ end
42
+ end
43
+
44
+ def validate_presence!(object, id)
45
+ unless RestforceMock::Sandbox.storage[object][id]
46
+ msg = "Provided external ID field does not exist or is not accessible: #{id}"
47
+ raise Faraday::Error::ResourceNotFound.new(msg)
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def schema
54
+ RestforceMock::SchemaManager.new.load_schema(RestforceMock.configuration.schema_file)
55
+ end
56
+
57
+ class Body
58
+ def initialize(id)
59
+ @body = {'id' => id}
60
+ end
61
+
62
+ def body
63
+ @body
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,24 @@
1
+ module RestforceMock
2
+ class Configuration
3
+ attr_accessor :schema_file
4
+ attr_writer :error_on_required
5
+ attr_writer :required_exclusions
6
+
7
+ def error_on_required
8
+ @error_on_required.nil? ? true : @error_on_required
9
+ end
10
+
11
+ def required_exclusions
12
+ @required_exclusions || default_exclusions
13
+ end
14
+
15
+ private
16
+
17
+ def default_exclusions
18
+ [
19
+ :Id, :IsDeleted, :Name, :CreatedDate, :CreatedById,
20
+ :LastModifiedDate, :LastModifiedById, :SystemModstamp
21
+ ]
22
+ end
23
+ end
24
+ end
@@ -8,12 +8,6 @@ module RestforceMock
8
8
  storage[name].merge!({ id => values })
9
9
  end
10
10
 
11
- def self.add_required(name, keys)
12
- if keys
13
- storage[:required][name] = keys
14
- end
15
- end
16
-
17
11
  def add_object(name, id, values)
18
12
  RestforceMock::Sandbox.add_object(name, id, values)
19
13
  end
@@ -42,14 +36,8 @@ module RestforceMock
42
36
 
43
37
  #Private
44
38
  def self.update_schema(object_name)
45
- s = client.describe(object_name)
46
- object_schema = {}
47
- s["fields"].each do |field|
48
- object_schema[field["name"]]= {
49
- type: field["type"]
50
- }
51
- end
52
- storage[:schema][object_name] = object_schema
39
+ s = RestforceMock::SchemaManager.new
40
+ storage[:schema][object_name] = s.get_schema(object_name)
53
41
  end
54
42
 
55
43
  def self.client
@@ -60,9 +48,6 @@ module RestforceMock
60
48
  storage = Hash.new do |hash, object|
61
49
  hash[object]={}
62
50
  end
63
- storage[:required] = Hash.new do |hash, object|
64
- hash[object]={}
65
- end
66
51
  storage[:schema] = Hash.new do |hash, object|
67
52
  hash[object]={}
68
53
  end
@@ -0,0 +1,57 @@
1
+ begin
2
+ require "yaml"
3
+ rescue LoadError
4
+ end
5
+
6
+ module RestforceMock
7
+ class SchemaManager
8
+
9
+ def initialize(client = default_client )
10
+ @client = client
11
+ end
12
+
13
+ def default_client
14
+ ::Restforce.new
15
+ end
16
+
17
+ # Get schema for Salesforce Object
18
+ #
19
+ # object_name - String
20
+ #
21
+ # Returns
22
+ #
23
+ # Hash
24
+ def get_schema(object_name)
25
+ s = @client.describe(object_name)
26
+ object_schema = {}
27
+ s["fields"].each do |field|
28
+ object_schema[field["name"]]= {
29
+ type: field["type"],
30
+ # http://salesforce.stackexchange.com/questions/25233/is-there-a-way-to-find-required-fields-on-an-objects
31
+ required: field["createable"] && !field["nillable"] && !field["defaultedOnCreate"]
32
+ }
33
+ end
34
+ object_schema
35
+ end
36
+
37
+ # Dump schema into file
38
+ #
39
+ # object_names - Array[String] array of name of objects in SF
40
+ # file - String yml file
41
+ def dump_schema(object_names, file)
42
+ schema = {}
43
+ object_names.each do |o|
44
+ schema[o] = get_schema(o)
45
+ end
46
+ File.open(file, 'w') {|f| f.write schema.to_yaml }
47
+ end
48
+
49
+ def load_schema(file)
50
+ if file.nil?
51
+ raise "Schema file is not defined"
52
+ end
53
+ thing = YAML.load_file(file)
54
+ end
55
+
56
+ end
57
+ end
@@ -1,3 +1,3 @@
1
1
  module RestforceMock
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -23,4 +23,6 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "bundler", "~> 1.10"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
25
  spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "dotenv"
26
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restforce_mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Katz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-09 00:00:00.000000000 Z
11
+ date: 2015-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: restforce
@@ -66,6 +66,34 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: dotenv
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
69
97
  description: Mock for Restforce gem
70
98
  email:
71
99
  - ilyakatz@gmail.com
@@ -78,6 +106,7 @@ files:
78
106
  - ".rspec"
79
107
  - ".ruby-style.yml"
80
108
  - ".travis.yml"
109
+ - CHANGELOG.md
81
110
  - CODE_OF_CONDUCT.md
82
111
  - Gemfile
83
112
  - LICENSE.txt
@@ -86,7 +115,10 @@ files:
86
115
  - bin/console
87
116
  - bin/setup
88
117
  - lib/restforce_mock.rb
118
+ - lib/restforce_mock/client.rb
119
+ - lib/restforce_mock/configuration.rb
89
120
  - lib/restforce_mock/sandbox.rb
121
+ - lib/restforce_mock/schema_manager.rb
90
122
  - lib/restforce_mock/version.rb
91
123
  - restforce_mock.gemspec
92
124
  homepage: https://github.com/ilyakatz
@@ -109,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
141
  version: '0'
110
142
  requirements: []
111
143
  rubyforge_project:
112
- rubygems_version: 2.2.2
144
+ rubygems_version: 2.2.0
113
145
  signing_key:
114
146
  specification_version: 4
115
147
  summary: Mock for Restforce gem