prx_access 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 993d498f3a6140c1f29f964f5d77eca7c73361057bea37efb2adc485ca00a60a
4
+ data.tar.gz: 6be33628433e2090b0b7bed7a06098b6b4d968809d75aa825bedec3caac60eea
5
+ SHA512:
6
+ metadata.gz: 8fae3fac0e2698b9eb9133b06c645ef40328af0045429d3e237c885044655594c58b14f642f73e3581f0d64854940f8c35eef2a183d51a242fb65f0c5fd88665
7
+ data.tar.gz: 4d1a4587263130aaa5b4f14f43c459ca9c4d8e176772ec24cd3cc7ce00a5fda804c7dc3951733b225d3a1ae47606237d2b9fd65fa21547ee7df517e0378eba91
@@ -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
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 1.17.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at sam.vevang@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in prx_access.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Public Radio Exchange
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,76 @@
1
+ # PrxAccess
2
+
3
+ Welcome to PRX Access. This gem allows programmatic to the PRX HAL apis.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'prx_access'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install prx_access
20
+
21
+ ## Usage
22
+
23
+ PRXAccess is meant to be used as a mixin for your classes. Here's a
24
+ short example of how to fetch podcasts from feeder in Ruby:
25
+
26
+ ```
27
+ Running via Spring preloader in process 35676
28
+ Loading development environment (Rails 4.2.10)
29
+ irb(main):001:0> include PRXAccess
30
+ => Object
31
+ irb(main):002:0> feeder_api = api(root: feeder_root)
32
+ => #<PRXAccess::PRXHyperResource:0x3fce3bda28e0 @root="https://feeder.prx.org/api/v1" @href=nil @loaded=false @namespace="PRXAccess::PRXHyperResource" ...>
33
+ irb(main):003:0> root_api = feeder_api.get
34
+ => #<PRXAccess::PRXHyperResource:0x3fce3bd1be08 @root="https://feeder.prx.org/api/v1" @href="/api/v1" @loaded=true @namespace="PRXAccess::PRXHyperResource" ...>
35
+ irb(main):004:0> podcasts_api = root_api.podcasts.first
36
+ => #<PRXAccess::PRXHyperResource::Link:0x00007f9c779ee510 @resource=#<PRXAccess::PRXHyperResource:0x3fce3bd1be08 @root="https://feeder.prx.org/api/v1" @href="/api/v1" @loaded=true @namespace="PRXAccess::PRXHyperResource" ...>, @base_href="/api/v1/podcasts{?page,per,zoom}", @name=nil, @templated=true, @params={}, @default_method="get", @headers={}, @type=nil, @profile="http://meta.prx.org/model/collection/podcasts">
37
+ irb(main):005:0> podcasts_api.get.body
38
+ => {"count"=>10, "total"=>122, "_embedded"=>{"prx:items"=>[ ... <snip json>
39
+ ```
40
+
41
+ It's also possible to chain the calls:
42
+
43
+ ```ruby
44
+ api(root: feeder_root)
45
+ .get
46
+ .podcasts.first
47
+ .get
48
+ .body
49
+ ```
50
+
51
+ ## Development
52
+
53
+ After checking out the repo, run `bin/setup` to install dependencies.
54
+ Then, run `rake` to run the tests. You can also run `bin/console`
55
+ for an interactive prompt that will allow you to experiment.
56
+
57
+ To install this gem onto your local machine, run `bundle exec rake
58
+ install`. To release a new version, update the version number in
59
+ `version.rb`, and then run `bundle exec rake release`, which will create
60
+ a git tag for the version, push git commits and tags, and push the
61
+ `.gem` file to [rubygems.org](https://rubygems.org).
62
+
63
+ ## Contributing
64
+
65
+ Bug reports and pull requests are welcome on GitHub at
66
+ https://github.com/[USERNAME]/prx_access. This project is intended to be
67
+ a safe, welcoming space for collaboration, and contributors are expected
68
+ to adhere to the [Contributor Covenant](http://contributor-covenant.org)
69
+ code of conduct.
70
+
71
+ ## Code of Conduct
72
+
73
+ Everyone interacting in the PrxAccess project’s codebases, issue
74
+ trackers, chat rooms and mailing lists is expected to follow the [code
75
+ of
76
+ conduct](https://github.com/[USERNAME]/prx_access/blob/master/CODE_OF_CONDUCT.md).
@@ -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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "prx_access"
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__)
@@ -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 "prx_access/version"
2
+ require "prx_access/prx_hyper_resource"
3
+
4
+ module PRXAccess
5
+ end
@@ -0,0 +1,141 @@
1
+ require 'hyperresource'
2
+ require 'active_support/core_ext/hash'
3
+
4
+ module PRXAccess
5
+
6
+ class PRXHyperResource < HyperResource
7
+ def incoming_body_filter(hash)
8
+ super(hash.deep_transform_keys { |key| key.to_s.underscore })
9
+ end
10
+
11
+ def outgoing_body_filter(hash)
12
+ super(hash.deep_transform_keys { |key| key.to_s.camelize(:lower) })
13
+ end
14
+
15
+ class Link < HyperResource::Link
16
+ attr_accessor :type, :profile
17
+
18
+ def initialize(resource, link_spec={})
19
+ super
20
+ self.type = link_spec['type']
21
+ self.profile = link_spec['profile']
22
+ end
23
+
24
+ def where(params)
25
+ super.tap do |res|
26
+ res.type = self.type
27
+ res.profile = self.profile
28
+ end
29
+ end
30
+
31
+ def headers(*args)
32
+ super.tap do |res|
33
+ if args.count > 0
34
+ res.type = self.type
35
+ res.profile = self.profile
36
+ end
37
+ end
38
+ end
39
+
40
+ def post_response(attrs=nil)
41
+ attrs ||= self.resource.attributes
42
+ attrs = (self.resource.default_attributes || {}).merge(attrs)
43
+
44
+ # adding this line to call outgoing_body_filter
45
+ attrs = resource.outgoing_body_filter(attrs)
46
+
47
+ response = faraday_connection.post do |req|
48
+ req.body = self.resource.adapter.serialize(attrs)
49
+ end
50
+ response
51
+ end
52
+
53
+ def put_response(attrs=nil)
54
+ attrs ||= self.resource.attributes
55
+ attrs = (self.resource.default_attributes || {}).merge(attrs)
56
+
57
+ # adding this line to call outgoing_body_filter
58
+ attrs = resource.outgoing_body_filter(attrs)
59
+
60
+ response = faraday_connection.put do |req|
61
+ req.body = self.resource.adapter.serialize(attrs)
62
+ end
63
+ response
64
+ end
65
+
66
+ def patch_response(attrs=nil)
67
+ attrs ||= self.resource.attributes.changed_attributes
68
+ attrs = (self.resource.default_attributes || {}).merge(attrs)
69
+
70
+ # adding this line to call outgoing_body_filter
71
+ attrs = resource.outgoing_body_filter(attrs)
72
+
73
+ response = faraday_connection.patch do |req|
74
+ req.body = self.resource.adapter.serialize(attrs)
75
+ end
76
+ response
77
+ end
78
+ end
79
+ end
80
+
81
+ def default_headers
82
+ {
83
+ 'Content-Type' => 'application/json',
84
+ 'Accept' => 'application/json'
85
+ }
86
+ end
87
+
88
+ def api(options = {})
89
+ opts = { root: cms_root, headers: default_headers }.merge(options)
90
+ if !opts[:headers]['Authorization'] && account = opts.delete(:account)
91
+ token = get_account_token(account)
92
+ opts[:headers]['Authorization'] = "Bearer #{token}"
93
+ end
94
+
95
+ PRXHyperResource.new(opts)
96
+ end
97
+
98
+ def api_resource(body, root = cms_root)
99
+ href = body['_links']['self']['href']
100
+ resource = api(root: root)
101
+ link = PRXHyperResource::Link.new(resource, href: href)
102
+ PRXHyperResource.new_from(body: body, resource: resource, link: link)
103
+ end
104
+
105
+ def get_account_token(account)
106
+ id = ENV['PRX_CLIENT_ID']
107
+ se = ENV['PRX_SECRET']
108
+ oauth_options = { site: id_root, token_url: '/token' }
109
+ client = OAuth2::Client.new(id, se, oauth_options) do |faraday|
110
+ faraday.request :url_encoded
111
+ faraday.adapter :excon
112
+ end
113
+ client.client_credentials.get_token(account: account).token
114
+ end
115
+
116
+ def id_root
117
+ root_uri ENV['ID_HOST']
118
+ end
119
+
120
+ private
121
+
122
+ def method_missing(method, *args)
123
+ if method =~ /_root$/
124
+ root_uri ENV[method.to_s.sub(/_root$/, '_HOST').upcase], '/api/v1'
125
+ else
126
+ super
127
+ end
128
+ end
129
+
130
+ def respond_to_missing?(method, include_private = false)
131
+ method.to_s.ends_with?('_root') || super
132
+ end
133
+
134
+ def root_uri(host, path = '')
135
+ if host =~ /\.org|\.tech/
136
+ URI::HTTPS.build(host: host, path: path).to_s
137
+ else
138
+ URI::HTTP.build(host: host, path: path).to_s
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,3 @@
1
+ module PRXAccess
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,40 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "prx_access/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "prx_access"
7
+ spec.version = PRXAccess::VERSION
8
+ spec.authors = ["Sam Vevang"]
9
+ spec.email = ["sam.vevang@prx.org"]
10
+
11
+ spec.summary = %q{PRX Access navigates HAL hypermedia resources on PRX services.}
12
+ spec.description = %q{PRX Access is a gem to supply authentication headers and descend the HAL hypermedia link tree on PRX's APIs.}
13
+ spec.homepage = "https://github.com/PRX/prx_access"
14
+
15
+ if spec.respond_to?(:metadata)
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/PRX/prx_access"
18
+ else
19
+ raise "RubyGems 2.0 or newer is required to protect against " \
20
+ "public gem pushes."
21
+ end
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency "hyperresource", "~> 0.9.4"
33
+ spec.add_dependency "activesupport", ">= 4.0.0"
34
+
35
+ spec.add_development_dependency "webmock"
36
+ spec.add_development_dependency "pry"
37
+ spec.add_development_dependency "bundler", "~> 1.17"
38
+ spec.add_development_dependency "rake", "~> 10.0"
39
+ spec.add_development_dependency "rspec", "~> 3.0"
40
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prx_access
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sam Vevang
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hyperresource
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 4.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 4.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.17'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.17'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ description: PRX Access is a gem to supply authentication headers and descend the
112
+ HAL hypermedia link tree on PRX's APIs.
113
+ email:
114
+ - sam.vevang@prx.org
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - LICENSE
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - lib/prx_access.rb
130
+ - lib/prx_access/prx_hyper_resource.rb
131
+ - lib/prx_access/version.rb
132
+ - prx_access.gemspec
133
+ homepage: https://github.com/PRX/prx_access
134
+ licenses: []
135
+ metadata:
136
+ homepage_uri: https://github.com/PRX/prx_access
137
+ source_code_uri: https://github.com/PRX/prx_access
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.7.6
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: PRX Access navigates HAL hypermedia resources on PRX services.
158
+ test_files: []