heywatch 1.0.6 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/heywatch +16 -1
  2. data/lib/heywatch.rb +66 -46
  3. metadata +7 -14
data/bin/heywatch CHANGED
@@ -72,6 +72,7 @@ begin
72
72
  download # manage download | all, info, delete, count, create
73
73
  job # manage job | all, info, delete, count, create
74
74
  format # manage format | all, info, delete, count, create, update
75
+ upload # upload a video
75
76
 
76
77
  Usage:
77
78
 
@@ -85,6 +86,7 @@ begin
85
86
  heywatch video:all "[0]"
86
87
  heywatch job:all "[0..10]"
87
88
  heywatch format:count owner=true
89
+ heywatch upload path/to/video.mp4 "title=my title"
88
90
  )
89
91
  exit
90
92
  end
@@ -95,11 +97,24 @@ begin
95
97
  end
96
98
 
97
99
  resource, method = ARGV[0].split(":")
98
- method ||= "all"
99
100
  params = ARGV[1..-1]
101
+ if method.nil?
102
+ if params.size == 1 && params[0].to_i > 0
103
+ method = "info"
104
+ else
105
+ method = "all"
106
+ end
107
+ end
100
108
 
101
109
  $stderr.puts [resource, method, params].inspect if ENV["DEBUG"]
102
110
 
111
+ if resource == "upload"
112
+ path = ARGV[1]
113
+ params = Hash[*ARGV[2..-1].map{|p| k,v = p.split("=") }.flatten]
114
+ puts JSON.pretty_generate(hw.upload(path, params))
115
+ exit
116
+ end
117
+
103
118
  if method == "create"
104
119
  params = Hash[*ARGV[1..-1].map{|p| k,v = p.split("=") }.flatten]
105
120
 
data/lib/heywatch.rb CHANGED
@@ -6,9 +6,9 @@ class HeyWatch
6
6
  class BadRequest < RuntimeError; end
7
7
 
8
8
  URL = ENV["HEYWATCH_URL"] || "https://heywatch.com"
9
- VERSION = "1.0.6"
9
+ VERSION = "1.1.0"
10
10
 
11
- attr_reader :cli
11
+ attr_reader :credits_left
12
12
 
13
13
  # Authenticate with your HeyWatch credentials
14
14
  #
@@ -16,27 +16,24 @@ class HeyWatch
16
16
  def initialize(user, password)
17
17
  @cli = RestClient::Resource.new(URL, {:user => user, :password => password, :headers =>
18
18
  {:user_agent => "HeyWatch Ruby/#{VERSION}", :accept => "application/json"}})
19
-
19
+
20
+ self.account
20
21
  self
21
22
  end
22
23
 
23
24
  def self.register(data={})
24
- cli = RestClient::Resource.new(URL, {:headers =>
25
- {:user_agent => "HeyWatch Ruby/#{VERSION}", :accept => "application/json"}})
26
- JSON.parse(cli["/account"].post(data))
27
- rescue RestClient::BadRequest=> e
28
- raise BadRequest, e.http_body
25
+ HeyWatch.new(nil, nil).req("/account", :post, data)
29
26
  end
30
27
 
31
28
  def inspect # :nodoc:
32
- "#<HeyWatch: " + account.inspect + ">"
29
+ "#<HeyWatch: " + @me.inspect + ">"
33
30
  end
34
31
 
35
32
  # Get account information
36
33
  #
37
34
  # hw.account
38
35
  def account
39
- JSON.parse(@cli["/account"].get)
36
+ @me = req("/account")
40
37
  end
41
38
 
42
39
  # Get all from a given resource.
@@ -46,18 +43,16 @@ class HeyWatch
46
43
  # hw.all :format, :owner => true
47
44
  def all(*resource_and_filters)
48
45
  resource, filters = resource_and_filters
49
-
50
- result = JSON.parse(@cli["/#{resource}"].get)
51
- return result if filters.nil? or filters.empty?
52
-
53
- return filter_all(result, filters)
46
+ results = req("/#{resource}")
47
+ return results if filters.nil? || filters.empty?
48
+ return filter_all(results, filters)
54
49
  end
55
50
 
56
51
  # Get info about a given resource and id
57
52
  #
58
53
  # hw.info :format, 31
59
54
  def info(resource, id)
60
- JSON.parse(@cli["/#{resource}/#{id}"].get)
55
+ req("/#{resource}/#{id}")
61
56
  end
62
57
 
63
58
  # Count objects from a given resources.
@@ -77,7 +72,7 @@ class HeyWatch
77
72
  raise InvalidResource, "Can't retrieve '#{resource}'"
78
73
  end
79
74
 
80
- @cli["/#{resource}/#{id}.bin"].head do |res, req|
75
+ req("/#{resource}/#{id}.bin", :head) do |res, req|
81
76
  return RestClient.get(res.headers[:location], :raw_response => true, &block)
82
77
  end
83
78
  end
@@ -90,26 +85,21 @@ class HeyWatch
90
85
  # hw.jpg 12345, :async => true, :number => 6, :s3_directive => "s3://accesskey:secretkey@bucket"
91
86
  # => true
92
87
  def jpg(id, params={})
93
- if params.delete(:async) or params.delete("async")
94
- @cli["/encoded_video/#{id}/thumbnails"].post(params)
95
- return true
88
+ if params.delete(:async) || params.delete("async")
89
+ return req("/encoded_video/#{id}/thumbnails", :post, params)
96
90
  end
97
-
91
+
98
92
  unless params.empty?
99
93
  params = "?" + params.map{|k,v| "#{k}=#{v}"}.join("&")
100
94
  end
101
- @cli["/encoded_video/#{id}.jpg#{params}"].get
102
- rescue RestClient::BadRequest=> e
103
- raise BadRequest, e.http_body
95
+ req("/encoded_video/#{id}.jpg#{params}")
104
96
  end
105
97
 
106
98
  # Create a resource with the give data
107
99
  #
108
100
  # hw.create :download, :url => "http://site.com/video.mp4", :title => "testing"
109
- def create(resource, data={})
110
- JSON.parse(@cli["/#{resource}"].post(data))
111
- rescue RestClient::BadRequest=> e
112
- raise BadRequest, e.http_body
101
+ def create(resource, data={})
102
+ req("/#{resource}", :post, data)
113
103
  end
114
104
 
115
105
  # Update an object by giving its resource and ID (except account)
@@ -117,38 +107,68 @@ class HeyWatch
117
107
  # hw.update :format, 9877, :video_bitrate => 890
118
108
  def update(resource, *params)
119
109
  if resource.to_sym == :account
120
- @cli["/#{resource}"].put(params[0])
121
- return true
110
+ return req("/#{resource}", :put, params[0])
122
111
  end
123
112
 
124
113
  id, data = params
125
- @cli["/#{resource}/#{id}"].put(data)
126
- true
127
- rescue RestClient::BadRequest=> e
128
- raise BadRequest, e.http_body
114
+ req("/#{resource}/#{id}", :put, data)
129
115
  end
130
116
 
131
117
  # Delete a resource
132
118
  #
133
119
  # hw.delete :format, 9807
134
120
  def delete(resource, id)
135
- @cli["/#{resource}/#{id}"].delete
136
- true
121
+ req("/#{resource}/#{id}", :delete)
122
+ end
123
+
124
+ # Upload a video from the disk
125
+ #
126
+ # hw.upload "/path/to/video.mp4"
127
+ def upload(path, params={})
128
+ req("/upload", :post, params.merge({:data => File.new(path, "rb")}))
129
+ end
130
+
131
+ def req(path, method=:get, params={}, &block) # :nodoc:
132
+ res = @cli[singularize_resource_in_path(path)].send(method, params, &block)
133
+ retrieve_credits_or_gb_left(res.headers)
134
+
135
+ if res.headers[:status] == "204" || res.empty?
136
+ return true
137
+ end
138
+
139
+ if(res.headers[:content_type] =~ /json/ && !res.to_s.strip.empty?)
140
+ return JSON.parse(res)
141
+ end
142
+
143
+ return res
144
+ rescue RestClient::BadRequest=> e
145
+ raise BadRequest, e.http_body
137
146
  end
138
147
 
139
148
  private
140
149
 
141
- def filter_all(result, filters) # :nodoc:
142
- if filters.is_a?(Array)
143
- filters = Hash[*filters.map{|f| f.split("=") }.flatten]
144
- end
150
+ def filter_all(results, filters)
151
+ filters = filters.map{ |f| f.split("=") } if filters.is_a?(Array)
152
+ filters = Hash[filters.map{ |k, v| [k.to_s, Regexp.new(v.to_s)] }]
145
153
 
146
- filtered = []
147
- result.each do |r|
148
- if eval("true if " + filters.map{|k,v| "'#{r[k.to_s]}' =~ /#{v}/"}.join(" and "))
149
- filtered << r
150
- end
154
+ results.select do |result|
155
+ filters.all?{ |key, matcher| result[key].to_s =~ matcher }
156
+ end
157
+ end
158
+
159
+ def singularize_resource_in_path(path)
160
+ s_path = path.split("/")
161
+ if s_path[1][-1..-1] == "s"
162
+ s_path[1] = s_path[1].slice(0..-2)
163
+ end
164
+ return s_path.join("/")
165
+ end
166
+
167
+ def retrieve_credits_or_gb_left(headers)
168
+ if credits = headers[:x_hw_credits_left]
169
+ @credits_left = credits.to_i
170
+ elsif gb = headers[:x_hw_gb_left]
171
+ @credits_left = gb.to_f
151
172
  end
152
- filtered
153
173
  end
154
174
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: heywatch
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease:
4
+ prerelease: false
6
5
  segments:
7
6
  - 1
7
+ - 1
8
8
  - 0
9
- - 6
10
- version: 1.0.6
9
+ version: 1.1.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - Bruno Celeste
@@ -15,17 +14,16 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2012-05-23 00:00:00 Z
17
+ date: 2012-08-21 00:00:00 +02:00
18
+ default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rest-client
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
24
  requirements:
26
25
  - - ">="
27
26
  - !ruby/object:Gem::Version
28
- hash: 3
29
27
  segments:
30
28
  - 0
31
29
  version: "0"
@@ -35,11 +33,9 @@ dependencies:
35
33
  name: json
36
34
  prerelease: false
37
35
  requirement: &id002 !ruby/object:Gem::Requirement
38
- none: false
39
36
  requirements:
40
37
  - - ">="
41
38
  - !ruby/object:Gem::Version
42
- hash: 3
43
39
  segments:
44
40
  - 0
45
41
  version: "0"
@@ -56,6 +52,7 @@ extra_rdoc_files: []
56
52
  files:
57
53
  - lib/heywatch.rb
58
54
  - bin/heywatch
55
+ has_rdoc: true
59
56
  homepage: http://heywatch.com
60
57
  licenses: []
61
58
 
@@ -65,27 +62,23 @@ rdoc_options: []
65
62
  require_paths:
66
63
  - lib
67
64
  required_ruby_version: !ruby/object:Gem::Requirement
68
- none: false
69
65
  requirements:
70
66
  - - ">="
71
67
  - !ruby/object:Gem::Version
72
- hash: 3
73
68
  segments:
74
69
  - 0
75
70
  version: "0"
76
71
  required_rubygems_version: !ruby/object:Gem::Requirement
77
- none: false
78
72
  requirements:
79
73
  - - ">="
80
74
  - !ruby/object:Gem::Version
81
- hash: 3
82
75
  segments:
83
76
  - 0
84
77
  version: "0"
85
78
  requirements: []
86
79
 
87
80
  rubyforge_project:
88
- rubygems_version: 1.8.24
81
+ rubygems_version: 1.3.6
89
82
  signing_key:
90
83
  specification_version: 3
91
84
  summary: Client library and CLI to encode videos with HeyWatch