quasar_rest_client 0.1.0 → 0.2.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: 4573f4ab0e743f401cfef70607fab0c1b1469eb5
4
- data.tar.gz: 6f3304c930a010c41e4b186fd776a4841ae8f67f
3
+ metadata.gz: da105ccd0c72f3dc04e764b3426613e3c7e6dc6d
4
+ data.tar.gz: 469a399d6fc2f048170d497faefc84727196f5fe
5
5
  SHA512:
6
- metadata.gz: 495f054a0495e55766ef1d9a64be452f65249ac0c76a2be1d23ce9de4c0e124434277448ba28482b17db7ffdec88c9f78ab850730afdf72d158ebee32e60d641
7
- data.tar.gz: 006013b8135e66a0e2ce556244b38f607be403959cb34baa0ffaecdb57b79a98e9902109a4d76ce967e531f290cefb413d8a6f6c6ceb8a57fb2159cb97900e78
6
+ metadata.gz: 711a821b75a5d903da87b3642acb867121d86e02f156a6f0443b31dc477a712fb7667b7be638f67a8079366c44a6f42e66fb1bf0c2f67337296f3d976c515e6b
7
+ data.tar.gz: b98c29831f3ec5cd58127a6b507de54d1144e43549245d537f0e5927dcbad446eb9027dab59d6c7eabc649207874406f56df11c104eea9202d8b1b57d32962fe
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /live_spec/config/config.yml
data/README.md CHANGED
@@ -105,6 +105,20 @@ install`. To release a new version, update the version number in
105
105
  create a git tag for the version, push git commits and tags, and push
106
106
  the `.gem` file to [rubygems.org](https://rubygems.org).
107
107
 
108
+ ### Live Testing
109
+
110
+ To live test the client, you will need a working quasar server, and a
111
+ working mongodb server with data in it.
112
+
113
+ The `./live_spec` directory contains the spec tests for live testing,
114
+ and depends up on the `./live_spec/config/config.yml` file to be set
115
+ with the appropriate information to make the connection work. You can
116
+ copy the `./live_spec/config/config.sample.yml` file and fill in the
117
+ details.
118
+
119
+ A rake task `live_spec` is also available to make running these tests
120
+ simpler.
121
+
108
122
  ## Contributing
109
123
 
110
124
  Bug reports and pull requests are welcome on GitHub at
data/Rakefile CHANGED
@@ -4,3 +4,8 @@ require "rspec/core/rake_task"
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
6
  task :default => :spec
7
+
8
+ RSpec::Core::RakeTask.new(:live_spec) do |c|
9
+ c.pattern = 'live_spec/**{,/*/**}/*_spec.rb'
10
+ c.rspec_opts = '-I live_spec'
11
+ end
@@ -71,4 +71,29 @@ module QuasarRestClient
71
71
  proxy.simple_query
72
72
  end
73
73
 
74
+ # @param [String] q - query string
75
+ # @param [String] dest - destination collection to create
76
+ # @param [Hash] opts - options
77
+ # @return [String] JSON reponse
78
+ def self.long_query(q='', dest='', opts={})
79
+ proxy = Proxy.new(q, opts)
80
+ proxy.long_query(destination: dest)
81
+ end
82
+
83
+ # @param [String] coll - collection to retrieve
84
+ # @param [Hash] opts - options
85
+ # @return [String] JSON reponse
86
+ def self.get_data(coll='', opts={})
87
+ proxy = Proxy.new(nil, opts)
88
+ proxy.get_data(collection: coll)
89
+ end
90
+
91
+ # @param [String] coll - collection to delete
92
+ # @param [Hash] opts - options
93
+ # @return [String] JSON reponse
94
+ def self.delete_data(coll='', opts={})
95
+ proxy = Proxy.new(nil, opts)
96
+ proxy.delete_data(collection: coll)
97
+ end
98
+
74
99
  end
@@ -0,0 +1,41 @@
1
+ require 'time'
2
+ require 'date'
3
+
4
+ # Include this module
5
+ module QuasarRestClient
6
+ module MogrifyVars
7
+
8
+ def mogrify_vars(hash)
9
+ vars = hash[:var]&.map do |k, v|
10
+ new_key = "var.#{k}"
11
+
12
+ # This is hacky-hack, at best
13
+ # E.g. given var: { email: "yacht@mail.com"} =>
14
+ # var.email: '"yacht@mail.com"'
15
+ # i.e. quasar requires the double quotes
16
+ # Note that the documentation says single quotes, but it is WRONG
17
+ new_val = case v
18
+ when String
19
+ v.inspect
20
+ # NOTE: order is important: DateTime must precede Date
21
+ when DateTime
22
+ "TIMESTAMP '#{v.strftime("%F %T")}'"
23
+ when Date
24
+ "DATE '#{v.strftime("%F")}'"
25
+ when Time
26
+ "TIME '#{v.strftime("%T")}'"
27
+ when Array
28
+ v.inspect
29
+ else
30
+ v
31
+ end
32
+
33
+ [new_key, new_val]
34
+ end.to_h
35
+
36
+ hash.delete(:var)
37
+ hash.merge(vars)
38
+ end
39
+
40
+ end
41
+ end
@@ -1,8 +1,10 @@
1
1
  require "httparty"
2
+ require "quasar_rest_client/mogrify_vars"
2
3
 
3
4
  module QuasarRestClient
4
5
  class Proxy
5
6
  include HTTParty
7
+ include MogrifyVars
6
8
 
7
9
  # @!attribute query_hash
8
10
  # @return [Hash] values to form the query string for the request
@@ -36,10 +38,10 @@ module QuasarRestClient
36
38
  self.query_hash.merge!(opts)
37
39
  end
38
40
 
39
- # @return [String] JSON response from query
41
+ # @return [String] JSON response from get query
40
42
  def simple_query
41
43
  self.class.get(
42
- path,
44
+ '/query/fs',
43
45
  query: request_query,
44
46
  headers: request_headers,
45
47
  logger: Base.config.logger,
@@ -47,17 +49,65 @@ module QuasarRestClient
47
49
  )
48
50
  end
49
51
 
52
+ # @param [String] destination collection name to create for later extraction
53
+ # @return [String] JSON response from post query
54
+ def long_query(destination:)
55
+ fail "must provide a destination: #{destination.inspect} #{destination.class}" unless (String === destination && destination.length > 0)
56
+ body = query_hash.delete(:q)
57
+ self.class.post(
58
+ '/query/fs',
59
+ query: request_query,
60
+ body: body,
61
+ headers: request_headers.merge({"destination" => destination}),
62
+ logger: Base.config.logger,
63
+ base_uri: Base.config.endpoint
64
+ )
65
+ end
66
+
67
+ # @param [String] collection name of the collection to retrieve
68
+ # @return [String] JSON response
69
+ def get_data(collection:)
70
+ fail "must provide a collection: #{collection.inspect} [#{collection.class}]" unless (String === collection && collection.length > 0)
71
+
72
+ unless collection[0] == ?/
73
+ collection = ?/ + collection
74
+ end
75
+
76
+ query_hash.delete(:q)
77
+ query_hash.delete(:var)
78
+
79
+ self.class.get(
80
+ '/data/fs' + collection,
81
+ query: request_query,
82
+ headers: request_headers,
83
+ logger: Base.config.logger,
84
+ base_uri: Base.config.endpoint
85
+ )
86
+ end
87
+
88
+ # @param [String] collection name of the collection to delete
89
+ # @return [String] JSON response
90
+ def delete_data(collection:)
91
+ fail "must provide a collection: #{collection.inspect} [#{collection.class}]" unless (String === collection && collection.length > 0)
92
+
93
+ unless collection[0] == ?/
94
+ collection = ?/ + collection
95
+ end
96
+
97
+ self.class.delete(
98
+ '/data/fs' + collection,
99
+ headers: request_headers,
100
+ logger: Base.config.logger,
101
+ base_uri: Base.config.endpoint
102
+ )
103
+ end
104
+
50
105
  private
51
106
 
52
107
  def endpoint
53
108
  Base.config.endpoint
54
109
  end
55
110
 
56
- # TODO: return proper path based on query type for future expansion
57
- def path
58
- '/query/fs'
59
- end
60
-
61
111
  def request_headers
62
112
  {
63
113
  "accept-encoding" => '',
@@ -66,7 +116,7 @@ module QuasarRestClient
66
116
  end
67
117
 
68
118
  def request_query
69
- stringify_keys(query_hash)
119
+ stringify_keys(mogrify_vars(query_hash))
70
120
  end
71
121
 
72
122
  def stringify_keys(hash)
@@ -1,3 +1,3 @@
1
1
  module QuasarRestClient
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,11 @@
1
+ ## Fill in with your live testing data
2
+
3
+ endpoint_host: '-'
4
+ endpoint_port: 0
5
+ mount: '-'
6
+ parent: '-'
7
+ collection: '-'
8
+ new_collection: '-'
9
+ where_clause: '-'
10
+ vars:
11
+ brand_key: '-'
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "live_spec_helper"
4
+ require "logger"
5
+ require 'ostruct'
6
+ require "erb"
7
+ require "yaml"
8
+
9
+ def read_config(file = File.join(File.dirname(__FILE__), 'config', 'config.yml'))
10
+ conf = YAML.load(ERB.new(File.read(file)).result)
11
+ OpenStruct.new(conf)
12
+ end
13
+
14
+ def show_response(resp)
15
+ "response code: #{resp.code} \n" +
16
+ "response request: #{resp.request.uri}"
17
+ end
18
+
19
+ RSpec.describe "Live Testing" do
20
+ let(:config) { read_config }
21
+ let!(:collection_path) { ["", config.mount, config.parent, config.collection].join('/') }
22
+ let!(:new_collection_path) { ["", config.mount, config.parent, config.new_collection].join('/') }
23
+ let!(:endpoint) { "http://#{config.endpoint_host}:#{config.endpoint_port}" }
24
+ let!(:sql_format) { "SELECT %s FROM `#{collection_path}` WHERE #{config.where_clause}" }
25
+ let!(:sql_count) { sql_format % "count(*)" }
26
+ let!(:sql_fetch) { sql_format % "*" }
27
+ let!(:logger) { nil }
28
+
29
+ before do
30
+ QuasarRestClient.config.endpoint = endpoint
31
+ QuasarRestClient.config.logger = logger
32
+ end
33
+
34
+ it "count_query" do
35
+ resp = QuasarRestClient.simple_query(sql_count, var: config.vars)
36
+ expect(resp.code).to(eq(200), show_response(resp))
37
+ end
38
+
39
+ it "fetch_query" do
40
+ resp = QuasarRestClient.simple_query(sql_fetch, limit: 2, var: config.vars)
41
+ expect(resp.code).to(eq(200), show_response(resp))
42
+ end
43
+
44
+ it "long_query" do
45
+ resp = QuasarRestClient.long_query(sql_fetch, new_collection_path, var: config.vars)
46
+ expect(resp.code).to(eq(200), show_response(resp))
47
+ end
48
+
49
+ it "get_data" do
50
+ QuasarRestClient.long_query(sql_fetch, new_collection_path, var: config.vars)
51
+ resp = QuasarRestClient.get_data(new_collection_path, limit: 2)
52
+ expect(resp.code).to(eq(200), show_response(resp))
53
+ end
54
+
55
+ it "delete_data" do
56
+ QuasarRestClient.long_query(sql_fetch, new_collection_path, var: config.vars)
57
+ resp = QuasarRestClient.delete_data(new_collection_path, limit: 2)
58
+ expect(resp.code).to(eq(204), show_response(resp))
59
+ end
60
+
61
+ end
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'quasar_rest_client'
3
+
4
+ RSpec.configure do |config|
5
+ config.before(:example) do
6
+ QuasarRestClient::Base.class_variable_set(:@@config, nil)
7
+ end
8
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quasar_rest_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tamara Temple
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-10 00:00:00.000000000 Z
11
+ date: 2016-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -134,8 +134,12 @@ files:
134
134
  - lib/quasar_rest_client.rb
135
135
  - lib/quasar_rest_client/base.rb
136
136
  - lib/quasar_rest_client/config.rb
137
+ - lib/quasar_rest_client/mogrify_vars.rb
137
138
  - lib/quasar_rest_client/proxy.rb
138
139
  - lib/quasar_rest_client/version.rb
140
+ - live_spec/config/config.sample.yml
141
+ - live_spec/live_spec.rb
142
+ - live_spec/live_spec_helper.rb
139
143
  - quasar_rest_client.gemspec
140
144
  homepage: https://github.com/riverock/quasar_rest_client
141
145
  licenses: []