GithubHelp home page GithubHelp logo

pmp's Introduction

PMP (Public Media Platform)

Build Status Coverage Status Code Climate Dependency Status Gem Version

Gem to make it easier to use the PMP (Public Media Platform) API, which is a hypermedia API using the collection.doc+json format.

You can learn more about the PMP here:

https://github.com/publicmediaplatform/pmpdocs/wiki

Installation

Add this line to your application's Gemfile:

gem 'pmp'

And then execute:

$ bundle

Or install it yourself as:

$ gem install pmp

Usage

You should usually go through the PMP::Client as a convenience or start with a PMP::CollectionDocument.

Below is a basic guide, you can also see the examples for more info.

To see it used in a project, there is the PMP Importer which, as it says in the name, imports data from podcast feeds and www.prx.org into the PMP.

The importer super class is a good place to look.

# so you need a few things, like the endpoint, but default is "https://api.pmp.io"
endpoint = "https://api-sandbox.pmp.io"

# and you need credentials, the gem doesn't help you create these (yet)
client_id = "thisisnota-real-client-id-soverysorry"
client_secret = "thisisnotarealsecreteither"

# construct the client, setting some config in there
# this will automatically get a token as there is not one set
pmp = PMP::Client.new(client_id: client_id, client_secret: client_secret, endpoint: endpoint)

# or if you have a token already
token = 'thisisnotatoken'
pmp = PMP::Client.new(oauth_token: token, endpoint: endpoint)

# or if you want to get a token
pmp = PMP::Client.new(client_id: client_id, client_secret: client_secret, endpoint: endpoint)
oauth_token = pmp.token

# get the token string out of the token response
puts oauth_token.token
> 'thisisnotatoken'

# so let's get the root doc - an PMP::CollectionDocument instance
root = pmp.root

# or we can get it without the client, since PMP::CollectionDocument defaults to root
root = PMP::CollectionDocument.new(oauth_token: token, endpoint: endpoint)

# wanna get an attribute, act like it is a ruby attribute
puts root.guid
> '04224975-e93c-4b17-9df9-96db37d318f3'

# want to get the links, you can get a list of them by the rels
puts root.links.keys.sort
> ["creator", "edit", "navigation", "query"]

# don't feel like getting root first? Methods on the client get passed on to root
puts pmp.links.keys.sort
> ["creator", "edit", "navigation", "query"]

# want to get the creator link? (N.B. following a link always returns an Array, note use of `first`):
puts pmp.links["creator"].first
> #<PMP::Link href="https://api-sandbox.pmp.io/docs/af676335-21df-4486-ab43-e88c1b48f026">

# get the same thing as a method
puts pmp.creator.first
> #<PMP::Link href="https://api-sandbox.pmp.io/docs/af676335-21df-4486-ab43-e88c1b48f026">

# like the root doc itself, this is lazy loaded
# but ask for an attribute on there, and you'll get the doc loaded up
puts pmp.creator.first.guid

#### http get request to link href occurs, loads info about the creator
> 'af676335-21df-4486-ab43-e88c1b48f026'

Saving and Deleting

Once you have a doc, you can save or delete it like so:

# create a new blank doc, will generatr a guid automatically if not present
doc = PMP::CollectionDocument.new()
doc.title = "this is an example, ok?"
doc.save

# get that guid!
guid = doc.guid

# how about the link to self
self_href = doc.self.first.href

# get a new doc using self, could just have used the link, this is a bad example perhaps
doc = PMP::CollectionDocument.new(href: self_href)

# update an existing attribute
doc.title = "this is another awesome example, cool?"

# can add an attribute (doesn't check schema yet)
doc.adding_an_attribute = "this will get saved as a new attribute adding-an-attribute"

# can add links (doesn't check schema yet)
doc.links['some-new-link'] = PMP::Link.new({href:'http://somenewlink.io'})
new_link = doc.some_new_link

# save changes
doc.save

# never mind, delete it
doc.delete

Credits

Very big hat tip to the hyperresource gem: https://github.com/gamache/hyperresource

To Do

Think about integrating this lovely json schema parsing project: https://github.com/google/autoparse

or this one: https://github.com/hoxworth/json-schema

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

pmp's People

Contributors

bricker avatar cavis avatar kookster avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

bricker cavis

pmp's Issues

Links should retain their JSON type when being converted to ruby structures

This came up for us when importing images/audio into our system โ€” PMP always represents enclosures as arrays, but this line removes the array if there's only one item, which forces us to wrap the enclosure in an Array. I think it's better to keep as close as possible to the data structure provided by the PMP API. Removing this line would break this library's API pretty significantly (and breaks some tests too). Do you have any thoughts on this?

URN in permissions.rb has changed

It looks like the URN used in the query has changed to urn:collectiondoc:query:docs.

Also, could there be better docs about how to query? It took me about 30 minutes to figure it out.

New oauth2 versions can't authenticate

For some reason, oauth2 version 1.4.2 can't authenticate with the PMP using client credentials. It gets an unauthorized. Something appears to have broken between 1.0.0 and 1.4.2.

Should try to isolate/fix that, or at least lock the gemspec to avoid bad versions.

Undefined method `oauth_token=' for #<Faraday::ConnectionOptions>

Seen with Ruby 2.0.0-p247, PMP 0.3.1, Faraday 0.9.0. No matter which way I instantiate the client, I get either a undefined method client_id= or undefined method oauth_token= from Faraday.

> pmp = PMP::Client.new(oauth_token: "12345678", endpoint: "https://api-sandbox.pmp.io")
> pmp.root.guid

NoMethodError: undefined method `client_id=' for #<Faraday::ConnectionOptions:0x007fa631c722f0>
    from /Users/cavis/.rvm/gems/ruby-2.0.0-p247/gems/faraday-0.9.0/lib/faraday/options.rb:31:in `block in update'
    from /Users/cavis/.rvm/gems/ruby-2.0.0-p247/gems/faraday-0.9.0/lib/faraday/options.rb:20:in `each'
    from /Users/cavis/.rvm/gems/ruby-2.0.0-p247/gems/faraday-0.9.0/lib/faraday/options.rb:20:in `update'
    from /Users/cavis/.rvm/gems/ruby-2.0.0-p247/gems/faraday-0.9.0/lib/faraday/options.rb:52:in `merge'
    from /Users/cavis/.rvm/gems/ruby-2.0.0-p247/gems/faraday-0.9.0/lib/faraday.rb:69:in `new'
    from /Users/cavis/.rvm/gems/ruby-2.0.0-p247/gems/oauth2-1.0.0/lib/oauth2/client.rb:54:in `connection'
    from /Users/cavis/.rvm/gems/ruby-2.0.0-p247/gems/oauth2-1.0.0/lib/oauth2/client.rb:73:in `token_url'
    from /Users/cavis/.rvm/gems/ruby-2.0.0-p247/gems/oauth2-1.0.0/lib/oauth2/client.rb:138:in `get_token'
    from /Users/cavis/.rvm/gems/ruby-2.0.0-p247/gems/oauth2-1.0.0/lib/oauth2/strategy/client_credentials.rb:24:in `get_token'
    from /Users/cavis/.rvm/gems/ruby-2.0.0-p247/gems/pmp-0.3.1/lib/pmp/token.rb:33:in `get_token'
    from /Users/cavis/.rvm/gems/ruby-2.0.0-p247/gems/pmp-0.3.1/lib/pmp/collection_document.rb:127:in `setup_oauth_token'
    from /Users/cavis/.rvm/gems/ruby-2.0.0-p247/gems/pmp-0.3.1/lib/pmp/collection_document.rb:136:in `request'
    from /Users/cavis/.rvm/gems/ruby-2.0.0-p247/gems/pmp-0.3.1/lib/pmp/collection_document.rb:83:in `load'
    from /Users/cavis/.rvm/gems/ruby-2.0.0-p247/gems/pmp-0.3.1/lib/pmp/collection_document.rb:162:in `method_missing'
    from (irb):12
    from /Users/cavis/.rvm/rubies/ruby-2.0.0-p247/bin/irb:12:in `<main>'

Updates to PMP home doc links

An upcoming release to the PMP will introduce some changes to home document links. Changes have already been deployed to https://api-sandbox.pmp.io. Here's my synopsis of what needs to happen for this SDK:

Breaking changes:

  • urn:collectiondoc:form:documentdelete has been added, and the DELETE method removed from urn:collectiondoc:form:documentsave. The only impact is that PMP::CollectionDocument.delete calls to edit_link('DELETE'), which would now raise an exception that the method isn't allowed.

Cleanup changes:

  • regenerate fixtures
  • urn:collectiondoc:query:guids should be removed from tests/fixtures
  • urn:collectiondoc:form:profilesave should be removed from tests/fixtures
  • urn:collectiondoc:form:schemasave should be removed from tests/fixtures

Possible upgrades:

  • urn:collectiondoc:form:listcredentials has been added (use instead of hardcoding)
  • use urn:collectiondoc:form:issuetoken and urn:collectiondoc:form:revoketoken instead of hardcoding the links

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.