tryphon-pige-client 0.0.2 → 0.0.3
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.
- data/examples/scheduler_chunks_daily.rb +16 -10
- data/lib/pige/client/chunk.rb +3 -49
- data/lib/pige/client/chunk_scheduler.rb +6 -2
- data/lib/pige/client/core_ext.rb +10 -1
- data/lib/pige/client/label.rb +34 -0
- data/lib/pige/client/resource.rb +74 -0
- data/lib/pige/client/version.rb +1 -1
- data/lib/tryphon-pige-client.rb +2 -0
- data/spec/fixtures/labels.json +1 -0
- data/spec/pige/client/chunk_spec.rb +0 -14
- data/spec/pige/client/label_spec.rb +115 -0
- data/spec/support/array_matchers.rb +5 -0
- data/spec/support/fixtures.rb +7 -0
- metadata +14 -4
@@ -6,19 +6,25 @@ Pige::Client.box_url = "http://pigebox.local"
|
|
6
6
|
include Pige::Client
|
7
7
|
|
8
8
|
ChunkScheduler.define do |schedule|
|
9
|
-
# Schedule Chunks for yesterday
|
10
|
-
|
9
|
+
# Schedule Chunks for yesterday by default
|
10
|
+
day_back = ARGV.first ? ARGV.first.to_i : 1
|
11
|
+
schedule.day = Date.today - day_back
|
11
12
|
|
12
13
|
# Doesn't really Chunks, only log orders
|
13
14
|
schedule.dry_run = true
|
14
15
|
|
16
|
+
# Default Attributes
|
17
|
+
# can be overridden in each create
|
18
|
+
schedule.default_attributes[:margin] = 5.minutes
|
19
|
+
#schedule.default_attributes[:format] = "ogg"
|
20
|
+
|
15
21
|
# Daily Chunks
|
16
22
|
schedule.create :title => "Flash 13h %d_%m_%Y", :begin => "13:00", :duration => 15.minutes, :margin => 2.minutes
|
17
23
|
|
18
24
|
# Only monday Chunks
|
19
25
|
schedule.on(:monday) do
|
20
26
|
create :title => "Emission Cinema lundi %d_%m_%Y", :begin => "17:00", :duration => 1.hour
|
21
|
-
create :title => "Chronique 'A Table'", :begin => "13:00", :duration => 5.minutes
|
27
|
+
create :title => "Chronique 'A Table'", :begin => "13:00", :duration => 5.minutes
|
22
28
|
end
|
23
29
|
|
24
30
|
# Only tuesday Chunks
|
@@ -27,25 +33,25 @@ ChunkScheduler.define do |schedule|
|
|
27
33
|
end
|
28
34
|
|
29
35
|
schedule.on(:wednesday) do
|
30
|
-
|
36
|
+
# ...
|
31
37
|
end
|
32
38
|
|
33
39
|
schedule.on(:thursday) do
|
34
|
-
|
40
|
+
# ...
|
35
41
|
end
|
36
42
|
|
37
43
|
schedule.on(:friday) do
|
38
|
-
|
44
|
+
# ...
|
39
45
|
end
|
40
46
|
|
41
47
|
schedule.on(:saturday) do
|
42
|
-
|
48
|
+
# ...
|
43
49
|
end
|
44
50
|
|
45
51
|
schedule.on(:sunday) do
|
46
|
-
|
52
|
+
# ...
|
47
53
|
end
|
48
54
|
end
|
49
55
|
|
50
|
-
|
51
|
-
|
56
|
+
# Remove oldest Chunks
|
57
|
+
#Pige::Client::Chunk.destroy_all(:older_than => 1.days.ago)
|
data/lib/pige/client/chunk.rb
CHANGED
@@ -1,54 +1,8 @@
|
|
1
1
|
module Pige::Client
|
2
|
-
class Chunk
|
3
|
-
include HTTParty
|
2
|
+
class Chunk < Resource
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
attr_accessor :begin, :completion_rate, :created_at, :end, :format, :id, :source_id, :title, :updated_at
|
9
|
-
|
10
|
-
def initialize(attributes = {})
|
11
|
-
attributes.each { |k,v| send "#{k}=", v }
|
12
|
-
end
|
13
|
-
|
14
|
-
def older_than?(date)
|
15
|
-
self.created_at < date
|
16
|
-
end
|
17
|
-
|
18
|
-
def destroy
|
19
|
-
self.class.destroy(self)
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.create(attributes = {})
|
23
|
-
post "#{Pige::Client.box_url}/sources/1/chunks", :query => { :chunk => attributes }
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.all
|
27
|
-
get("#{Pige::Client.box_url}/sources/1/chunks.json").map do |attributes|
|
28
|
-
Chunk.new attributes["chunk"]
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.older_than(date)
|
33
|
-
all.select do |chunk|
|
34
|
-
chunk.older_than?(date)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def self.destroy_all(conditions = {})
|
39
|
-
chunks =
|
40
|
-
if older_than = conditions[:older_than]
|
41
|
-
Chunk.older_than(older_than)
|
42
|
-
else
|
43
|
-
Chunk.all
|
44
|
-
end
|
45
|
-
|
46
|
-
chunks.each(&:destroy)
|
47
|
-
end
|
48
|
-
|
49
|
-
def self.destroy(chunk)
|
50
|
-
delete "#{Pige::Client.box_url}/sources/1/chunks/#{chunk.id}.json"
|
51
|
-
end
|
4
|
+
attr_accessor :begin, :completion_rate, :end, :format, :title
|
5
|
+
time_attribute :begin, :end
|
52
6
|
|
53
7
|
end
|
54
8
|
end
|
@@ -20,7 +20,8 @@ module Pige::Client
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
def create(attributes = {})
|
23
|
+
def create(attributes = {}, options = {})
|
24
|
+
options = { :delay => 1 }.merge(options)
|
24
25
|
attributes = default_attributes.merge(attributes)
|
25
26
|
|
26
27
|
[:begin, :end].each do |endpoint|
|
@@ -45,7 +46,10 @@ module Pige::Client
|
|
45
46
|
end
|
46
47
|
|
47
48
|
puts "Create Chunk with #{attributes.inspect}"
|
48
|
-
|
49
|
+
unless dry_run?
|
50
|
+
Pige::Client::Chunk.create attributes
|
51
|
+
sleep options[:delay].to_i if options[:delay]
|
52
|
+
end
|
49
53
|
end
|
50
54
|
|
51
55
|
end
|
data/lib/pige/client/core_ext.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
class Numeric
|
2
2
|
|
3
3
|
def hours
|
4
|
-
self *
|
4
|
+
self.minutes * 60
|
5
5
|
end
|
6
6
|
alias_method :hour, :hours
|
7
7
|
|
@@ -10,4 +10,13 @@ class Numeric
|
|
10
10
|
end
|
11
11
|
alias_method :minute, :minutes
|
12
12
|
|
13
|
+
def days
|
14
|
+
self.hours * 24
|
15
|
+
end
|
16
|
+
alias_method :day, :days
|
17
|
+
|
18
|
+
def ago
|
19
|
+
Time.now - self
|
20
|
+
end
|
21
|
+
|
13
22
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Pige::Client
|
2
|
+
class Label < Resource
|
3
|
+
attr_accessor :timestamp, :name
|
4
|
+
time_attribute :timestamp
|
5
|
+
|
6
|
+
def older_than?(date)
|
7
|
+
self.timestamp < date
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.all
|
11
|
+
[].tap do |labels|
|
12
|
+
page = 1
|
13
|
+
empty_page = false
|
14
|
+
|
15
|
+
begin
|
16
|
+
labels_in_page = page(page)
|
17
|
+
unless labels_in_page.empty?
|
18
|
+
labels.push *labels_in_page
|
19
|
+
page += 1
|
20
|
+
else
|
21
|
+
empty_page = true
|
22
|
+
end
|
23
|
+
end until empty_page
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.page(number)
|
28
|
+
get("#{Pige::Client.box_url}/sources/1/labels.json?page=#{number}").map do |attributes|
|
29
|
+
Label.new attributes["label"]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Pige::Client
|
2
|
+
class Resource
|
3
|
+
include HTTParty
|
4
|
+
|
5
|
+
# debug_output $stderr
|
6
|
+
format :json
|
7
|
+
|
8
|
+
def initialize(attributes = {})
|
9
|
+
attributes.each { |k,v| send "#{k}=", v }
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.time_attribute(*names)
|
13
|
+
names.each do |time_attribute|
|
14
|
+
define_method("#{time_attribute}=") do |value|
|
15
|
+
value = Time.parse(value) if String === value
|
16
|
+
instance_variable_set "@#{time_attribute}", value
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_accessor :created_at, :updated_at
|
22
|
+
time_attribute :created_at, :updated_at
|
23
|
+
|
24
|
+
attr_accessor :id, :source_id
|
25
|
+
|
26
|
+
def older_than?(date)
|
27
|
+
self.created_at < date
|
28
|
+
end
|
29
|
+
|
30
|
+
def destroy
|
31
|
+
self.class.destroy(self)
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.resource_name
|
35
|
+
name.downcase.split("::").last
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.base_url
|
39
|
+
"#{Pige::Client.box_url}/sources/1/#{resource_name}s"
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.create(attributes = {})
|
43
|
+
post "#{base_url}.json", :query => { resource_name => attributes }
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.all
|
47
|
+
get("#{Pige::Client.box_url}/sources/1/chunks.json").map do |attributes|
|
48
|
+
Chunk.new attributes["chunk"]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.older_than(date)
|
53
|
+
all.select do |resource|
|
54
|
+
resource.older_than?(date)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.destroy_all(conditions = {})
|
59
|
+
resources =
|
60
|
+
if older_than = conditions[:older_than]
|
61
|
+
older_than(older_than)
|
62
|
+
else
|
63
|
+
all
|
64
|
+
end
|
65
|
+
|
66
|
+
resources.each(&:destroy)
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.destroy(resource)
|
70
|
+
delete "#{base_url}/#{resource.id}.json"
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
data/lib/pige/client/version.rb
CHANGED
data/lib/tryphon-pige-client.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
[{"label":{"updated_at":"2011-05-26T12:14:06+02:00","source_id":1,"id":51,"name":"Test","timestamp":"2011-05-26T12:14:02+02:00","created_at":"2011-05-26T12:14:06+02:00"}},{"label":{"updated_at":"2011-05-26T11:55:25+02:00","source_id":1,"id":50,"name":"Test","timestamp":"2011-05-26T11:55:21+02:00","created_at":"2011-05-26T11:55:25+02:00"}},{"label":{"updated_at":"2011-03-04T22:16:10+01:00","source_id":1,"id":49,"name":"test","timestamp":"2011-03-04T22:16:10+01:00","created_at":"2011-03-04T22:16:10+01:00"}},{"label":{"updated_at":"2011-03-04T22:14:41+01:00","source_id":1,"id":48,"name":"test","timestamp":"2011-03-04T22:14:41+01:00","created_at":"2011-03-04T22:14:41+01:00"}},{"label":{"updated_at":"2011-03-04T22:13:33+01:00","source_id":1,"id":47,"name":"test","timestamp":"2011-03-04T22:13:33+01:00","created_at":"2011-03-04T22:13:33+01:00"}},{"label":{"updated_at":"2011-03-04T22:12:45+01:00","source_id":1,"id":46,"name":"test","timestamp":"2011-03-04T22:12:45+01:00","created_at":"2011-03-04T22:12:45+01:00"}},{"label":{"updated_at":"2011-03-04T22:09:37+01:00","source_id":1,"id":45,"name":"test","timestamp":"2011-03-04T22:09:37+01:00","created_at":"2011-03-04T22:09:37+01:00"}},{"label":{"updated_at":"2011-03-04T22:09:34+01:00","source_id":1,"id":44,"name":"test","timestamp":"2011-03-04T22:09:34+01:00","created_at":"2011-03-04T22:09:34+01:00"}},{"label":{"updated_at":"2011-03-04T22:09:32+01:00","source_id":1,"id":43,"name":"test","timestamp":"2011-03-04T22:09:32+01:00","created_at":"2011-03-04T22:09:32+01:00"}},{"label":{"updated_at":"2011-03-04T21:11:29+01:00","source_id":1,"id":42,"name":"test","timestamp":"2011-03-04T21:11:29+01:00","created_at":"2011-03-04T21:11:29+01:00"}}]
|
@@ -2,26 +2,12 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Chunk do
|
4
4
|
|
5
|
-
def fixture_file(file)
|
6
|
-
File.expand_path("../../../fixtures/#{file}", __FILE__)
|
7
|
-
end
|
8
|
-
|
9
|
-
def fixture_content(file)
|
10
|
-
File.read fixture_file(file)
|
11
|
-
end
|
12
|
-
|
13
5
|
describe ".all" do
|
14
6
|
|
15
7
|
before do
|
16
8
|
FakeWeb.register_uri(:get, "http://pigebox.local/sources/1/chunks.json", :body => fixture_content("chunks.json"))
|
17
9
|
end
|
18
10
|
|
19
|
-
RSpec::Matchers.define :be_an_array_of do |expected|
|
20
|
-
match do |actual|
|
21
|
-
Array === actual and actual.all? { |e| expected === e }
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
11
|
it "should return a array of Chunks" do
|
26
12
|
Chunk.all.should be_an_array_of(Chunk)
|
27
13
|
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Label do
|
4
|
+
|
5
|
+
describe ".page" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
FakeWeb.register_uri(:get, "http://pigebox.local/sources/1/labels.json?page=2", :body => fixture_content("labels.json"))
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return a array of Labels" do
|
12
|
+
Label.page(2).should be_an_array_of(Label)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should create Labels with received data" do
|
16
|
+
Label.page(2).first.name.should == "Test"
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
describe ".all" do
|
22
|
+
|
23
|
+
let(:label) { mock }
|
24
|
+
|
25
|
+
it "should concat pages until an empty one" do
|
26
|
+
Label.should_receive(:page).with(1).and_return([label])
|
27
|
+
Label.should_receive(:page).with(2).and_return([label])
|
28
|
+
Label.should_receive(:page).with(3).and_return([])
|
29
|
+
|
30
|
+
Label.all.should == [label, label]
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
describe ".destroy_all" do
|
36
|
+
|
37
|
+
let(:label) { mock }
|
38
|
+
|
39
|
+
context "without conditions" do
|
40
|
+
|
41
|
+
it "should destroy all Labels returned by Label.all" do
|
42
|
+
Label.stub :all => [label]
|
43
|
+
label.should_receive(:destroy)
|
44
|
+
Label.destroy_all
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
context "with :older_than condition" do
|
50
|
+
|
51
|
+
let(:date) { Time.now }
|
52
|
+
|
53
|
+
it "should destroy all Labels returned by Label.older_than" do
|
54
|
+
Label.should_receive(:older_than).with(date).and_return([label])
|
55
|
+
label.should_receive(:destroy)
|
56
|
+
Label.destroy_all :older_than => date
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "#older_than" do
|
64
|
+
|
65
|
+
let(:label) { Label.new }
|
66
|
+
let(:date) { Time.now }
|
67
|
+
|
68
|
+
it "should be true when the Label timestamp is before the given date" do
|
69
|
+
label.timestamp = date - 1
|
70
|
+
label.should be_older_than(date)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should be false when the Label timestamp is after the given date" do
|
74
|
+
label.timestamp = date + 1
|
75
|
+
label.should_not be_older_than(date)
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
describe ".older_than" do
|
81
|
+
|
82
|
+
let(:label) { mock }
|
83
|
+
let(:date) { Time.now }
|
84
|
+
|
85
|
+
before do
|
86
|
+
Label.stub :all => [label]
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should select Labels older than specified date" do
|
90
|
+
label.should_receive(:older_than?).with(date).and_return(true)
|
91
|
+
Label.older_than(date).should include(label)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should ignore Labels not older than specified date" do
|
95
|
+
label.should_receive(:older_than?).with(date).and_return(false)
|
96
|
+
Label.older_than(date).should_not include(label)
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "#destroy" do
|
102
|
+
|
103
|
+
let(:label) { Label.new :id => 1 }
|
104
|
+
|
105
|
+
it "should send a DELETE request with Label id" do
|
106
|
+
FakeWeb.register_uri :delete, "http://pigebox.local/sources/1/labels/#{label.id}.json", {}
|
107
|
+
label.destroy
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tryphon-pige-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-12-
|
13
|
+
date: 2012-12-08 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: fakeweb
|
@@ -144,12 +144,18 @@ files:
|
|
144
144
|
- lib/pige/client/chunk.rb
|
145
145
|
- lib/pige/client/chunk_scheduler.rb
|
146
146
|
- lib/pige/client/core_ext.rb
|
147
|
+
- lib/pige/client/label.rb
|
148
|
+
- lib/pige/client/resource.rb
|
147
149
|
- lib/pige/client/version.rb
|
148
150
|
- lib/tryphon-pige-client.rb
|
149
151
|
- spec/fixtures/chunks.json
|
152
|
+
- spec/fixtures/labels.json
|
150
153
|
- spec/pige/client/chunk_spec.rb
|
154
|
+
- spec/pige/client/label_spec.rb
|
151
155
|
- spec/spec_helper.rb
|
156
|
+
- spec/support/array_matchers.rb
|
152
157
|
- spec/support/fakeweb.rb
|
158
|
+
- spec/support/fixtures.rb
|
153
159
|
- tasks/rdoc.rake
|
154
160
|
- tasks/rspec.rake
|
155
161
|
- tryphon-pige-client.gemspec
|
@@ -167,7 +173,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
167
173
|
version: '0'
|
168
174
|
segments:
|
169
175
|
- 0
|
170
|
-
hash:
|
176
|
+
hash: 3105553272469934144
|
171
177
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
172
178
|
none: false
|
173
179
|
requirements:
|
@@ -176,7 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
176
182
|
version: '0'
|
177
183
|
segments:
|
178
184
|
- 0
|
179
|
-
hash:
|
185
|
+
hash: 3105553272469934144
|
180
186
|
requirements: []
|
181
187
|
rubyforge_project:
|
182
188
|
rubygems_version: 1.8.23
|
@@ -185,6 +191,10 @@ specification_version: 3
|
|
185
191
|
summary: Tryphon PigeBox API ruby client
|
186
192
|
test_files:
|
187
193
|
- spec/fixtures/chunks.json
|
194
|
+
- spec/fixtures/labels.json
|
188
195
|
- spec/pige/client/chunk_spec.rb
|
196
|
+
- spec/pige/client/label_spec.rb
|
189
197
|
- spec/spec_helper.rb
|
198
|
+
- spec/support/array_matchers.rb
|
190
199
|
- spec/support/fakeweb.rb
|
200
|
+
- spec/support/fixtures.rb
|