parsable 0.2.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3647c899ec5e6def562f04586513c86e474abf84
4
- data.tar.gz: 8bba282b7f3b95fc81df04c3e743ba41a2c6e454
3
+ metadata.gz: fa26def0d4d25d2a7fbe0cb241a1b87d78d43d81
4
+ data.tar.gz: 247b83cce85574adca35d3d2e6bfa2e7aaf1f10e
5
5
  SHA512:
6
- metadata.gz: b58d2729bfe483afa34a9328cc955d8f10bdb49592146cc6eb0bafd4bfb9eb46b9f665324933496095b320c209406e59a2f91cc9acafa2620f1777b5e730a4f9
7
- data.tar.gz: 182c30d0c82e7a69b6a8ff9e8ec5b54b4b0bf6dc7cbca2c9076872116598f6410e0d15be4d872419ceec9ec53bdf7bdd22545b6ff8ec88b17da3011c473cae76
6
+ metadata.gz: 853901451d3865fc969adadc18618063f3238b2d8101c28af8afa1acd037913ad3ff502d4c3191946a7761bba4fc67e7a309d19d969c0f45753c2a3c6e0f5660
7
+ data.tar.gz: 77ebfb3746e93df6e4ca88a37d886e9a092652ee3b85bbaf760dd09ff940e80aacd094315c713d708e916b7bd8275e3fce28b4677b758b6e23e164b28621ec20
@@ -0,0 +1,39 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://httpbin.org/get?unsecure=notsecure
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Api-Key:
11
+ - flkdsjflksjfejifwf
12
+ response:
13
+ status:
14
+ code: 200
15
+ message: OK
16
+ headers:
17
+ Server:
18
+ - nginx
19
+ Date:
20
+ - Wed, 30 Dec 2015 05:59:13 GMT
21
+ Content-Type:
22
+ - application/json
23
+ Content-Length:
24
+ - '241'
25
+ Connection:
26
+ - keep-alive
27
+ Access-Control-Allow-Origin:
28
+ - '*'
29
+ Access-Control-Allow-Credentials:
30
+ - 'true'
31
+ body:
32
+ encoding: ASCII-8BIT
33
+ string: "{\n \"args\": {\n \"unsecure\": \"notsecure\"\n }, \n \"headers\":
34
+ {\n \"Accept\": \"*/*\", \n \"Api-Key\": \"flkdsjflksjfejifwf\", \n
35
+ \ \"Host\": \"httpbin.org\"\n }, \n \"origin\": \"107.139.199.11\", \n
36
+ \ \"url\": \"http://httpbin.org/get?unsecure=notsecure\"\n}\n"
37
+ http_version:
38
+ recorded_at: Wed, 30 Dec 2015 05:59:13 GMT
39
+ recorded_with: VCR 3.0.0
@@ -17,6 +17,7 @@ module Parsable
17
17
  })
18
18
 
19
19
  @variables.store(:remote, Parsable::Remote.new)
20
+ @variables.store(:sremote, Parsable::Remote.new(:secure => true))
20
21
  end
21
22
 
22
23
  def custom_store attribute, value
@@ -5,7 +5,10 @@ module Parsable
5
5
  class Remote
6
6
  # uses the response from a remote location
7
7
 
8
- def initialize
8
+ attr_accessor :secure
9
+
10
+ def initialize options={}
11
+ self.secure = options.fetch(:secure, false)
9
12
  end
10
13
 
11
14
  def method_missing(method_sym, *arguments, &block)
@@ -16,15 +19,24 @@ module Parsable
16
19
 
17
20
  private
18
21
 
22
+ def perform url, headers={}
23
+ begin
24
+ Curl::Easy.perform(url) do |http|
25
+ headers.each { |header, value| http.headers[header] = value }
26
+ http.connect_timeout = 2
27
+ http.on_success { |easy| @body = easy.body_str }
28
+ end
29
+ rescue Curl::Err::CurlError
30
+ end
31
+ end
32
+
19
33
  def get_response uri
34
+ transformer = Parsable::UriHelper.new(uri)
35
+ url = transformer.to_s
36
+ headers = transformer.secrets
37
+
20
38
  0.upto(2) do |i|
21
- begin
22
- Curl::Easy.perform(uri.to_s) do |http|
23
- http.connect_timeout = 2
24
- http.on_success { |easy| @body = easy.body_str }
25
- end
26
- rescue Curl::Err::CurlError
27
- end
39
+ perform(url, headers)
28
40
 
29
41
  break if @body
30
42
  end
@@ -0,0 +1,43 @@
1
+ module Parsable
2
+ class UriHelper
3
+
4
+ def initialize uri
5
+ @uri = uri
6
+ end
7
+
8
+ def exploded_query
9
+ @query ||= original_query.split("&").each_with_object({}) do |query, hash|
10
+ name, value = query.split("=")
11
+ hash.store(name, value) if name && value
12
+ end
13
+ end
14
+
15
+ def query
16
+ query_hash.each_with_object([]) { |(key, value), array| array.push("#{key}=#{value}") }.join("&")
17
+ end
18
+
19
+ def query_hash
20
+ exploded_query.reject { |key, value| scrub_param?(key) }
21
+ end
22
+
23
+ def secrets
24
+ exploded_query.select { |key, value| scrub_param?(key) }
25
+ end
26
+
27
+ def to_s
28
+ uri = @uri.dup
29
+ uri.query = query
30
+ uri.to_s
31
+ end
32
+
33
+ private
34
+
35
+ def original_query
36
+ @uri.query.to_s
37
+ end
38
+
39
+ def scrub_param? param_name
40
+ !!(param_name =~ /(key|token)/)
41
+ end
42
+ end
43
+ end
@@ -1,3 +1,3 @@
1
1
  module Parsable
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/parsable.rb CHANGED
@@ -3,6 +3,7 @@ require 'parsable/parser'
3
3
  require 'parsable/context'
4
4
  require 'parsable/parsed_item'
5
5
  require 'parsable/remote'
6
+ require 'parsable/uri_helper'
6
7
 
7
8
  module Parsable
8
9
 
data/spec/remote_spec.rb CHANGED
@@ -16,6 +16,18 @@ describe Parsable::Remote do
16
16
  query = {"args" => {"query1" => "q1", "query2" => "q2"}}
17
17
  expect(JSON.parse(subject.send("http://httpbin.org/get?query1=q1&query2=q2"))).to include(query)
18
18
  end
19
+ context "with secure request" do
20
+
21
+ subject { described_class.new(:secure => true) }
22
+
23
+ it "moves sensitive query params into headers" do
24
+ query = {"unsecure" => "notsecure"}
25
+ headers = {"api_key" => "flkdsjflksjfejifwf"}
26
+ body_hash = JSON.parse(subject.send("http://httpbin.org/get?api_key=flkdsjflksjfejifwf&unsecure=notsecure"))
27
+ expect(body_hash["args"]).to eql(query)
28
+ expect(body_hash["headers"]).to include("Api-Key" => "flkdsjflksjfejifwf")
29
+ end
30
+ end
19
31
  end
20
32
 
21
33
  context "cant connect" do
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe Parsable::UriHelper do
4
+
5
+ subject { described_class.new(URI.parse("http://httpbin.org/get?unsecure2=notsecure2&secure_api_key=flkdsjflksjfejifwf&unsecure=notsecure&")) }
6
+
7
+ describe '#exploded_query' do
8
+ it "is a hash representation of all query params" do
9
+ expected = {"unsecure2"=>"notsecure2", "secure_api_key" => "flkdsjflksjfejifwf", "unsecure" => "notsecure" }
10
+ expect(subject.exploded_query).to eql(expected)
11
+ end
12
+
13
+ context "no query" do
14
+ subject { described_class.new(URI.parse("http://httpbin.org/get")) }
15
+ it "is empty" do
16
+ expect(subject.exploded_query).to eql({})
17
+ end
18
+ end
19
+ end
20
+
21
+ describe '#query_hash' do
22
+ it "does not inlcude sensitve key" do
23
+ expect(subject.query_hash.keys).to_not include("secure_api_key")
24
+ end
25
+
26
+ it "includes nonsecure param" do
27
+ expect(subject.query_hash.keys).to include("unsecure")
28
+ end
29
+ end
30
+
31
+ describe '#query' do
32
+ it "is joined by &s" do
33
+ expect(subject.query).to eql("unsecure2=notsecure2&unsecure=notsecure")
34
+ end
35
+ end
36
+
37
+ describe '#secrets' do
38
+ it "is a hash" do
39
+ expect(subject.secrets).to be_kind_of(Hash)
40
+ end
41
+
42
+ it "has secret keys" do
43
+ expect(subject.secrets.keys).to include("secure_api_key")
44
+ end
45
+
46
+ it 'does not have non secrets' do
47
+ expect(subject.secrets.keys).to_not include("unsecure", "unsecure2")
48
+ end
49
+ end
50
+
51
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parsable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hubert Liu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-14 00:00:00.000000000 Z
11
+ date: 2015-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -125,11 +125,13 @@ files:
125
125
  - fixtures/vcr_cassettes/Parsable_Remote/_method_missing/url/cant_connect/returns_nil.yml
126
126
  - fixtures/vcr_cassettes/Parsable_Remote/_method_missing/url/returns_the_body.yml
127
127
  - fixtures/vcr_cassettes/Parsable_Remote/_method_missing/url/with_query_params/uses_them_in_the_request.yml
128
+ - fixtures/vcr_cassettes/Parsable_Remote/_method_missing/url/with_query_params/with_secure_request/moves_sensitive_query_params_into_headers.yml
128
129
  - lib/parsable.rb
129
130
  - lib/parsable/context.rb
130
131
  - lib/parsable/parsed_item.rb
131
132
  - lib/parsable/parser.rb
132
133
  - lib/parsable/remote.rb
134
+ - lib/parsable/uri_helper.rb
133
135
  - lib/parsable/version.rb
134
136
  - parsable.gemspec
135
137
  - spec/context_spec.rb
@@ -137,6 +139,7 @@ files:
137
139
  - spec/parser_spec.rb
138
140
  - spec/remote_spec.rb
139
141
  - spec/spec_helper.rb
142
+ - spec/uri_helper.rb
140
143
  homepage: ''
141
144
  licenses:
142
145
  - MIT
@@ -167,3 +170,4 @@ test_files:
167
170
  - spec/parser_spec.rb
168
171
  - spec/remote_spec.rb
169
172
  - spec/spec_helper.rb
173
+ - spec/uri_helper.rb