Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release v1.0 as a active fork based on JsonApiClient #21

Merged
merged 30 commits into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
138cbd8
Persistence to work more like ActiveRecord
Feb 25, 2015
64bce72
Create query to always assume not persisted.
Feb 25, 2015
3401e80
ignore ruby-version
jsmestad Mar 4, 2015
fc02613
Update a spec to demonstate simulating persited object.
Mar 17, 2015
559f911
Merge pull request #17 from danelowe/feature/persistence
jsmestad Mar 18, 2015
3d0dc13
Update attributes_spec.rb
jsmestad Mar 18, 2015
3b82499
failing specs using Beta payload
jsmestad Mar 4, 2015
8b4dc05
First pass at creating a JSONAPI RC parser
jsmestad Mar 4, 2015
5ba68fd
remove unused before block
jsmestad Mar 4, 2015
28ca90c
Resolve edge case where constants collided
jsmestad Mar 5, 2015
ab82a65
Switch to Beta parser by default; leaving Stable if regressions need …
jsmestad Mar 18, 2015
47c9559
add notes about branch differences
jsmestad Mar 18, 2015
b5163e8
#serializable_hash establishes a "type" value based on Resource.json_key
jsmestad Mar 18, 2015
94dc58e
add_link and add_links properly outputs type
jsmestad Mar 18, 2015
75e047a
Use application/vnd.api+json for both Accept and Content-Type headers
jsmestad Mar 18, 2015
480863f
Parser can now appropriately handle single resource responses.
mattmueller May 5, 2015
0333c4d
Use included key instead of linked key for fetching associated resour…
mattmueller May 5, 2015
6c7bfbc
Update parser to expect linkage objects inside of the links object.
mattmueller May 5, 2015
7b82327
Merge pull request #18 from mattmueller/update_beta_parser_to_accomod…
jsmestad May 14, 2015
a1ffa8c
Base new v1.0 JSONAPI::Consumer off of JsonApiClient v1.5.3
jsmestad Apr 13, 2018
abf35ea
Update README
jsmestad Apr 13, 2018
04a2053
Get specs passing
jsmestad Apr 13, 2018
bbbff61
Get CircleCI set up
jsmestad Apr 13, 2018
16ed089
Swap build badge for CircleCI
jsmestad Apr 13, 2018
e841b81
Consolidate files and loading to main file
jsmestad Apr 13, 2018
5ef0bd4
Pull in patch to allow inherit custom headers from parent
jsmestad Apr 13, 2018
dc5c669
Faster dyanmic attributes from @danbernier
jsmestad Apr 13, 2018
5cdb69a
Apply QueryBuilder instead of Array change for HasMany associations.
jsmestad Apr 13, 2018
02d8749
HasOne assocation will now fetch association when not included.
jsmestad Apr 13, 2018
b52f89c
Update gemspec, add console
jsmestad Apr 13, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,41 +2,38 @@

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)


## Installation

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 @@ -53,36 +50,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 @@ -95,3 +65,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