fetching 0.4.1

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.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ quickfix.out
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fetching.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Michael Gee
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Fetching
2
+
3
+ Turn deeply nested hashes and arrays (like an API response) into an whiney object with method syntax.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ Fetching(HTTParty.get(url, query: query)).forms[0].request_form_id
9
+
10
+ # fails loudly unless the response from HTTParty has a top level `forms` key
11
+ # fails loudly if `forms` is an empty array
12
+ # fails loudly if the first `form` doesn't have a `request_form_id` key
13
+ ```
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ gem 'fetching', git: 'https://github.com/covermymeds/fetching-gem.git'
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/fetching.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fetching/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fetching"
8
+ spec.version = Fetching::VERSION
9
+ spec.authors = ["Michael Gee", "Mark Lorenz"]
10
+ spec.email = ["mgee@covermymeds.com", "mlorenz@covermymeds.com"]
11
+ spec.description = %q{More sass in more structs.}
12
+
13
+ spec.summary = <<-HEREDOC
14
+ This gem is a work in progress. The implementation code is not what's
15
+ important. What is important: Don't de-serialize API responses in to
16
+ hashes and arrays. Use a "strict" object that inforces key presence,
17
+ and array bounds.}
18
+ HEREDOC
19
+
20
+ spec.homepage = ""
21
+ spec.license = "MIT"
22
+
23
+ spec.files = `git ls-files`.split($/)
24
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.3"
29
+ spec.add_development_dependency "rake"
30
+ spec.add_development_dependency "rspec"
31
+ spec.add_development_dependency "pry-plus"
32
+ end
@@ -0,0 +1,3 @@
1
+ class Fetching
2
+ VERSION = "0.4.1"
3
+ end
data/lib/fetching.rb ADDED
@@ -0,0 +1,94 @@
1
+ require "json"
2
+
3
+ module Kernel
4
+
5
+ def Fetching(arg)
6
+ Fetching.from(arg)
7
+ end
8
+
9
+ end
10
+
11
+ class Fetching
12
+
13
+ WHITELIST = %w[ define_singleton_method class object_id
14
+ == inspect to_s instance_variables instance_eval
15
+ instance_variable_get ]
16
+ all_methods = instance_methods.map(&:to_s).grep(/\A[^_]/)
17
+ (all_methods - WHITELIST).each(&method(:undef_method))
18
+
19
+ def self.from(value)
20
+ case value
21
+ when ->(v) { v.respond_to? :to_ary }
22
+ FetchingArray.new(value.to_ary)
23
+ when ->(v) { v.respond_to? :to_hash }
24
+ FetchingHash.new(value.to_hash)
25
+ else
26
+ value
27
+ end
28
+ end
29
+
30
+ def self.from_json json, closure
31
+ from(JSON.parse json).__send__ closure
32
+ end
33
+
34
+ def initialize table
35
+ @table = table
36
+ end
37
+
38
+ def ==(other)
39
+ other.hash == hash
40
+ end
41
+
42
+ def hash
43
+ self.class.hash ^ @table.hash
44
+ end
45
+
46
+ private
47
+
48
+ def no_method key
49
+ end
50
+
51
+ end
52
+
53
+ class FetchingArray < Fetching
54
+
55
+ include Enumerable
56
+
57
+ def [](index)
58
+ Fetching.from @table.fetch(index)
59
+ end
60
+
61
+ def each
62
+ @table.each_index do |i|
63
+ yield self[i]
64
+ end
65
+ end
66
+
67
+ end
68
+
69
+ class FetchingHash < Fetching
70
+
71
+ def initialize *args
72
+ super
73
+ make_methods
74
+ end
75
+
76
+ def to_hash
77
+ @table.dup
78
+ end
79
+
80
+ private
81
+
82
+ def make_methods
83
+ @table.each do |k, v|
84
+ define_singleton_method(k) do
85
+ Fetching.from(v)
86
+ end
87
+ end
88
+ end
89
+
90
+ def method_missing key, *args, &block
91
+ fail NoMethodError, "#{key} not found\nyou have:\n#{@table.inspect}"
92
+ end
93
+
94
+ end
@@ -0,0 +1,78 @@
1
+ require "spec_helper"
2
+
3
+ describe Fetching do
4
+
5
+ let(:input) { { one: 1, two: two, ary: ary, object_ary: object_ary } }
6
+ let(:two) { { "two"=>2 } }
7
+ let(:ary) { [1, 2] }
8
+ let(:object_ary) { [{}, last_object] }
9
+ let(:last_object) { { three: 3 } }
10
+
11
+ subject { Fetching(input) }
12
+ specify("#one") { expect(subject.one).to eq(1) }
13
+ specify("#two") { expect(subject.two).to eq(Fetching(two)) }
14
+ specify("#ary") { expect(subject.ary).to eq(Fetching(ary)) }
15
+ specify "objects in arrays" do
16
+ expect(subject.object_ary[1].three).to eq(3)
17
+ end
18
+
19
+ describe "an unknown hash key" do
20
+ it "raises NoMethodError" do
21
+ expected_message = "not_a_key not found\nyou have:\n{:one=>1, :two=>{\"two\"=>2}, :ary=>[1, 2], :object_ary=>[{}, {:three=>3}]}"
22
+
23
+ expect{ subject.not_a_key }.to raise_error(NoMethodError, expected_message)
24
+ end
25
+ end
26
+
27
+ describe "an unknown array index" do
28
+ it "raises NoMethodError" do
29
+ expected_message = "index 3 outside of array bounds: -2...2"
30
+ expect{ subject.ary[ary.size + 1] }.to raise_error(IndexError, expected_message)
31
+ end
32
+ end
33
+
34
+ describe "a bad closure" do
35
+ it "raises the expected error" do
36
+ expect { Fetching.from_json("{}", :not_a_key) }.to raise_error(NoMethodError)
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ describe FetchingHash do
43
+
44
+ specify "#to_hash" do
45
+ hash = {one: 1, two: 2}
46
+ sassy_hash = Fetching(hash)
47
+ expect(sassy_hash.to_hash).to eq(hash)
48
+ end
49
+
50
+ specify "#to_hash doesn't allow you to break fetching" do
51
+ sassy_hash = Fetching(one: 1, two: 2)
52
+ hash = sassy_hash.to_hash
53
+ hash[:one] = ":)"
54
+ expect(sassy_hash.to_hash[:one]).to eq(1)
55
+ end
56
+
57
+ specify "#to_hash does a deep copy" do
58
+ hash = {one: 1, two: {three: 3}}
59
+ sassy_hash = Fetching(hash)
60
+ expect(sassy_hash.to_hash).to eq(hash)
61
+ end
62
+
63
+ end
64
+
65
+ describe FetchingArray do
66
+
67
+ specify "#map" do
68
+ ary = [1, 2]
69
+ sassy_ary = Fetching(ary)
70
+ expect(sassy_ary.map(&:to_s)).to eq(%w[1 2])
71
+ end
72
+
73
+ specify "Sassiness should go deep" do
74
+ Fetching([{one: 1}]).each do |element|
75
+ expect(element.one).to eq(1)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,13 @@
1
+ require "fetching"
2
+ require "rspec/autorun"
3
+ require "pathname"
4
+
5
+ Pathname(__FILE__).dirname.join("support").each_child { |f| require f }
6
+
7
+ RSpec.configure do |config|
8
+ # Run specs in random order to surface order dependencies. If you find an
9
+ # order dependency and want to debug it, you can fix the order by providing
10
+ # the seed, which is printed after each run.
11
+ # --seed 1234
12
+ config.order = "random"
13
+ end
@@ -0,0 +1,35 @@
1
+ # Adapted from https://github.com/bronson/vim-runtest/blob/master/rspec_formatter.rb.
2
+ require 'rspec/core/formatters/base_text_formatter'
3
+
4
+ class VimFormatter < RSpec::Core::Formatters::BaseTextFormatter
5
+
6
+ def example_failed example
7
+ exception = example.execution_result[:exception]
8
+ path = Regexp.last_match[1] if exception.backtrace.find do |frame|
9
+ frame =~ %r{\b(spec/.*_spec\.rb:\d+)(?::|\z)}
10
+ end
11
+ message = format_message exception.message
12
+ path = format_caller(path || ' ')
13
+ output.puts "#{path}: #{example.example_group.description.strip} " +
14
+ "#{example.description.strip}: #{message.strip}" if path
15
+ end
16
+
17
+ def example_pending *args; end
18
+
19
+ def dump_failures *args; end
20
+
21
+ def dump_pending *args; end
22
+
23
+ def message msg; end
24
+
25
+ def dump_summary *args; end
26
+
27
+ def seed *args; end
28
+
29
+ private
30
+
31
+ def format_message msg
32
+ msg.gsub("\n", ' ')[0, 80]
33
+ end
34
+
35
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fetching
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Gee
9
+ - Mark Lorenz
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2014-04-24 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1.3'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '1.3'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: pry-plus
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ description: More sass in more structs.
80
+ email:
81
+ - mgee@covermymeds.com
82
+ - mlorenz@covermymeds.com
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - .gitignore
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.md
91
+ - Rakefile
92
+ - fetching.gemspec
93
+ - lib/fetching.rb
94
+ - lib/fetching/version.rb
95
+ - spec/fetching_spec.rb
96
+ - spec/spec_helper.rb
97
+ - spec/support/vim_formatter.rb
98
+ homepage: ''
99
+ licenses:
100
+ - MIT
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 1.8.25
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: ! 'This gem is a work in progress. The implementation code is not what''s
123
+ important. What is important: Don''t de-serialize API responses in to hashes and
124
+ arrays. Use a "strict" object that inforces key presence, and array bounds.}'
125
+ test_files:
126
+ - spec/fetching_spec.rb
127
+ - spec/spec_helper.rb
128
+ - spec/support/vim_formatter.rb
129
+ has_rdoc: