graphlient 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e1adef5ff2cf8488f33f2decb2242192aa29654c
4
+ data.tar.gz: 85c4b69d0f9a9d4cd49cba7aff1cdeeb23d6fcaf
5
+ SHA512:
6
+ metadata.gz: e461cb4260672b0ac960b7673a7133aa8131361a5a590c6bc5bf5bf65a378c7dd2bd3d8ae40983e6e7ca702039418525c02204fd6961fd06bd6bcf833ed499a3
7
+ data.tar.gz: 04b967185ca939bc62c7fc3cfc61d721e59c1491965fa803e6fbd850a2cef79c1ed67491089b1a0bd421f9fdd44e2e7a9743e51b78e1b4704ceb92b630c45428
data/.byebug_history ADDED
@@ -0,0 +1,23 @@
1
+ c
2
+ next
3
+ nex
4
+ Graphlient.config.graphql_endpoint
5
+ c
6
+ Graphlient.config.graphql_endpoint
7
+ query.to_s
8
+ c
9
+ &block
10
+ args
11
+ m
12
+ c
13
+ n
14
+ &block
15
+ args
16
+ m
17
+ c
18
+ send(m, *args, &block)
19
+ send(m, args, &block)
20
+ &block
21
+ block
22
+ args
23
+ m
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.0
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+
3
+ cache: bundler
4
+
5
+ rvm:
6
+ - 2.3.0
7
+ - 2.4.1
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+
7
+ group :development do
8
+ gem 'rubocop', '~> 0.47.1', require: false
9
+ gem 'byebug'
10
+ end
11
+
12
+ group :test do
13
+ gem 'rspec'
14
+ gem 'rspec-mocks'
15
+ gem 'webmock'
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,64 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ graphlient (0.0.3)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ addressable (2.5.2)
10
+ public_suffix (>= 2.0.2, < 4.0)
11
+ ast (2.3.0)
12
+ byebug (9.1.0)
13
+ crack (0.4.3)
14
+ safe_yaml (~> 1.0.0)
15
+ diff-lcs (1.3)
16
+ hashdiff (0.3.6)
17
+ parser (2.4.0.0)
18
+ ast (~> 2.2)
19
+ powerpack (0.1.1)
20
+ public_suffix (3.0.0)
21
+ rainbow (2.2.2)
22
+ rake
23
+ rake (12.1.0)
24
+ rspec (3.6.0)
25
+ rspec-core (~> 3.6.0)
26
+ rspec-expectations (~> 3.6.0)
27
+ rspec-mocks (~> 3.6.0)
28
+ rspec-core (3.6.0)
29
+ rspec-support (~> 3.6.0)
30
+ rspec-expectations (3.6.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.6.0)
33
+ rspec-mocks (3.6.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.6.0)
36
+ rspec-support (3.6.0)
37
+ rubocop (0.47.1)
38
+ parser (>= 2.3.3.1, < 3.0)
39
+ powerpack (~> 0.1)
40
+ rainbow (>= 1.99.1, < 3.0)
41
+ ruby-progressbar (~> 1.7)
42
+ unicode-display_width (~> 1.0, >= 1.0.1)
43
+ ruby-progressbar (1.9.0)
44
+ safe_yaml (1.0.4)
45
+ unicode-display_width (1.3.0)
46
+ webmock (3.0.1)
47
+ addressable (>= 2.3.6)
48
+ crack (>= 0.3.2)
49
+ hashdiff
50
+
51
+ PLATFORMS
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ byebug
56
+ graphlient!
57
+ rake
58
+ rspec
59
+ rspec-mocks
60
+ rubocop (~> 0.47.1)
61
+ webmock
62
+
63
+ BUNDLED WITH
64
+ 1.15.4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Ashkan Nasseri
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # Graphlient ![](https://travis-ci.org/ashkan18/graphlient.svg?branch=master)
2
+ Ruby Client for consuming GraphQL based APIs.
3
+
4
+ ## Installation
5
+ Add following line to your Gemfile
6
+
7
+ ```ruby
8
+ gem 'graphlient'
9
+ ```
10
+
11
+ ## Configuration
12
+
13
+ Add `graphlient.rb under config/initializers. You can use this config to setup `graphql_endpoint`. Here is a sample of configuration:
14
+
15
+ ```ruby
16
+ # config/initializers/graphlient.rb
17
+ Graphlient.configure do |config|
18
+ config.graphql_endpoint = 'http://test-graphql.biz/graphql' # target GraphQL endpoint
19
+ end
20
+ ```
21
+
22
+ ## Usage
23
+ There are 3 different usages.
24
+
25
+ ### Graphlient::Client
26
+ After configuring the client, you can use the client by passing the query in a block.
27
+
28
+ ```ruby
29
+ response = Graphlient::Client.query(headers: { 'Authorization' => 'Bearer 123'}) do
30
+ invoice(id: 10) do
31
+ id
32
+ total
33
+ line_items do
34
+ price
35
+ item_type
36
+ end
37
+ end
38
+ end
39
+ ```
40
+
41
+ This will call the endpoint setup in the configuration with `POST` and passes `query` as
42
+ ```graphql
43
+ invoice(id: 10) {
44
+ id
45
+ total
46
+ line_items{
47
+ price
48
+ item_type
49
+ }
50
+ }
51
+ ```
52
+ It also sets the `Authorization` header based on passed in data.
53
+
54
+ ### Use Graphlient::Query directly
55
+ You can directly use `Graphlient::Query` to generate GraphQL queries. Example:
56
+ ```ruby
57
+ query = Graphlient::Query.new do
58
+ query do
59
+ invoice(id: 10) do
60
+ line_items
61
+ end
62
+ end
63
+ end
64
+
65
+ query.to_s
66
+ # "\nquery{\n invoice(id: 10){\n line_items\n }\n }\n"
67
+ ```
68
+
69
+ ### Use Graphlient::Extension::Query
70
+ You can include `Graphlient::Extension::Query` in your module. This will add new `method_missing` method to your context which will be used to generate GraphQL queries.
71
+
72
+ ```ruby
73
+ include Graphlient::Extension::Query
74
+ query = invoice(id: 10) do
75
+ line_items
76
+ end
77
+
78
+ query.to_s
79
+ # "\nquery{\n invoice(id: 10){\n line_items\n }\n }\n"
80
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'bundler/gem_tasks'
3
+ Bundler.setup(:default, :development)
4
+
5
+ require 'rspec/core'
6
+ require 'rspec/core/rake_task'
7
+
8
+ RSpec::Core::RakeTask.new(:spec) do |spec|
9
+ spec.pattern = FileList['spec/**/*_spec.rb']
10
+ end
11
+
12
+ task default: :spec
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
3
+
4
+ require 'graphlient/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'graphlient'
8
+ s.version = Graphlient::VERSION
9
+ s.authors = ['Ashkan Nasseri']
10
+ s.email = 'ashkan.nasseri@gmail.com'
11
+ s.platform = Gem::Platform::RUBY
12
+ s.required_rubygems_version = '>= 1.3.6'
13
+ s.files = `git ls-files`.split("\n")
14
+ s.require_paths = ['lib']
15
+ s.homepage = 'http://github.com/ashkan18/graphlient'
16
+ s.licenses = ['MIT']
17
+ s.summary = "Ruby Gem for consuming GraphQL endpoints"
18
+ end
@@ -0,0 +1,22 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+
5
+ module Graphlient
6
+ module Client
7
+ def self.query(headers = {}, &block)
8
+ query = Graphlient::Query.new do
9
+ instance_eval(&block)
10
+ end
11
+ uri = URI(Graphlient.config.graphql_endpoint)
12
+ http = Net::HTTP.new(uri.host, uri.port)
13
+ request = Net::HTTP::Post.new(uri.request_uri)
14
+ request.body = { query: query.to_s }.to_json
15
+ headers.each do |k, v|
16
+ request[k] = v
17
+ end
18
+ response = http.request(request)
19
+ JSON.parse(response.body, symbolize_names: true)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Graphlient
4
+ module Config
5
+ extend self
6
+
7
+ attr_accessor :graphql_endpoint
8
+
9
+ def reset
10
+ self.graphql_endpoint = nil
11
+ end
12
+
13
+ reset
14
+ end
15
+
16
+ class << self
17
+ def configure
18
+ yield(Config) if block_given?
19
+ Config
20
+ end
21
+
22
+ def config
23
+ Config
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ module Graphlient
2
+ module Extension
3
+ module Query
4
+ def method_missing(m, *args, &block)
5
+ Graphlient::Query.new do
6
+ send(m, *args, &block)
7
+ end
8
+ end
9
+
10
+ def respond_to_missing?(m, include_private = false)
11
+ super
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,50 @@
1
+ module Graphlient
2
+ class Query
3
+ attr_accessor :query_str
4
+
5
+ def initialize(&block)
6
+ @indents = 0
7
+ @query_str = ''
8
+ instance_eval(&block)
9
+ end
10
+
11
+ def method_missing(m, *args, &block)
12
+ append(m, args, &block)
13
+ end
14
+
15
+ def respond_to_missing?(m, include_private = false)
16
+ super
17
+ end
18
+
19
+ def to_s
20
+ "{ #{query_str} }"
21
+ end
22
+
23
+ private
24
+
25
+ def append(query_field, args, &block)
26
+ # add field
27
+ @query_str << "\n#{indent}#{query_field}"
28
+ # add filter
29
+ @query_str << "(#{get_args_str(args)})" if args.any?
30
+
31
+ if block_given?
32
+ @indents += 1
33
+ @query_str << '{'
34
+ instance_eval(&block)
35
+ @query_str << '}'
36
+ @indents -= 1
37
+ end
38
+
39
+ @query_str << "\n#{indent}"
40
+ end
41
+
42
+ def indent
43
+ ' ' * @indents
44
+ end
45
+
46
+ def get_args_str(args)
47
+ args.detect { |arg| arg.is_a? Hash }.map { |k, v| "#{k}: #{v}" }.join(',')
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Graphlient
3
+ VERSION = '0.0.3'
4
+ end
data/lib/graphlient.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'graphlient/version'
2
+ require 'graphlient/config.rb'
3
+ require 'graphlient/extension'
4
+ require 'graphlient/query'
5
+ require 'graphlient/client'
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+ require 'spec_helper'
3
+
4
+ describe Graphlient::Client do
5
+ describe '#query' do
6
+ let(:graphql_endpoint) { 'http://graph.biz/gprahql' }
7
+ let(:graphql_post_request) { stub_request(:post, 'http://graph.biz/gprahql').to_return(body: {}.to_json) }
8
+ before do
9
+ Graphlient.configure do |config|
10
+ config.graphql_endpoint = graphql_endpoint
11
+ end
12
+ end
13
+ it 'returns expected query with block' do
14
+ graphql_post_request
15
+ Graphlient::Client.query('Authorization' => 'Bearer 1231', 'Content-Type' => 'application/json') do
16
+ invoice(id: 10) do
17
+ line_items
18
+ end
19
+ end
20
+ expect(graphql_post_request.with(
21
+ body: { query: "{ \ninvoice(id: 10){\n line_items\n }\n }" },
22
+ headers: { 'Content-Type' => 'application/json'})
23
+ ).to have_been_made.once
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ require 'spec_helper'
3
+
4
+ describe Graphlient::Extension::Query do
5
+ describe 'Query' do
6
+ include Graphlient::Extension::Query
7
+
8
+ it 'returns proper query' do
9
+ query = invoice(id: 10) do
10
+ line_items
11
+ end
12
+ expect( query.to_s ).to eq("{ \ninvoice(id: 10){\n line_items\n }\n }")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+ require 'spec_helper'
3
+
4
+ describe Graphlient::Query do
5
+ describe '#initialize' do
6
+ it 'returns expected query with block' do
7
+ query = Graphlient::Query.new do
8
+ invoice do
9
+ line_items
10
+ end
11
+ end
12
+ expect(query.to_s).to eq "{ \ninvoice{\n line_items\n }\n }"
13
+ end
14
+
15
+ it 'returns expected query with block and attributes' do
16
+ query = Graphlient::Query.new do
17
+ invoice(id: 10) do
18
+ line_items
19
+ end
20
+ end
21
+ expect(query.to_s).to eq "{ \ninvoice(id: 10){\n line_items\n }\n }"
22
+ end
23
+
24
+ it 'returns expected query with block and attributes' do
25
+ query = Graphlient::Query.new do
26
+ invoice(id: 10) do
27
+ line_items(name: 'test')
28
+ end
29
+ end
30
+ expect(query.to_s).to eq "{ \ninvoice(id: 10){\n line_items(name: test)\n }\n }"
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'rubygems'
5
+ require 'rspec'
6
+ require 'graphlient'
7
+ require 'byebug'
8
+ require 'webmock/rspec'
9
+
10
+ RSpec.configure do |c|
11
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphlient
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Ashkan Nasseri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: ashkan.nasseri@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".byebug_history"
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".ruby-version"
23
+ - ".travis.yml"
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE
27
+ - README.md
28
+ - Rakefile
29
+ - graphlient.gemspec
30
+ - lib/graphlient.rb
31
+ - lib/graphlient/client.rb
32
+ - lib/graphlient/config.rb
33
+ - lib/graphlient/extension.rb
34
+ - lib/graphlient/query.rb
35
+ - lib/graphlient/version.rb
36
+ - spec/graphlient/client_spec.rb
37
+ - spec/graphlient/extension_spec.rb
38
+ - spec/graphlient/query_spec.rb
39
+ - spec/spec_helper.rb
40
+ homepage: http://github.com/ashkan18/graphlient
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 1.3.6
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.6.13
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Ruby Gem for consuming GraphQL endpoints
64
+ test_files: []