okay 4.0.0 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -0
- data/lib/okay/graphql.rb +124 -0
- data/lib/okay/http.rb +6 -6
- data/lib/okay/version.rb +1 -1
- data/okay.gemspec +1 -0
- metadata +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5954167c96379ab1ee2a94068253b417f17bdea70a4d9b700b8e94e8e59c5495
|
4
|
+
data.tar.gz: fad91f6ef89734072991b0985763b98a0c6195d7b0176d76ee3d401dbca645ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96337777a6f774ff282af8f883b11411977b14ee43e277d1fb239dfe85a2ce805451ffcfdcc6f65608d7e3ac552bf82bdbce6138ed0bd88278901513f6ddb805
|
7
|
+
data.tar.gz: a4cb525e1ab90e43d56a40e52641ef5143cc0a8e14554f47398b8b9d1bc39c80d4218683b38ef699cb13daed58835be83b64d7bebc68ab3213c56fe89bb63c2f
|
data/README.md
CHANGED
@@ -65,6 +65,26 @@ Okay::HTTP.post("https://httpbin.org/post", form_data: { "foo" => "bar" })
|
|
65
65
|
Okay::HTTP.post("https://httpbin.org/post", data: "hello, world!")
|
66
66
|
```
|
67
67
|
|
68
|
+
### GraphQL
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
require "okay/graphql"
|
72
|
+
require "json"
|
73
|
+
|
74
|
+
query = GraphQL.query {
|
75
|
+
viewer {
|
76
|
+
login
|
77
|
+
}
|
78
|
+
}
|
79
|
+
|
80
|
+
response = request.submit!(:github, {bearer_token: ENV["DEMO_GITHUB_TOKEN"]})
|
81
|
+
JSON.parse(response.body)
|
82
|
+
# =>
|
83
|
+
{"data" =>
|
84
|
+
{"viewer" =>
|
85
|
+
{"login" => "duckinator"}}}
|
86
|
+
```
|
87
|
+
|
68
88
|
## Development
|
69
89
|
|
70
90
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/okay/graphql.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
require "okay/version"
|
2
|
+
require "okay/http"
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
module Okay
|
6
|
+
# Example usage:
|
7
|
+
#
|
8
|
+
# require "okay/graphql"
|
9
|
+
# require "pp"
|
10
|
+
# query = GraphQL.query {
|
11
|
+
# viewer {
|
12
|
+
# login
|
13
|
+
# }
|
14
|
+
# }
|
15
|
+
# response = request.submit!(:github, {bearer_token: ENV["DEMO_GITHUB_TOKEN"]})
|
16
|
+
# pp JSON.parse(response.body)
|
17
|
+
class GraphQL
|
18
|
+
Container = Struct.new(:value) do
|
19
|
+
def inspect
|
20
|
+
value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class QueryDSL
|
25
|
+
def initialize(indent = 0, &query)
|
26
|
+
@query = ""
|
27
|
+
@indent = indent
|
28
|
+
@indent_str = " " * indent
|
29
|
+
instance_exec(&query)
|
30
|
+
end
|
31
|
+
|
32
|
+
def method_missing(name, *args, **kwargs, &block)
|
33
|
+
query_part = @indent_str + name.to_s
|
34
|
+
if args.length > 0 || kwargs.length > 0
|
35
|
+
query_part += "("
|
36
|
+
|
37
|
+
query_args = []
|
38
|
+
query_args += args unless args.empty?
|
39
|
+
query_args += kwargs.map { |k,v|
|
40
|
+
[k,v.inspect].join(": ")
|
41
|
+
}
|
42
|
+
query_part += query_args.join(", ")
|
43
|
+
|
44
|
+
query_part += ")"
|
45
|
+
end
|
46
|
+
|
47
|
+
if block
|
48
|
+
query_part += " {\n"
|
49
|
+
query_part += QueryDSL.new(@indent + 2, &block).to_s
|
50
|
+
query_part += @indent_str + "}"
|
51
|
+
end
|
52
|
+
|
53
|
+
@query += "#{query_part}\n"
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_s
|
57
|
+
@query
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class Query
|
62
|
+
def initialize(&query)
|
63
|
+
@query = QueryDSL.new(&query)
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_s
|
67
|
+
"query {\n" +
|
68
|
+
@query.to_s.gsub(/^/, " ") +
|
69
|
+
"}"
|
70
|
+
end
|
71
|
+
|
72
|
+
def submit!(url, headers = {})
|
73
|
+
if url == :github
|
74
|
+
url = "https://api.github.com/graphql"
|
75
|
+
|
76
|
+
bearer_token = headers[:bearer_token]
|
77
|
+
|
78
|
+
if bearer_token.nil?
|
79
|
+
raise ArgumentError, "missing keyword: bearer_token"
|
80
|
+
end
|
81
|
+
|
82
|
+
headers = default_github_headers(bearer_token).merge(headers)
|
83
|
+
end
|
84
|
+
|
85
|
+
data = {
|
86
|
+
"query" => to_s
|
87
|
+
}.to_json
|
88
|
+
Okay::HTTP.post(url, headers: headers, data: data)
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
92
|
+
def default_github_headers(token)
|
93
|
+
{
|
94
|
+
"Accept" => "application/vnd.github.v4.idl",
|
95
|
+
"Authorization" => "bearer #{token}",
|
96
|
+
}
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def self.query(&query_)
|
101
|
+
Query.new(&query_)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
class Object
|
107
|
+
def self.const_missing(name)
|
108
|
+
# HACK: if const_missing is called inside Okay::GraphQL#initialize,
|
109
|
+
# we wrap the constant name in a GraphQL::Container.
|
110
|
+
# Otherwise, we re-raise the exception.
|
111
|
+
if caller[2] =~ /^#{Regexp.escape(__FILE__)}:\d+:in `initialize'$/
|
112
|
+
Okay::GraphQL::Container.new(name.to_s)
|
113
|
+
else
|
114
|
+
# Create an exception equivalent to what's normally raised.
|
115
|
+
exception = NameError.new("uninitialized constant #{name.to_s}")
|
116
|
+
|
117
|
+
# Preserve the backtrace.
|
118
|
+
exception.set_backtrace(caller)
|
119
|
+
|
120
|
+
# Raise the exception.
|
121
|
+
raise exception
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
data/lib/okay/http.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require "okay/version"
|
2
|
+
require "openssl/better_defaults"
|
3
|
+
require "net/https"
|
4
|
+
require "cacert"
|
5
5
|
|
6
6
|
Cacert.set_in_env
|
7
7
|
|
@@ -60,7 +60,7 @@ module Okay
|
|
60
60
|
|
61
61
|
options = {
|
62
62
|
# If the URI starts with "https://", enable SSL/TLS.
|
63
|
-
use_ssl: (uri.scheme ==
|
63
|
+
use_ssl: (uri.scheme == "https")
|
64
64
|
}
|
65
65
|
|
66
66
|
# Net::HTTP.start() keeps a connection to the host alive
|
@@ -88,7 +88,7 @@ module Okay
|
|
88
88
|
# Follow a redirect.
|
89
89
|
# Decrements +redirect_limit+ while doing so, to avoid redirect loops.
|
90
90
|
# NOTE: Does not handle HTTP 307. https://httpstatuses.com/307
|
91
|
-
send_request(:Get, response[
|
91
|
+
send_request(:Get, response["location"], parameters, body, headers, redirect_limit - 1)
|
92
92
|
else
|
93
93
|
response
|
94
94
|
end
|
data/lib/okay/version.rb
CHANGED
data/okay.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: okay
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ellen Marie Dash
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-12-
|
11
|
+
date: 2017-12-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: openssl-better_defaults
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
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'
|
83
97
|
description: Okay, minimalist implementations of common utilities in Ruby. E.g., HTTP
|
84
98
|
fetchers.
|
85
99
|
email:
|
@@ -99,6 +113,7 @@ files:
|
|
99
113
|
- bin/console
|
100
114
|
- bin/setup
|
101
115
|
- lib/okay.rb
|
116
|
+
- lib/okay/graphql.rb
|
102
117
|
- lib/okay/http.rb
|
103
118
|
- lib/okay/version.rb
|
104
119
|
- okay.gemspec
|
@@ -122,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
137
|
version: '0'
|
123
138
|
requirements: []
|
124
139
|
rubyforge_project:
|
125
|
-
rubygems_version: 2.7.
|
140
|
+
rubygems_version: 2.7.3
|
126
141
|
signing_key:
|
127
142
|
specification_version: 4
|
128
143
|
summary: Okay, minimalist implementations of common utilities.
|