rest-scrapyd 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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +60 -0
  4. data/lib/rest-scrapyd.rb +104 -0
  5. metadata +63 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 07145a8352001d446eec7552548b46f13353dbda
4
+ data.tar.gz: dea9c9d7988700c9b95b463a8e9ec7ffa190406d
5
+ SHA512:
6
+ metadata.gz: 4aaa558d3cdb57c41585752b4ae6c0687b4f3f104d79aceb54bbedbcc37aa08bd859af5b6bff8cc764fcc6333d39895091c3e233e408766a9e97aedeaabd3102
7
+ data.tar.gz: 4e6aaef7f108cf06ec7ab9a78aee2c15ed070d34cb29b2d81fd2373bb0e3457ec1ac14c4590d44d5922b892af4b04f74f008671678165a9c1671d5bdef32f8ee
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2015 wvengen
2
+
3
+ MIT License
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
9
+ of the Software, and to permit persons to whom the Software is furnished to do
10
+ 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,60 @@
1
+ rest-scrapyd
2
+ ============
3
+ [![Gem Version](https://badge.fury.io/rb/rest-scrapyd.svg)](http://badge.fury.io/rb/rest-scrapyd)
4
+ [![Documentation](http://b.repl.ca/v1/yard-docs-blue.png)](http://rubydoc.info/github/wvengen/rest-scrapyd)
5
+
6
+ Ruby client for the [Scrapyd](http://scrapyd.readthedocs.org/) REST API
7
+ built on top of [rest-core](https://github.com/godfat/rest-core).
8
+
9
+
10
+ Installation
11
+ ------------
12
+
13
+ ```sh
14
+ gem install rest-scrapyd
15
+ ```
16
+
17
+ or, when using a Gemfile
18
+
19
+ ```ruby
20
+ gem 'rest-scrapyd'
21
+ ```
22
+
23
+ and run `bundle install`.
24
+
25
+
26
+ Usage
27
+ -----
28
+
29
+ ```ruby
30
+ require 'rest-scrapyd'
31
+
32
+ r = RestScrapyd.new site: "http://example.com:6800/"
33
+ r.listprojects
34
+ # => ["project1", "project2"]
35
+ r.listspiders(project: "project1")
36
+ # => ["spider1", "spider2"]
37
+
38
+ # connect to default site at http://localhost:6800/
39
+ r = RestScrapyd.new
40
+ # and set a default project
41
+ r.project = "project1"
42
+ r.listspiders
43
+ # => ["spider1", "spider2"]
44
+ r.listversions
45
+ # => ["123456-master"]
46
+
47
+ # you can also specify a default project on construction
48
+ r = RestScrapyd.new project: "project1"
49
+ r.schedule("spider1", "123456-master")
50
+ # => "1234567890abcdef1234567890abcdef"
51
+ ```
52
+
53
+ For more information, see the [RestScrapyd](http://rubydoc.info/github/wvengen/rest-scrapyd/RestScrapyd.html)
54
+ and [Scrapyd API](http://scrapyd.readthedocs.org/en/latest/api.html) documentation.
55
+
56
+
57
+ Copyright
58
+ ---------
59
+
60
+ Copyright © 2015 wvengen, released under the [MIT license](LICENSE).
@@ -0,0 +1,104 @@
1
+ #
2
+ # Simple REST-client for the Scrapyd API
3
+ #
4
+ require 'rest-core'
5
+
6
+ RestScrapyd = RC::Builder.client do
7
+ use RC::Timeout , 3
8
+
9
+ use RC::DefaultSite , 'http://localhost:6800/'
10
+ use RC::DefaultHeaders, {'Accept' => 'application/json'}
11
+
12
+ use RC::CommonLogger , nil
13
+ use RC::ErrorHandler , lambda {|env| RuntimeError.new(env[RC::RESPONSE_BODY]['message']) }
14
+ use RC::ErrorDetector , lambda {|env| env[RC::RESPONSE_BODY]['status'] != 'ok'}
15
+ use RC::ErrorDetectorHttp
16
+ use RC::JsonResponse , true
17
+ use RC::Cache , nil, 600
18
+ end
19
+
20
+ # Don't define a `RestScrapyd::Client` and include it in the class to avoid yardoc choking
21
+ class RestScrapyd
22
+ include RestCore
23
+
24
+ # @!attribute project
25
+ # Default project name (can also be passed as hash argument to constructor).
26
+ attr_accessor :project
27
+
28
+ # Add a version to a project, creating the project if it doesn’t exist.
29
+ # @param version [String] Project version
30
+ # @param egg [String, IO] Filename or file to upload, must be a Scrapy egg
31
+ # @return [Hash<String, Object>] Response, right now just +{"spiders": <n>}+
32
+ # @see http://scrapyd.readthedocs.org/en/latest/api.html#addversion-json
33
+ def addversion(version, egg, project=self.project)
34
+ egg = File.open(egg, 'rb') if egg.is_a? String
35
+ post('addversion.json', project: project, version: version, egg: egg)
36
+ end
37
+
38
+ # Schedule a spider run (also known as a job).
39
+ # @param spider [String] Spider name
40
+ # @param version [String] Project version
41
+ # @param settings [Hash<String, String>] Additional Scrapy settings
42
+ # @option settings [String] :project ({self.project}) Project name
43
+ # @return [String] Job id
44
+ # @see http://scrapyd.readthedocs.org/en/latest/api.html#schedule-json
45
+ def schedule(spider, version, settings={})
46
+ settings = {project: self.project, spider: spider}.merge(settings)
47
+ post('schedule.json', settings)['jobid']
48
+ end
49
+
50
+ # Cancel a scheduled or running job
51
+ # @param job [String] Jobid to cancel
52
+ # @param project [String] Project name
53
+ # @return [String] Previous state of job
54
+ # @see http://scrapyd.readthedocs.org/en/latest/api.html#cancel-json
55
+ def cancel(job, project=self.project)
56
+ post('cancel.json', project: project, job: job)['prevstate']
57
+ end
58
+
59
+ # Get the list of projects uploaded to this Scrapy server.
60
+ # @return [Array<String>] Available projects
61
+ # @see http://scrapyd.readthedocs.org/en/latest/api.html#listprojects-json
62
+ def listprojects
63
+ get('listprojects.json')['projects']
64
+ end
65
+
66
+ # Get the list of versions available for some project.
67
+ # @param project [String] Project name
68
+ # @return [Array<String>] Available versions for project
69
+ # @see http://scrapyd.readthedocs.org/en/latest/api.html#listversions-json
70
+ def listversions(project=self.project)
71
+ get('listversions.json', project: project)['versions']
72
+ end
73
+
74
+ # Get the list of spiders available in the last version of some project.
75
+ # @param project [String] Project name
76
+ # @return [Array<String>] Available spiders for project
77
+ # @see http://scrapyd.readthedocs.org/en/latest/api.html#listspiders-json
78
+ def listspiders(project=self.project)
79
+ get('listspiders.json', project: project)['spiders']
80
+ end
81
+
82
+ # Get the list of pending, running and finished jobs of some project.
83
+ # @param project [String] Project name
84
+ # @return [Hash<String, Array<Hash>>] Jobs for project
85
+ # @see http://scrapyd.readthedocs.org/en/latest/api.html#listjobs-json
86
+ def listjobs(project=self.project)
87
+ get('listjobs.json', project: project).reject{|k,v| k=='status'}
88
+ end
89
+
90
+ # Delete a project version.
91
+ # @param version [String] Project version
92
+ # @param project [String] Project name
93
+ # @see http://scrapyd.readthedocs.org/en/latest/api.html#delversion-json
94
+ def delversion(version, project=self.project)
95
+ post('delversion.json', project: project, version: version)
96
+ end
97
+
98
+ # Delete a project and all its uploaded versions.
99
+ # @param project [String] Project name
100
+ # @see http://scrapyd.readthedocs.org/en/latest/api.html#delproject-json
101
+ def delproject(project)
102
+ post('delproject.json', project: project)
103
+ end
104
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rest-scrapyd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - wvengen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.3.0
27
+ description: REST client for the Scrapyd API, built with [rest-core](https://github.com/godfat/rest-core).
28
+ email: dev-rails@willem.engen.nl
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files:
32
+ - README.md
33
+ - LICENSE
34
+ files:
35
+ - LICENSE
36
+ - README.md
37
+ - lib/rest-scrapyd.rb
38
+ homepage: https://github.com/wvengen/ruby-rest-scrapyd
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options:
44
+ - "--charset=UTF-8"
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.4.3
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: REST client for the Scrapyd API
63
+ test_files: []