Class: Jeql::GraphqlBlock
- Inherits:
-
Liquid::Block
- Object
- Liquid::Block
- Jeql::GraphqlBlock
- Defined in:
- lib/jeql/graphql_block.rb
Constant Summary collapse
- GraphQlError =
Class.new(Jekyll::Errors::FatalException)
- PARAMS_SYNTAX =
/(\w+):\s*['"](\w+)['"],?/
Instance Method Summary collapse
-
#initialize(tag_name, text, tokens) ⇒ GraphqlBlock
constructor
A new instance of GraphqlBlock.
- #render(context) ⇒ Object
Constructor Details
#initialize(tag_name, text, tokens) ⇒ GraphqlBlock
Returns a new instance of GraphqlBlock.
5 6 7 8 9 |
# File 'lib/jeql/graphql_block.rb', line 5 def initialize(tag_name, text, tokens) super @params = text.scan(PARAMS_SYNTAX) @text = text end |
Instance Method Details
#render(context) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/jeql/graphql_block.rb', line 11 def render(context) hash_params = Hash[@params] endpoint_config = context.registers[:site].config["jeql"][hash_params["endpoint"]] query = Jeql::Query.new(hash_params["query"], context.registers[:site].config["source"], endpoint_config) if query.response.success? context['data'] = JSON.parse(query.response.body)['data'] super else raise GraphQlError, "The query #{query.query_name} failed" end end |