documentcloud 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Miles Zimmerman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ DocumentCloud RubyGem
2
+ =====================
3
+
4
+ Rubygem for interacting with the DocumentCloud API
5
+
6
+
7
+ ## Usage
8
+
9
+ ```gem install documentcloud``` or put in your gemfile and ```bundle install```
10
+
11
+ To authenticate, initialize the configuration and pass in a block:
12
+
13
+ ``` ruby
14
+ DocumentCloud.configure do |config|
15
+ config.email = 'my_email@somedomain.com'
16
+ config.password = 'my_secret_password'
17
+ end
18
+ ```
19
+
20
+
21
+ ### Search
22
+
23
+ All search results return Document objects that has methods and accessors for the information.
24
+
25
+ ``` ruby
26
+ search = DocumentCloud.search('my query')
27
+
28
+ puts search.total # returns number of results
29
+
30
+ puts search.documents[0].pdf
31
+ ```
32
+
33
+
34
+ ### Upload
35
+
36
+ You can upload a document by providing a ruby File object, or a string with the url to a file.
37
+
38
+ ``` ruby
39
+ local_doc = DocumentCloud.upload(File.new('/my/file/path.pdf','rb'), 'My Document Title')
40
+
41
+ remote_doc = DocumentCloud.upload('http://somesite.com/file.pdf', 'Document Title')
42
+ ```
43
+
44
+ Both return a document object which can then be used.
45
+
46
+
47
+ ### Information
48
+
49
+ DocumentCloud API info: http://www.documentcloud.org/help/api
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'document_cloud/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.add_dependency "rest-client", "~> 1.6.7"
8
+ spec.add_dependency 'multi_json', '~> 1.8'
9
+
10
+ spec.name = 'documentcloud'
11
+ spec.version = DocumentCloud::Version
12
+ spec.description = "Rubygem for interacting with the DocumentCloud API"
13
+ spec.summary = spec.description
14
+ spec.authors = ["Miles Zimmerman"]
15
+ spec.email = ['miles@zserver.org']
16
+ spec.homepage = 'https://github.com/mileszim/documentcloud'
17
+ spec.licenses = ['MIT']
18
+
19
+ spec.files = %w(LICENSE README.md documentcloud.gemspec)
20
+ spec.files += Dir.glob("lib/**/*.rb")
21
+
22
+ spec.require_paths = ['lib']
23
+ end
@@ -0,0 +1,43 @@
1
+ require 'rest_client'
2
+ require 'multi_json'
3
+ require 'date'
4
+
5
+ require_relative 'document_cloud/document'
6
+ require_relative 'document_cloud/search_results'
7
+ require_relative 'document_cloud/default'
8
+ require_relative 'document_cloud/configurable'
9
+ require_relative 'document_cloud/client'
10
+
11
+ module DocumentCloud
12
+ class << self
13
+ include DocumentCloud::Configurable
14
+
15
+ # Delegate to a DocumentCloud::Client
16
+ #
17
+ # @return [DocumentCloud::Client]
18
+ def client
19
+ @client = DocumentCloud::Client.new(credentials) unless defined?(@client)
20
+ @client
21
+ end
22
+
23
+ # Has a client been initialized on the DocumentCloud module?
24
+ #
25
+ # @return [Boolean]
26
+ def client?
27
+ !!@client
28
+ end
29
+
30
+
31
+ def respond_to?(method_name, include_private=false)
32
+ client.respond_to?(method_name, include_private) || super
33
+ end
34
+
35
+ private
36
+
37
+ def method_missing(method_name, *args, &block)
38
+ return super unless client.respond_to?(method_name)
39
+ client.send(method_name, *args, &block)
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,23 @@
1
+ module DocumentCloud
2
+ module API
3
+ module Document
4
+ include DocumentCloud::API::Utils
5
+ DOCUMENT_PATH = "/documents/"
6
+
7
+ # Fetch a document
8
+ #
9
+ # @param id [String] The document id
10
+ # @returns [DocumentCloud::Document] The fetched document
11
+ def document(id)
12
+ build_object DocumentCloud::Document, get(document_path(id))
13
+ end
14
+
15
+ private
16
+
17
+ def document_path(document_id)
18
+ "#{DOCUMENT_PATH}/#{document_id.to_s}.json"
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ module DocumentCloud
2
+ module API
3
+ module Search
4
+ include DocumentCloud::API::Utils
5
+ SEARCH_PATH = '/search.json'
6
+
7
+ # Search catalog of public documents
8
+ #
9
+ # @see http://www.documentcloud.org/help/api
10
+ # @param q [String] The search query
11
+ # @param options [Hash] Customizable set of options
12
+ # @param options [Integer] :page Response page number
13
+ # @param options [Integer] :per_page The number of documents to return per page
14
+ # @param options [Boolean] :sections Include document sections in the results
15
+ # @param options [Boolean] :annotations Include document annotations in the results
16
+ # @param options [Boolean] :data Include key/value data in the results
17
+ # @param options [Integer] :mentions Include highlighted mentions of the search phrase
18
+ # @returns [DocumentCloud::SearchResults] Results of the search
19
+ def search(q, options={})
20
+ build_object DocumentCloud::SearchResults, get(SEARCH_PATH, options.merge(:q => q))
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ module DocumentCloud
2
+ module API
3
+ module Upload
4
+ include DocumentCloud::API::Utils
5
+ UPLOAD_PATH = '/upload.json'
6
+
7
+ # Upload a document
8
+ #
9
+ # @see http://www.documentcloud.org/help/api
10
+ # @param file [String|File] Local file to upload, or remote url of file
11
+ # @param title [String] The document's canonical title
12
+ # @param options [Hash] Customizable set of options
13
+ # @param options [String] :source The source who produced the document
14
+ # @param options [String] :description A paragraph of detailed description
15
+ # @param options [String] :related_article The URL of the article associated with the document
16
+ # @param options [String] :published_url The URL of the page on which the document will be embedded
17
+ # @param options [String] :access One of 'public', 'private', 'organization', defaults to 'private'
18
+ # @param options [Integer] :project A numeric Project id, to upload the document into an existing project.
19
+ # @param options [Hash] :data A hash of arbitrary key/value data pairs
20
+ # @param options [Boolean] :secure If you're dealing with a truly sensitive document, pass the "secure" parameter in order to prevent the document from being sent to OpenCalais for entity extraction.
21
+ # @returns [DocumentCloud::Document] The document uploaded
22
+ def upload(file, title, options={})
23
+ build_object DocumentCloud::Document, post(UPLOAD_PATH, options.merge(file: file, title: title))
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ module DocumentCloud
2
+ module API
3
+ module Utils
4
+
5
+ def parse_json(json)
6
+ MultiJson.load(json, symbolize_keys: true)
7
+ end
8
+
9
+ def build_object(object, json)
10
+ parsed = parse_json json
11
+ parsed = parsed[:document] if parsed[:document]
12
+ object.new(parsed)
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,37 @@
1
+ require_relative 'configurable'
2
+ require_relative 'api/utils'
3
+ require_relative 'api/search'
4
+ require_relative 'api/upload'
5
+ require_relative 'api/document'
6
+
7
+ module DocumentCloud
8
+ class Client
9
+ include DocumentCloud::API::Utils
10
+ include DocumentCloud::API::Search
11
+ include DocumentCloud::API::Upload
12
+ include DocumentCloud::API::Document
13
+ include DocumentCloud::Configurable
14
+
15
+ def initialize(options={})
16
+ DocumentCloud::Configurable.keys.each do |key|
17
+ instance_variable_set(:"@#{key}", options[key] || DocumentCloud.instance_variable_get(:"@#{key}"))
18
+ end
19
+ end
20
+
21
+ # Perform HTTP GET request
22
+ def get(path, params={})
23
+ RestClient.get request_base+path, {params: params}
24
+ end
25
+
26
+ def post(path, params={})
27
+ RestClient.post request_base+path, params
28
+ end
29
+
30
+ private
31
+
32
+ def request_base
33
+ "#{DocumentCloud::Default.http_mode}://#{@email}:#{@password}@#{DocumentCloud::Default.endpoint}"
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,34 @@
1
+ module DocumentCloud
2
+ module Configurable
3
+ attr_writer :email, :password
4
+
5
+ class << self
6
+ def keys
7
+ @keys ||= [:email, :password]
8
+ end
9
+ end
10
+
11
+ # Allow block configuration
12
+ def configure
13
+ yield self
14
+ format_email!
15
+ self
16
+ end
17
+
18
+ private
19
+
20
+ # @return [Hash]
21
+ def credentials
22
+ {
23
+ email: @email,
24
+ password: @password
25
+ }
26
+ end
27
+
28
+ # Ensure email is correct format for RestClient posts
29
+ def format_email!
30
+ @email.gsub!(/@/, "%40")
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ module DocumentCloud
2
+ module Default
3
+
4
+ ENDPOINT = 'www.documentcloud.org/api' unless defined? DocumentCloud::Default::ENDPOINT
5
+ HTTP_MODE = 'https' unless defined? DocumentCloud::Default::HTTP_mode
6
+
7
+ class << self
8
+
9
+ def endpoint
10
+ ENDPOINT
11
+ end
12
+
13
+ def http_mode
14
+ HTTP_MODE
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,46 @@
1
+ module DocumentCloud
2
+ class Document
3
+ attr_reader :id, :title, :access, :pages, :description, :source,
4
+ :canonical_url, :language, :display_language, :created_at, :updated_at
5
+
6
+ def initialize(attrs={})
7
+ @id = attrs[:id]
8
+ @title = attrs[:title]
9
+ @access = attrs[:access]
10
+ @pages = attrs[:pages]
11
+ @description = attrs[:description]
12
+ @source = attrs[:source]
13
+ @canonical_url = attrs[:canonical_url]
14
+ @language = attrs[:language]
15
+ @display_language = attrs[:display_language]
16
+ @created_at = DateTime.parse(attrs[:created_at])
17
+ @updated_at = DateTime.parse(attrs[:updated_at])
18
+ @resources = attrs[:resources]
19
+ end
20
+
21
+ def pdf
22
+ @resources[:pdf]
23
+ end
24
+
25
+ def print_annotations
26
+ @resources[:print_annotations]
27
+ end
28
+
29
+ def related_article
30
+ @resources[:related_article]
31
+ end
32
+
33
+ def text
34
+ @resources[:text]
35
+ end
36
+
37
+ def thumbnail
38
+ @resources[:thumbnail]
39
+ end
40
+
41
+ def image(page, size=1)
42
+ @resources[:page][:image].gsub(/\{page\}/, page.to_s).gsub(/\{size\}/,size.to_s)
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,19 @@
1
+ module DocumentCloud
2
+ class SearchResults
3
+ attr_reader :total, :page, :per_page, :query
4
+
5
+ def initialize(attrs)
6
+ @total = attrs[:total]
7
+ @page = attrs[:page]
8
+ @per_page = attrs[:per_page]
9
+ @query = attrs[:q]
10
+
11
+ @documents = attrs[:documents].map {|d| DocumentCloud::Document.new(d) }
12
+ end
13
+
14
+ def documents
15
+ @documents
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module DocumentCloud
2
+ class Version
3
+ MAJOR = 0 unless defined? DocumentCloud::Version::MAJOR
4
+ MINOR = 1 unless defined? DocumentCloud::Version::MINOR
5
+ PATCH = 1 unless defined? DocumentCloud::Version::PATCH
6
+ PRE = nil unless defined? DocumentCloud::Version::PRE
7
+
8
+ class << self
9
+
10
+ # @return [String]
11
+ def to_s
12
+ [MAJOR, MINOR, PATCH, PRE].compact.join('.')
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
19
+
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: documentcloud
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Miles Zimmerman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.6.7
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.6.7
30
+ - !ruby/object:Gem::Dependency
31
+ name: multi_json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.8'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.8'
46
+ description: Rubygem for interacting with the DocumentCloud API
47
+ email:
48
+ - miles@zserver.org
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - LICENSE
54
+ - README.md
55
+ - documentcloud.gemspec
56
+ - lib/document_cloud/api/document.rb
57
+ - lib/document_cloud/api/search.rb
58
+ - lib/document_cloud/api/upload.rb
59
+ - lib/document_cloud/api/utils.rb
60
+ - lib/document_cloud/client.rb
61
+ - lib/document_cloud/configurable.rb
62
+ - lib/document_cloud/default.rb
63
+ - lib/document_cloud/document.rb
64
+ - lib/document_cloud/search_results.rb
65
+ - lib/document_cloud/version.rb
66
+ - lib/document_cloud.rb
67
+ homepage: https://github.com/mileszim/documentcloud
68
+ licenses:
69
+ - MIT
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.24
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Rubygem for interacting with the DocumentCloud API
92
+ test_files: []
93
+ has_rdoc: