activegraphql 0.1.0

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: 657360098f58cecb708293738e5ecf43bc40890e
4
+ data.tar.gz: 51e15e8a81e7100617f665ec94b4cc4b27612546
5
+ SHA512:
6
+ metadata.gz: 9a5233fa7a1e4d9a6f31202d751a24b4fd145f09552958885d4913cce9cb2f20bc2e29c25cbcd200b13c3bb9ffb8ca94d88599ad5c331407b27606063b2cfef9
7
+ data.tar.gz: 496784159351d5efbbde0cc844ab09d6db13d8f3e4ca4d4d6270abe151069d3ade3a2258451291be49a9da69c1e8ffe6d5327199a87bd67a7f07e5e9092fd270
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activegraphql.gemspec
4
+ gemspec
5
+
6
+ gem 'byebug', group: [:development, :test]
7
+ gem 'rspec-its', group: [:test]
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 aviscasillas
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # ActiveGraphQL
2
+
3
+ [![Build Status](https://travis-ci.org/wakoopa/activegraphql.png)](https://travis-ci.org/wakoopa/activegraphql)
4
+
5
+ ActiveGraphQL connects classes to [GraphQL](http://graphql.org/) services.
6
+ The library provides a **Model** class that, when subclassed and configured, encapsulates the communication with the service.
7
+
8
+ ```ruby
9
+ class MyModel < ActiveGraphQL::Model
10
+ configure url: 'http://some-graphql-service/endpoint'
11
+ end
12
+ ```
13
+
14
+ Any subclass of `ActiveGraphQL::Model` will provide the following methods:
15
+
16
+ - **all:** Retrive all objects for the entity.
17
+ - **where(conditions):** Retrieve all objects for the entity finding by conditions.
18
+ - **find_by(conditions):** Retrieve first object for the entity finding by conditions.
19
+
20
+ Any one of these methods returns an `ActiveGraphQL::Fetcher` who provides the method `fetch(*graph)` that is responsible of calling the service. The `*graph` arguments allow to specify how the response format will be.
21
+
22
+ For convention, any method is performing a call to the service with a query, that is resolved based on: model class name, conditions and graph.
23
+
24
+ **Retrieving all `MyModel` objects (just ask for retrieving `id`)**
25
+
26
+ ```ruby
27
+ >> MyModel.all.fetch(:id).first.id
28
+ => "1"
29
+ ```
30
+
31
+ Resolved query:
32
+
33
+ ```
34
+ { myModels { id } }
35
+ ```
36
+
37
+ **Retrieving all `MyModel` objects with `value == "a"` (ask for `id` and `name`)**
38
+
39
+ ```ruby
40
+ >> m = MyModel.where(value: 'a').fetch(:id, :name).first
41
+
42
+ >> m.id
43
+ => "3"
44
+
45
+ >> m.name
46
+ => "Whatever"
47
+ ```
48
+
49
+ Resolved query:
50
+
51
+ ```
52
+ { myModels("value": "a") { id, name } }
53
+ ```
54
+
55
+ **Retrieving `MyModel` object with `id == "5"` (ask for `id`, `name` and `nestedObject { id }`)**
56
+
57
+ ```ruby
58
+ >> m = MyModel.find_by(id: '5').fetch(:id, :name, nested_object: [:description])
59
+
60
+ >> m.id
61
+ => "5"
62
+
63
+ >> m.name
64
+ => "Whatever"
65
+
66
+ >> m.nested_object.description
67
+ => "Some description here"
68
+ ```
69
+
70
+ Resolved query:
71
+
72
+ ```
73
+ { myModel("id": "5") { id, name, nestedObject { description } } }
74
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activegraphql/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'activegraphql'
8
+ spec.version = ActiveGraphQL::VERSION
9
+ spec.authors = ['Wakoopa']
10
+ spec.email = ['info@wakoopa.com']
11
+
12
+ spec.summary = %q{Ruby client for GraphQL services}
13
+ spec.description = %q{}
14
+ spec.homepage = 'https://github.com/wakoopa/active-graphql'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.11'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.0'
25
+ spec.add_dependency 'hashie', '~> 3.4'
26
+ spec.add_dependency 'activesupport', '~> 4.2'
27
+ spec.add_dependency 'httparty', '~> 0.13'
28
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "activegraphql"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ require 'active_support'
2
+ require 'activegraphql/version'
3
+ require 'activegraphql/query'
4
+ require 'activegraphql/fetcher'
5
+ require 'activegraphql/model'
@@ -0,0 +1,28 @@
1
+ require 'activegraphql/support/fancy'
2
+
3
+ module ActiveGraphQL
4
+ class Fetcher < Support::Fancy
5
+ attr_accessor :url, :klass, :action, :params, :query
6
+
7
+ class Error < StandardError; end
8
+
9
+ def initialize(attrs)
10
+ super(attrs)
11
+ self.query = Query.new(url: url, action: action, params: params)
12
+ end
13
+
14
+ def fetch(*graph)
15
+ response = query.get(*graph)
16
+ return if response.blank?
17
+
18
+ case response
19
+ when Hash
20
+ klass.new(response)
21
+ when Array
22
+ response.map { |h| klass.new(h) }
23
+ else
24
+ fail Error, "Unexpected response for query: #{response}"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,59 @@
1
+ require 'active_support/core_ext'
2
+ require 'hashie/mash'
3
+
4
+ module ActiveGraphQL
5
+ class Model < ::Hashie::Mash
6
+ class Error < StandardError; end
7
+
8
+ class << self
9
+ attr_accessor :url
10
+
11
+ # This provides ability to configure the class inherited from here.
12
+ # Also field classes will have the configuration.
13
+ #
14
+ # Example:
15
+ # class BaseModelToMyService < ActiveGraphql::Model
16
+ # configure url: 'http://localhost:3000/graphql'
17
+ # end
18
+ #
19
+ # class ModelToMyService < BaseModelToMyService
20
+ # end
21
+ #
22
+ # BaseModelToMyService.url
23
+ # => "http://localhost:3000/graphql"
24
+ #
25
+ # ModelToMyService.url
26
+ # => "http://localhost:3000/graphql"
27
+ def configure(url: nil)
28
+ configurable_class.url = url
29
+ end
30
+
31
+ # Resolves the class who is extending from ActiveGraphql::Model.
32
+ #
33
+ # This provides the capability for configuring inheritable classes with
34
+ # specific configuration to the corresponding graphql service.
35
+ def configurable_class
36
+ ancestors[ancestors.find_index(ActiveGraphQL::Model) - 1]
37
+ end
38
+
39
+ def all
40
+ build_fetcher(name.demodulize.underscore.pluralize.to_sym)
41
+ end
42
+
43
+ def where(conditions = {})
44
+ build_fetcher(name.demodulize.underscore.pluralize.to_sym, conditions)
45
+ end
46
+
47
+ def find_by(conditions = {})
48
+ build_fetcher(name.demodulize.underscore.to_sym, conditions)
49
+ end
50
+
51
+ def build_fetcher(action, params = nil)
52
+ Fetcher.new(url: configurable_class.url,
53
+ klass: self,
54
+ action: action,
55
+ params: params)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,80 @@
1
+ require 'httparty'
2
+ require 'active_support/inflector'
3
+ require 'activegraphql/support/fancy'
4
+
5
+ module ActiveGraphQL
6
+ class Query < Support::Fancy
7
+ attr_accessor :url, :action, :params, :graph, :response
8
+
9
+ class ServerError < StandardError; end
10
+
11
+ def get(*graph)
12
+ self.graph = graph
13
+
14
+ self.response = HTTParty.get(url, query: { query: to_s })
15
+
16
+ raise(ServerError, response_error_messages) if response_errors.present?
17
+ response_data
18
+ end
19
+
20
+ def response_data
21
+ return unless response['data']
22
+ to_snake_case(response['data'][qaction])
23
+ end
24
+
25
+ def response_errors
26
+ to_snake_case(response['errors'])
27
+ end
28
+
29
+ def response_error_messages
30
+ response_errors.map { |e| e[:message] }
31
+ end
32
+
33
+ def to_s
34
+ str = "{ #{qaction}"
35
+ str << (qparams.present? ? "(#{qparams}) {" : ' {')
36
+ str << " #{qgraph(graph)} } }"
37
+ str
38
+ end
39
+
40
+ def qaction
41
+ action.to_s.camelize(:lower)
42
+ end
43
+
44
+ def qparams
45
+ return if params.blank?
46
+
47
+ param_strings = params.map do |k, v|
48
+ "#{k.to_s.camelize(:lower)}: \"#{v}\""
49
+ end
50
+
51
+ param_strings.join(', ')
52
+ end
53
+
54
+ def qgraph(graph)
55
+ graph_strings = graph.map do |item|
56
+ case item
57
+ when Symbol
58
+ item.to_s.camelize(:lower)
59
+ when Hash
60
+ item.map { |k, v| "#{k.to_s.camelize(:lower)} { #{qgraph(v)} }" }
61
+ end
62
+ end
63
+
64
+ graph_strings.join(', ')
65
+ end
66
+
67
+ private
68
+
69
+ def to_snake_case(value)
70
+ case value
71
+ when Array
72
+ value.map { |v| to_snake_case(v) }
73
+ when Hash
74
+ Hash[value.map { |k, v| [k.to_s.underscore.to_sym, to_snake_case(v)] }]
75
+ else
76
+ value
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,9 @@
1
+ module ActiveGraphQL
2
+ module Support
3
+ class Fancy
4
+ def initialize(attrs = {})
5
+ attrs.each { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveGraphQL
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activegraphql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Wakoopa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: hashie
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.4'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: httparty
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.13'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.13'
97
+ description: ''
98
+ email:
99
+ - info@wakoopa.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - activegraphql.gemspec
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/activegraphql.rb
115
+ - lib/activegraphql/fetcher.rb
116
+ - lib/activegraphql/model.rb
117
+ - lib/activegraphql/query.rb
118
+ - lib/activegraphql/support/fancy.rb
119
+ - lib/activegraphql/version.rb
120
+ homepage: https://github.com/wakoopa/active-graphql
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.5.1
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Ruby client for GraphQL services
144
+ test_files: []