ruby-apicalypse 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 302530da949aa573fdd0ff2de172205b9cef45554795eeeba39e763a5472692b
4
+ data.tar.gz: 7a3225d1776a3e4074f434718c1664473477ab7cac1b7c648b887807e08c6ad5
5
+ SHA512:
6
+ metadata.gz: d35a4165dd080b17059dea7a29b5fb887a11459282a7e8eeb9df186380360bb6cea1acee48a44df38237a6e5492ac1626beaef75f7dffc136ae7409f056b17f4
7
+ data.tar.gz: e910b48a66e5d85a02db77ec844cdf4c9b2a2fd4f246b04eb37ee577bcb101cd2e9930fce31098ed74fc1aefb921dbc5c1634a460ed749051e7516a75e0ff6d1
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby-apicalypse (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.5.2)
10
+ public_suffix (>= 2.0.2, < 4.0)
11
+ crack (0.4.3)
12
+ safe_yaml (~> 1.0.0)
13
+ diff-lcs (1.3)
14
+ hashdiff (0.3.8)
15
+ public_suffix (3.0.3)
16
+ rake (10.5.0)
17
+ rspec (3.8.0)
18
+ rspec-core (~> 3.8.0)
19
+ rspec-expectations (~> 3.8.0)
20
+ rspec-mocks (~> 3.8.0)
21
+ rspec-core (3.8.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-expectations (3.8.2)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.8.0)
26
+ rspec-mocks (3.8.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.8.0)
29
+ rspec-support (3.8.0)
30
+ safe_yaml (1.0.4)
31
+ webmock (3.5.1)
32
+ addressable (>= 2.3.6)
33
+ crack (>= 0.3.2)
34
+ hashdiff
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ bundler (~> 1.16)
41
+ rake (~> 10.0)
42
+ rspec (~> 3.0)
43
+ ruby-apicalypse!
44
+ webmock (~> 3.0)
45
+
46
+ BUNDLED WITH
47
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019
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,123 @@
1
+ # Ruby wrapper for Apicalypse
2
+
3
+ [What is Apicalypse?](https://apicalypse.io/)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ruby-apicalypse'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ruby-apicalypse
20
+
21
+ ## Usage
22
+
23
+ ### Raw Apicalypse
24
+
25
+ ```ruby
26
+ rawQueryString = "fields a,b,c;limit 50;offset 0;";
27
+
28
+ Apicalypse.new("https://myapi.com/foobar")
29
+ .query(rawQueryString)
30
+ .request
31
+ ```
32
+
33
+ ### Apicalypse Query Builder
34
+
35
+ ```ruby
36
+ api = Apicalypse.new("https://myapi.com/foobar")
37
+
38
+ api
39
+ .fields(:name, :movies, :age) # fetches only the name, movies and age fields
40
+ .limit(50) # limit to 50 results
41
+ .offset(10) # offset results by 10
42
+ .sort(:name) # default sort direction is 'asc' (ascending)
43
+ .sort(:name, :desc) # sorts by name, descending
44
+ .search("Arnold") # search for a specific name (search implementations can vary)
45
+ .where("age > 50 & movies != n") # filter the results
46
+
47
+ api.request
48
+ ```
49
+
50
+ Learn more about the Apicalypse syntax [here](https://apicalypse.io/syntax/).
51
+
52
+ ### Optional configurations
53
+
54
+ By default, the apicalypse query method is put in the request body. If your server doesn't support GET bodies, you can put the request in the URL as instead.
55
+
56
+ ```ruby
57
+ # Raw
58
+ Apicalypse.new("https://any-apicalypse-api.com/foobar", {
59
+ queryMethod: :url
60
+ })
61
+ .query("limit=50&fields=name")
62
+ .request
63
+
64
+ # Scoped
65
+ Apicalypse.new("https://any-apicalypse-api.com/foobar", {
66
+ queryMethod: :url
67
+ })
68
+ .limit(50)
69
+ .fields(:name)
70
+ .request
71
+ ```
72
+
73
+ Both examples will request `https://any-apicalypse-api.com/foobar?limit=50&fields=name`.
74
+
75
+ You can also pass custom request headers.
76
+
77
+ ```ruby
78
+ Apicalypse.new("https://any-apicalypse-api.com/foobar", {
79
+ headers: {
80
+ 'user-key' => 'your-api-key',
81
+ 'Accept' => 'application/json'
82
+ }
83
+ })
84
+ .search('Json Born')
85
+ .request
86
+ ```
87
+
88
+ ### Real world example
89
+
90
+ ```ruby
91
+ api_endpoint = 'https://api-v3.igdb.com/games'
92
+ request_headers = { headers: { 'user-key' => 'your-igdb-api-key' } }
93
+
94
+ api = Apicalypse.new(api_endpoint, request_headers)
95
+
96
+ api
97
+ .fields(:name, :total_rating)
98
+ .where(category: 1)
99
+ .search('Call of Duty')
100
+ .limit(2)
101
+ .request
102
+
103
+ # api response
104
+
105
+ [
106
+ {"id"=>107299, "name"=>"Call of Duty: WWII - Shadow War", "total_rating"=>75.0},
107
+ {"id"=>57700, "name"=>"Call of Duty: Infinite Warfare - Retribution", "total_rating"=>60.0}
108
+ ]
109
+ ```
110
+
111
+ ## Development
112
+
113
+ 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.
114
+
115
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
116
+
117
+ ## Contributing
118
+
119
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ad2games/ruby-apicalypse. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
120
+
121
+ ## License
122
+
123
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'apicalypse'
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(__FILE__)
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
data/lib/apicalypse.rb ADDED
@@ -0,0 +1,49 @@
1
+ require 'apicalypse/exceptions'
2
+ require 'apicalypse/request'
3
+ require 'apicalypse/scope'
4
+ require 'apicalypse/version'
5
+
6
+ class Apicalypse
7
+ def initialize(uri, options = {})
8
+ @scope = Scope.new(self)
9
+ @request = Request.new(uri, options)
10
+ end
11
+
12
+ attr_reader :scope
13
+
14
+ def fields(*args)
15
+ @scope.fields(args)
16
+ end
17
+
18
+ def exclude(args)
19
+ @scope.exclude(args)
20
+ end
21
+
22
+ def where(args)
23
+ @scope.where(args)
24
+ end
25
+
26
+ def limit(args)
27
+ @scope.limit(args)
28
+ end
29
+
30
+ def offset(args)
31
+ @scope.offset(args)
32
+ end
33
+
34
+ def sort(args)
35
+ @scope.sort(args)
36
+ end
37
+
38
+ def search(args)
39
+ @scope.search(args)
40
+ end
41
+
42
+ def query(args)
43
+ @scope.query(args)
44
+ end
45
+
46
+ def request
47
+ @request.perform(@scope)
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ class Apicalypse
2
+ class ScopeError < StandardError; end
3
+ end
@@ -0,0 +1,75 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ class Apicalypse
5
+ class Request
6
+ def initialize(uri, options)
7
+ @uri = uri
8
+ @options = options
9
+ end
10
+
11
+ def perform(scope)
12
+ uri = build_uri(scope)
13
+ body = build_body(scope)
14
+
15
+ response = http_get(uri, body)
16
+ raise_on_http_error(response)
17
+
18
+ JSON.parse(response.body)
19
+ end
20
+
21
+ private
22
+
23
+ def build_uri(scope)
24
+ uri = URI(@uri)
25
+
26
+ return uri unless query_method_url?
27
+
28
+ uri.query = if scope.chain[:query]
29
+ scope.chain[:query]
30
+ else
31
+ scope.chain.to_query
32
+ end
33
+
34
+ uri
35
+ end
36
+
37
+ def build_body(scope)
38
+ return scope.chain[:query] if scope.chain[:query]
39
+
40
+ scope.chain.map do |k, v|
41
+ if v.is_a?(Hash)
42
+ "#{k} #{v.keys[0]}=#{v.values[0]};"
43
+ else
44
+ "#{k} #{v};"
45
+ end
46
+ end.join('')
47
+ end
48
+
49
+ def http_get(uri, body)
50
+ Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
51
+ req = Net::HTTP::Get.new(uri, request_headers)
52
+ req.body = body unless query_method_url?
53
+
54
+ http.request req
55
+ end
56
+ end
57
+
58
+ def request_headers
59
+ headers = { 'Accept' => 'application/json' }
60
+ headers.merge!(@options[:headers]) if @options[:headers]
61
+
62
+ headers
63
+ end
64
+
65
+ def raise_on_http_error(response)
66
+ return if response.is_a? Net::HTTPSuccess
67
+
68
+ raise "Request failed with status #{response.code}: #{response.body}"
69
+ end
70
+
71
+ def query_method_url?
72
+ @options[:query_method] == :url
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,66 @@
1
+ class Apicalypse
2
+ class Scope
3
+ def initialize(klass)
4
+ @klass = klass
5
+ end
6
+
7
+ def chain
8
+ @chain ||= {}
9
+ end
10
+
11
+ def fields(*args)
12
+ chain[:fields] = args.join(',')
13
+ @klass
14
+ end
15
+
16
+ def exclude(*args)
17
+ chain[:exclude] = args.join(',')
18
+ @klass
19
+ end
20
+
21
+ def where(args)
22
+ raise_on_invalid_scope_chain(args)
23
+
24
+ chain[:where] = chain[:where] ? chain[:where].merge!(args) : args
25
+ @klass
26
+ end
27
+
28
+ def limit(args)
29
+ chain[:limit] = args
30
+ @klass
31
+ end
32
+
33
+ def offset(args)
34
+ chain[:offset] = args
35
+ @klass
36
+ end
37
+
38
+ def sort(args)
39
+ chain[:sort] = args
40
+ @klass
41
+ end
42
+
43
+ def search(args)
44
+ chain[:search] = format('"%<term>s"', term: args)
45
+ @klass
46
+ end
47
+
48
+ def query(args)
49
+ chain[:query] = args
50
+ @klass
51
+ end
52
+
53
+ private
54
+
55
+ def raise_on_invalid_scope_chain(scope)
56
+ return unless chain[:where]
57
+
58
+ if chain[:where].class != scope.class
59
+ raise ScopeError, /Hash and String where scopes can't be combined./
60
+ end
61
+ if chain[:where].is_a?(String)
62
+ raise ScopeError, /Multiple String where scopes are not supported./
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ class Apicalypse
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,37 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'apicalypse/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ruby-apicalypse'
7
+ spec.version = Apicalypse::VERSION
8
+ spec.authors = ['Stefan Greffenius']
9
+ spec.email = ['stefan.greffenius@gmail.com']
10
+
11
+ spec.summary = 'Ruby wrapper gem for creating simple Apicalypse queries.'
12
+ spec.description = 'More informations related to Apicalypse can be found here: https://apicalypse.io/'
13
+ spec.homepage = 'https://www.github.com/ad2games/ruby-apicalypse'
14
+ spec.license = 'MIT'
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
22
+ # end
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ end
29
+ spec.bindir = 'exe'
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ['lib']
32
+
33
+ spec.add_development_dependency 'bundler', '~> 1.16'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rspec', '~> 3.0'
36
+ spec.add_development_dependency 'webmock', '~> 3.0'
37
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-apicalypse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stefan Greffenius
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-04 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: 'More informations related to Apicalypse can be found here: https://apicalypse.io/'
70
+ email:
71
+ - stefan.greffenius@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/apicalypse.rb
86
+ - lib/apicalypse/exceptions.rb
87
+ - lib/apicalypse/request.rb
88
+ - lib/apicalypse/scope.rb
89
+ - lib/apicalypse/version.rb
90
+ - ruby-apicalypse.gemspec
91
+ homepage: https://www.github.com/ad2games/ruby-apicalypse
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.7.7
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Ruby wrapper gem for creating simple Apicalypse queries.
115
+ test_files: []