tryphon-pige-client 0.0.3 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,18 +1,19 @@
1
1
  *.gem
2
2
  *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
3
+ /.bundle
4
+ /.config
5
+ /.yardoc
6
+ /Gemfile.lock
7
+ /InstalledFiles
8
+ /_yardoc
9
+ /coverage
10
+ /doc/
11
+ /lib/bundler/man
12
+ /pkg
13
+ /rdoc
14
+ /spec/reports
15
+ /test/tmp
16
+ /test/version_tmp
17
+ /tmp
18
18
  *~
19
+ /.ruby-version
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'tryphon-pige-client'
4
4
 
5
- Pige::Client.box_url = "http://pigebox.local"
5
+ Pige::Client.box_url = "http://#{ENV.fetch('PIGEBOX_HOST', 'pigebox.local')}"
6
6
  include Pige::Client
7
7
 
8
8
  ChunkScheduler.define do |schedule|
@@ -10,16 +10,16 @@ ChunkScheduler.define do |schedule|
10
10
  day_back = ARGV.first ? ARGV.first.to_i : 1
11
11
  schedule.day = Date.today - day_back
12
12
 
13
- # Doesn't really Chunks, only log orders
14
- schedule.dry_run = true
13
+ # Doesn't really Chunks, only log orders
14
+ schedule.dry_run = ENV.fetch('DRY_RUN',"true") != "false"
15
15
 
16
16
  # Default Attributes
17
17
  # can be overridden in each create
18
18
  schedule.default_attributes[:margin] = 5.minutes
19
- #schedule.default_attributes[:format] = "ogg"
19
+ # schedule.default_attributes[:format] = "vorbis"
20
20
 
21
21
  # Daily Chunks
22
- schedule.create :title => "Flash 13h %d_%m_%Y", :begin => "13:00", :duration => 15.minutes, :margin => 2.minutes
22
+ schedule.create :title => "Flash 13h %d_%m_%Y", :begin => "13:00", :begin_label => "Generique Flash", :duration => 15.minutes, :end_label => "Fin Direct", :margin => 0
23
23
 
24
24
  # Only monday Chunks
25
25
  schedule.on(:monday) do
@@ -51,7 +51,7 @@ ChunkScheduler.define do |schedule|
51
51
  schedule.on(:sunday) do
52
52
  # ...
53
53
  end
54
- end
54
+ end.download("/tmp")
55
55
 
56
56
  # Remove oldest Chunks
57
57
  #Pige::Client::Chunk.destroy_all(:older_than => 1.days.ago)
@@ -4,5 +4,28 @@ module Pige::Client
4
4
  attr_accessor :begin, :completion_rate, :end, :format, :title
5
5
  time_attribute :begin, :end
6
6
 
7
+ def completion_rate
8
+ self.class.get("#{Pige::Client.box_url}/sources/1/chunks/#{id}.json")["completion_rate"].to_f
9
+ end
10
+
11
+ def extension
12
+ format == "vorbis" ? "ogg" : format
13
+ end
14
+
15
+ def download_url
16
+ "#{Pige::Client.box_url}/sources/1/chunks/#{id}.#{extension}"
17
+ end
18
+
19
+ def download_file
20
+ "#{title}.#{extension}"
21
+ end
22
+
23
+ def download(target)
24
+ File.open(File.join(target, download_file), "w") do |file|
25
+ open(download_url) do |url|
26
+ IO.copy_stream url, file
27
+ end
28
+ end
29
+ end
7
30
  end
8
31
  end
@@ -4,13 +4,50 @@ module Pige::Client
4
4
  attr_accessor :day, :default_attributes, :dry_run
5
5
  alias_method :dry_run?, :dry_run
6
6
 
7
+ def chunks
8
+ @chunks ||= []
9
+ end
10
+
11
+ def wait(timeout = nil)
12
+ timeout ||= chunks.size * 5
13
+ print "Wait for new chunks to be ready : "
14
+ deadline = Time.now + (timeout * 60)
15
+
16
+ uncompleted_chunks = chunks.dup
17
+
18
+ while Time.now < deadline
19
+ uncompleted_chunks.delete_if do |chunk|
20
+ chunk.completion_rate == 1
21
+ end
22
+ if uncompleted_chunks.empty?
23
+ puts "done\nChunks are ready"
24
+ break
25
+ else
26
+ print "."
27
+ sleep 10
28
+ end
29
+ end
30
+ end
31
+
32
+ def download(target)
33
+ wait
34
+
35
+ FileUtils.mkdir_p target
36
+ chunks.each do |chunk|
37
+ puts "Download '#{chunk.title}' to #{target}/#{chunk.download_file} ..."
38
+ chunk.download target
39
+ end
40
+ end
41
+
7
42
  def initialize(day = Date.today)
8
43
  self.day = day
9
44
  self.default_attributes = { :format => "wav" }
10
45
  end
11
46
 
12
47
  def self.define(&block)
13
- yield new
48
+ new.tap do |scheduler|
49
+ yield scheduler
50
+ end
14
51
  end
15
52
 
16
53
  def on(criteria, &block)
@@ -20,8 +57,7 @@ module Pige::Client
20
57
  end
21
58
  end
22
59
 
23
- def create(attributes = {}, options = {})
24
- options = { :delay => 1 }.merge(options)
60
+ def chunk_attributes(attributes = {})
25
61
  attributes = default_attributes.merge(attributes)
26
62
 
27
63
  [:begin, :end].each do |endpoint|
@@ -36,6 +72,26 @@ module Pige::Client
36
72
  end
37
73
  end
38
74
 
75
+ if name = attributes[:begin_label]
76
+ label = Pige::Client::Label.by_name(name, after: attributes[:begin])
77
+ if label
78
+ puts "Found label '#{name}' at #{label.timestamp}"
79
+ attributes[:begin] = label.timestamp
80
+ else
81
+ puts "Can't find' label '#{name}'"
82
+ end
83
+ end
84
+
85
+ if name = attributes[:end_label]
86
+ label = Pige::Client::Label.by_name(name, before: attributes[:end])
87
+ if label
88
+ puts "Found label '#{name}' at #{label.timestamp}"
89
+ attributes[:end] = label.timestamp
90
+ else
91
+ puts "Can't find' label '#{name}'"
92
+ end
93
+ end
94
+
39
95
  if margin = attributes.delete(:margin)
40
96
  attributes[:begin] -= margin
41
97
  attributes[:end] += margin
@@ -45,9 +101,17 @@ module Pige::Client
45
101
  attributes[:title] = day.strftime(attributes[:title])
46
102
  end
47
103
 
104
+ attributes
105
+ end
106
+
107
+ def create(attributes = {}, options = {})
108
+ options = { :delay => 1 }.merge(options)
109
+
110
+ attributes = chunk_attributes(attributes)
48
111
  puts "Create Chunk with #{attributes.inspect}"
112
+
49
113
  unless dry_run?
50
- Pige::Client::Chunk.create attributes
114
+ chunks << Pige::Client::Chunk.create(attributes)
51
115
  sleep options[:delay].to_i if options[:delay]
52
116
  end
53
117
  end
@@ -7,25 +7,40 @@ module Pige::Client
7
7
  self.timestamp < date
8
8
  end
9
9
 
10
- def self.all
10
+ def self.all(options = {})
11
11
  [].tap do |labels|
12
12
  page = 1
13
13
  empty_page = false
14
14
 
15
15
  begin
16
- labels_in_page = page(page)
16
+ labels_in_page = page(page, options)
17
17
  unless labels_in_page.empty?
18
18
  labels.push *labels_in_page
19
19
  page += 1
20
20
  else
21
21
  empty_page = true
22
22
  end
23
- end until empty_page
23
+ end until empty_page
24
24
  end
25
25
  end
26
26
 
27
- def self.page(number)
28
- get("#{Pige::Client.box_url}/sources/1/labels.json?page=#{number}").map do |attributes|
27
+ def self.by_name(name, options = {})
28
+ options = options.merge(term: name)
29
+ attributes = get("#{Pige::Client.box_url}/sources/1/labels.json", query: options).first
30
+ Label.new attributes if attributes
31
+ end
32
+
33
+ def self.older_than(date)
34
+ all(before: date)
35
+ end
36
+
37
+ def self.page(number, options = {})
38
+ additionnal_query = options.map do |k,v|
39
+ "#{k}=#{URI.escape(v.to_s)}" if v
40
+ end.join('&')
41
+ additionnal_query = "&#{additionnal_query}" unless additionnal_query.empty?
42
+
43
+ get("#{Pige::Client.box_url}/sources/1/labels.json?page=#{number}#{additionnal_query}").map do |attributes|
29
44
  Label.new attributes["label"]
30
45
  end
31
46
  end
@@ -2,7 +2,7 @@ module Pige::Client
2
2
  class Resource
3
3
  include HTTParty
4
4
 
5
- # debug_output $stderr
5
+ debug_output $stderr
6
6
  format :json
7
7
 
8
8
  def initialize(attributes = {})
@@ -21,6 +21,11 @@ module Pige::Client
21
21
  attr_accessor :created_at, :updated_at
22
22
  time_attribute :created_at, :updated_at
23
23
 
24
+ attr_accessor :errors
25
+ def errors
26
+ @errors ||= []
27
+ end
28
+
24
29
  attr_accessor :id, :source_id
25
30
 
26
31
  def older_than?(date)
@@ -35,17 +40,22 @@ module Pige::Client
35
40
  name.downcase.split("::").last
36
41
  end
37
42
 
43
+ def self.resources_name
44
+ "#{resource_name}s"
45
+ end
46
+
38
47
  def self.base_url
39
48
  "#{Pige::Client.box_url}/sources/1/#{resource_name}s"
40
49
  end
41
50
 
42
51
  def self.create(attributes = {})
43
- post "#{base_url}.json", :query => { resource_name => attributes }
52
+ response = post("#{base_url}.json", :query => { resource_name => attributes })
53
+ new response
44
54
  end
45
55
 
46
56
  def self.all
47
- get("#{Pige::Client.box_url}/sources/1/chunks.json").map do |attributes|
48
- Chunk.new attributes["chunk"]
57
+ get("#{Pige::Client.box_url}/sources/1/#{resources_name}.json").map do |attributes|
58
+ new attributes[resource_name]
49
59
  end
50
60
  end
51
61
 
@@ -56,7 +66,7 @@ module Pige::Client
56
66
  end
57
67
 
58
68
  def self.destroy_all(conditions = {})
59
- resources =
69
+ resources =
60
70
  if older_than = conditions[:older_than]
61
71
  older_than(older_than)
62
72
  else
@@ -1,6 +1,5 @@
1
1
  module Pige
2
2
  module Client
3
- VERSION = "0.0.3"
3
+ VERSION = "1.0"
4
4
  end
5
5
  end
6
-
@@ -1,4 +1,5 @@
1
1
  require 'httparty'
2
+ require 'open-uri'
2
3
 
3
4
  require "pige/client"
4
5
  require "pige/client/version"
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChunkScheduler do
4
+
5
+ describe "#day" do
6
+
7
+ it "should use today by default" do
8
+ subject.day.should == Date.today
9
+ end
10
+
11
+ end
12
+
13
+ describe "#dry_run" do
14
+
15
+ it "should be false by default" do
16
+ subject.dry_run.should be_false
17
+ end
18
+
19
+ end
20
+
21
+ describe "#default_attributes" do
22
+
23
+ it "should define format to wav" do
24
+ subject.default_attributes[:format].should == "wav"
25
+ end
26
+
27
+ end
28
+
29
+ describe "#on" do
30
+
31
+ it "should yield block if day matchs criteria" do
32
+ subject.day.stub :dummy? => true
33
+ has_been_yield = false
34
+ subject.on(:dummy) { has_been_yield = true }
35
+ end
36
+
37
+ end
38
+
39
+ describe "#chunk_attributes" do
40
+
41
+ context "when begin_label is specified" do
42
+
43
+ let(:time) { Time.now }
44
+ let(:label) { double(timestamp: Time.now) }
45
+
46
+ it "should use label timestamp if found" do
47
+ Pige::Client::Label.should_receive(:by_name).with("dummy", after: nil).and_return(label)
48
+
49
+ attributes = subject.chunk_attributes begin_label: "dummy"
50
+ attributes[:begin].should == label.timestamp
51
+ end
52
+
53
+ it "should search label after begin time" do
54
+ Pige::Client::Label.should_receive(:by_name).with("dummy", after: time).and_return(label)
55
+
56
+ attributes = subject.chunk_attributes begin_label: "dummy", begin: time
57
+ attributes[:begin].should == label.timestamp
58
+ end
59
+
60
+ it "should use begin time if label is not found" do
61
+ Pige::Client::Label.stub by_name: nil
62
+
63
+ attributes = subject.chunk_attributes begin: time
64
+ attributes[:begin].should == time
65
+ end
66
+
67
+ end
68
+
69
+ context "when end_label is specified" do
70
+
71
+ let(:time) { Time.now }
72
+ let(:label) { double(timestamp: Time.now) }
73
+
74
+ it "should use label timestamp if found" do
75
+ Pige::Client::Label.should_receive(:by_name).with("dummy", before: nil).and_return(label)
76
+
77
+ attributes = subject.chunk_attributes end_label: "dummy"
78
+ attributes[:end].should == label.timestamp
79
+ end
80
+
81
+ it "should search label before end time" do
82
+ Pige::Client::Label.should_receive(:by_name).with("dummy", before: time).and_return(label)
83
+
84
+ attributes = subject.chunk_attributes end_label: "dummy", end: time
85
+ attributes[:end].should == label.timestamp
86
+ end
87
+
88
+ it "should use end time if label is not found" do
89
+ Pige::Client::Label.stub by_name: nil
90
+
91
+ attributes = subject.chunk_attributes end: time
92
+ attributes[:end].should == time
93
+ end
94
+
95
+ end
96
+
97
+ end
98
+
99
+ end
@@ -2,6 +2,14 @@ require 'spec_helper'
2
2
 
3
3
  describe Chunk do
4
4
 
5
+ describe ".resources_name" do
6
+
7
+ it "should be chunks" do
8
+ Chunk.resources_name.should == "chunks"
9
+ end
10
+
11
+ end
12
+
5
13
  describe ".all" do
6
14
 
7
15
  before do
@@ -15,13 +23,13 @@ describe Chunk do
15
23
  it "should create Chunks with received data" do
16
24
  Chunk.all.first.title.should == "Emission Cinema lundi 03_12_2012"
17
25
  end
18
-
26
+
19
27
  end
20
28
 
21
29
  describe ".destroy_all" do
22
30
 
23
31
  let(:chunk) { mock }
24
-
32
+
25
33
  context "without conditions" do
26
34
 
27
35
  it "should destroy all Chunks returned by Chunk.all" do
@@ -41,7 +49,7 @@ describe Chunk do
41
49
  chunk.should_receive(:destroy)
42
50
  Chunk.destroy_all :older_than => date
43
51
  end
44
-
52
+
45
53
  end
46
54
 
47
55
  end
@@ -60,7 +68,7 @@ describe Chunk do
60
68
  chunk.created_at = date + 1
61
69
  chunk.should_not be_older_than(date)
62
70
  end
63
-
71
+
64
72
  end
65
73
 
66
74
  describe ".older_than" do
@@ -71,7 +79,7 @@ describe Chunk do
71
79
  before do
72
80
  Chunk.stub :all => [chunk]
73
81
  end
74
-
82
+
75
83
  it "should select Chunks older than specified date" do
76
84
  chunk.should_receive(:older_than?).with(date).and_return(true)
77
85
  Chunk.older_than(date).should include(chunk)
@@ -94,8 +102,5 @@ describe Chunk do
94
102
  end
95
103
 
96
104
  end
97
-
98
- end
99
-
100
-
101
105
 
106
+ end
@@ -15,7 +15,7 @@ describe Label do
15
15
  it "should create Labels with received data" do
16
16
  Label.page(2).first.name.should == "Test"
17
17
  end
18
-
18
+
19
19
  end
20
20
 
21
21
  describe ".all" do
@@ -23,9 +23,9 @@ describe Label do
23
23
  let(:label) { mock }
24
24
 
25
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([])
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
29
 
30
30
  Label.all.should == [label, label]
31
31
  end
@@ -35,7 +35,7 @@ describe Label do
35
35
  describe ".destroy_all" do
36
36
 
37
37
  let(:label) { mock }
38
-
38
+
39
39
  context "without conditions" do
40
40
 
41
41
  it "should destroy all Labels returned by Label.all" do
@@ -55,7 +55,7 @@ describe Label do
55
55
  label.should_receive(:destroy)
56
56
  Label.destroy_all :older_than => date
57
57
  end
58
-
58
+
59
59
  end
60
60
 
61
61
  end
@@ -74,26 +74,17 @@ describe Label do
74
74
  label.timestamp = date + 1
75
75
  label.should_not be_older_than(date)
76
76
  end
77
-
77
+
78
78
  end
79
79
 
80
80
  describe ".older_than" do
81
81
 
82
- let(:label) { mock }
83
82
  let(:date) { Time.now }
84
83
 
85
- before do
86
- Label.stub :all => [label]
87
- end
88
-
89
84
  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)
85
+ FakeWeb.register_uri(:get, "http://pigebox.local/sources/1/labels.json?page=1&before=#{URI.escape(date.to_s)}", :body => fixture_content("labels.json"))
86
+ FakeWeb.register_uri(:get, "http://pigebox.local/sources/1/labels.json?page=2&before=#{URI.escape(date.to_s)}", :body => [].to_json)
87
+ Label.older_than(date).first.name.should == "Test"
97
88
  end
98
89
 
99
90
  end
@@ -108,8 +99,5 @@ describe Label do
108
99
  end
109
100
 
110
101
  end
111
-
112
- end
113
-
114
-
115
102
 
103
+ end
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.3
4
+ version: '1.0'
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-08 00:00:00.000000000 Z
13
+ date: 2015-01-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: fakeweb
@@ -150,6 +150,7 @@ files:
150
150
  - lib/tryphon-pige-client.rb
151
151
  - spec/fixtures/chunks.json
152
152
  - spec/fixtures/labels.json
153
+ - spec/pige/client/chunk_scheduler_spec.rb
153
154
  - spec/pige/client/chunk_spec.rb
154
155
  - spec/pige/client/label_spec.rb
155
156
  - spec/spec_helper.rb
@@ -173,7 +174,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
173
174
  version: '0'
174
175
  segments:
175
176
  - 0
176
- hash: 3105553272469934144
177
+ hash: -3329681472246691501
177
178
  required_rubygems_version: !ruby/object:Gem::Requirement
178
179
  none: false
179
180
  requirements:
@@ -182,7 +183,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
182
183
  version: '0'
183
184
  segments:
184
185
  - 0
185
- hash: 3105553272469934144
186
+ hash: -3329681472246691501
186
187
  requirements: []
187
188
  rubyforge_project:
188
189
  rubygems_version: 1.8.23
@@ -192,6 +193,7 @@ summary: Tryphon PigeBox API ruby client
192
193
  test_files:
193
194
  - spec/fixtures/chunks.json
194
195
  - spec/fixtures/labels.json
196
+ - spec/pige/client/chunk_scheduler_spec.rb
195
197
  - spec/pige/client/chunk_spec.rb
196
198
  - spec/pige/client/label_spec.rb
197
199
  - spec/spec_helper.rb