Skip to content

Commit

Permalink
Merge pull request #21 from jsmestad/develop
Browse files Browse the repository at this point in the history
Release v1.0 as a active fork based on JsonApiClient
  • Loading branch information
jsmestad authored Apr 13, 2018
2 parents 1617f9b + b52f89c commit 4f23779
Show file tree
Hide file tree
Showing 105 changed files with 6,405 additions and 1,762 deletions.
27 changes: 27 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Ruby CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
#
version: 2
jobs:
build:
docker:
# specify the version you desire here
- image: circleci/ruby:2.5
working_directory: ~/jsonapi_consumer

steps:
- checkout

- run:
name: install dependencies
command: |
bundle install --jobs=4 --retry=3 --path vendor/bundle
- run:
command: env CI=true bundle exec rake test
when: always

# collect reports
- store_test_results:
path: /tmp/reports
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.bundle/
/.ruby-version
/.yardoc
/Gemfile.lock
/_yardoc/
Expand Down
2 changes: 0 additions & 2 deletions .rspec

This file was deleted.

9 changes: 0 additions & 9 deletions .travis.yml

This file was deleted.

36 changes: 0 additions & 36 deletions CHANGELOG.md

This file was deleted.

10 changes: 6 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
source 'https://rubygems.org'
source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

# Specify your gem's dependencies in jsonapi-consumer.gemspec
gemspec

gem 'fsevent'
gem 'guard-rspec', require: false
gem 'rake'
gem 'minitest-ci'
gem 'pry'
6 changes: 0 additions & 6 deletions Guardfile

This file was deleted.

48 changes: 10 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

An ActiveModel-compliant consumer framework for communicating with JSONAPI-based APIs.

[![Build Status](https://travis-ci.org/jsmestad/jsonapi-consumer.svg?branch=master)](https://travis-ci.org/jsmestad/jsonapi-consumer)
[![CircleCI](https://circleci.com/gh/jsmestad/jsonapi-consumer.svg?style=svg)](https://circleci.com/gh/jsmestad/jsonapi-consumer)


## JSONAPI Compatibility

Expand All @@ -13,34 +14,30 @@ The [master branch](https://github.com/jsmestad/jsonapi-consumer/tree/master) is
Add this line to your application's Gemfile:

```ruby
gem 'jsonapi-consumer'
gem 'jsonapi-consumer', github: 'jsmestad/jsonapi-consumer', branch: 'develop'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install jsonapi-consumer

## Usage

It's suggested to create a base resource for the whole API that you can re-use.

```ruby
class Base
include JSONAPI::Consumer::Resource

self.host = 'http://localhost:3000/api/'
class Base < JSONAPI::Consumer::Resource
# self.connection_options = {} # Faraday connection options
# self.json_key_format = :dasherized_key # (default: underscored_key)
# self.route_format = :dasherized_route # (default: underscored_route)
self.site = 'http://localhost:3000/api/'
end
```

Then inherit from that Base class for each resource defined in your API.

```ruby
module Blog

class Author < Base
has_many :posts, class_name: 'Blog::Post'
end
Expand All @@ -57,36 +54,9 @@ module Blog
class Comment < Base

end

end
```

#### Additional Features

##### Dynamic Objects

By default calling `.new` or `.build` on a resource will give you an empty
object with no attributes defined. This is less than ideal when building forms
with something like Rails' FormBuilder.

We suggest setting up your model to do a `GET /{resource_name}/new` if your
server supports it and using `.build` instead of `.new`. This will populate the
object with defaults set by the server response.

```ruby
class User
include JSONAPI::Consumer::Resource

self.request_new_object_on_build = true

# .build will now call GET /users/new
end
```

#### Testing

We suggest [Webmock](https://github.com/bblimke/webmock) at this stage in
development. We plan to add test helpers before the first major release.

## Contributing

Expand All @@ -99,3 +69,5 @@ development. We plan to add test helpers before the first major release.
## Copyright & License

JSONAPI::Consumer is distributed under the Apache 2.0 License. See LICENSE.txt file for more information.

Version v1 is a rewrite is based on the excellent work by [json_api_client](https://github.com/chingor13/json_api_client) [v1.5.3](https://github.com/chingor13/json_api_client/tree/v1.5.3).
23 changes: 17 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
require "bundler/gem_tasks"

begin
require 'rspec/core/rake_task'
require 'rdoc/task'

RSpec::Core::RakeTask.new(:spec)
RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'JsonApiClient'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('lib/**/*.rb')
end


require 'rake/testtask'

task default: :spec
rescue LoadError
# no rspec available
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end

task default: :test
14 changes: 14 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "mikasa_client"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
require "pry"
Pry.start(__FILE__)

# require "irb"
# IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
30 changes: 19 additions & 11 deletions jsonapi-consumer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,28 @@ Gem::Specification.new do |spec|
spec.homepage = "https://github.com/jsmestad/jsonapi-consumer"
spec.license = "Apache-2.0"

spec.files = `git ls-files -z`.split("\x0") - ['Guardfile', '.travis.yml']
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
if spec.respond_to?(:metadata)
spec.metadata["allowed_push_host"] = ""
else
raise "RubyGems 2.0 or newer is required to protect against " \
"public gem pushes."
end

spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_runtime_dependency "activemodel"
spec.add_runtime_dependency "activesupport"
spec.add_runtime_dependency "faraday", "~> 0.9.0"
spec.add_runtime_dependency "activesupport", '>= 3.2'
spec.add_runtime_dependency "faraday", '>= 0.9'
spec.add_runtime_dependency "faraday_middleware"
spec.add_runtime_dependency "addressable", '~> 2.5.2'
spec.add_runtime_dependency "activemodel", '>= 3.2'

spec.add_development_dependency "bundler", "~> 1.6"
spec.add_development_dependency "factory_girl"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec"
spec.add_development_dependency "rspec-its"
spec.add_development_dependency "webmock"
spec.add_development_dependency "mocha"
end
93 changes: 59 additions & 34 deletions lib/jsonapi/consumer.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,66 @@
require "jsonapi/consumer/version"

require 'faraday'
require 'faraday_middleware'
require 'active_model'

require "active_support/concern"
require "active_support/core_ext"
require "active_support/inflector"
require "faraday"
require "faraday_middleware"
require "addressable/uri"
require "active_support/core_ext/string"

module JSONAPI
module Consumer

module Associations
autoload :BaseAssociation, 'jsonapi/consumer/associations/base_association'
autoload :BelongsTo, 'jsonapi/consumer/associations/belongs_to'
autoload :HasMany, 'jsonapi/consumer/associations/has_many'
autoload :HasOne, 'jsonapi/consumer/associations/has_one'
end

module Helpers
autoload :Callbacks, 'jsonapi/consumer/helpers/callbacks'
autoload :Dirty, 'jsonapi/consumer/helpers/dirty'
autoload :DynamicAttributes, 'jsonapi/consumer/helpers/dynamic_attributes'
autoload :URI, 'jsonapi/consumer/helpers/uri'
end

module Linking
autoload :Links, "jsonapi/consumer/linking/links"
autoload :TopLevelLinks, "jsonapi/consumer/linking/top_level_links"
end

module Relationships
autoload :Relations, "jsonapi/consumer/relationships/relations"
autoload :TopLevelRelations, "jsonapi/consumer/relationships/top_level_relations"
end

module Middleware
autoload :JsonRequest, 'jsonapi/consumer/middleware/json_request'
autoload :ParseJson, 'jsonapi/consumer/middleware/parse_json'
autoload :Status, 'jsonapi/consumer/middleware/status'
end

module Paginating
autoload :Paginator, 'jsonapi/consumer/paginating/paginator'
end

module Parsers
autoload :Parser, 'jsonapi/consumer/parsers/parser'
end

module Query
autoload :Builder, 'jsonapi/consumer/query/builder'
autoload :Requestor, 'jsonapi/consumer/query/requestor'
end

autoload :Connection, 'jsonapi/consumer/connection'
autoload :Errors, 'jsonapi/consumer/errors'
autoload :ErrorCollector, 'jsonapi/consumer/error_collector'
autoload :Formatter, 'jsonapi/consumer/formatter'
autoload :Implementation, 'jsonapi/consumer/implementation'
autoload :IncludedData, 'jsonapi/consumer/included_data'
autoload :MetaData, 'jsonapi/consumer/meta_data'
autoload :Resource, 'jsonapi/consumer/resource'
autoload :ResultSet, 'jsonapi/consumer/result_set'
autoload :Schema, 'jsonapi/consumer/schema'
autoload :Utils, 'jsonapi/consumer/utils'
end
end

require "jsonapi/consumer/errors"

require "jsonapi/consumer/middleware"
require "jsonapi/consumer/middleware/parse_json"
require "jsonapi/consumer/middleware/raise_error"
require "jsonapi/consumer/middleware/request_headers"
require "jsonapi/consumer/middleware/request_timeout"

require "jsonapi/consumer/parser"

require "jsonapi/consumer/query"
require "jsonapi/consumer/query/base"
require "jsonapi/consumer/query/create"
require "jsonapi/consumer/query/delete"
require "jsonapi/consumer/query/find"
require "jsonapi/consumer/query/new"
require "jsonapi/consumer/query/update"

require "jsonapi/consumer/resource/association_concern"
require "jsonapi/consumer/resource/attributes_concern"
require "jsonapi/consumer/resource/connection_concern"
require "jsonapi/consumer/resource/finders_concern"
require "jsonapi/consumer/resource/object_build_concern"
require "jsonapi/consumer/resource/serializer_concern"
require "jsonapi/consumer/resource"
end
Loading

0 comments on commit 4f23779

Please sign in to comment.