heywatch 1.1.0 → 1.1.1

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 (2) hide show
  1. data/lib/heywatch.rb +30 -20
  2. metadata +3 -3
data/lib/heywatch.rb CHANGED
@@ -4,31 +4,41 @@ require "json"
4
4
  class HeyWatch
5
5
  class InvalidResource < ArgumentError; end
6
6
  class BadRequest < RuntimeError; end
7
-
7
+
8
8
  URL = ENV["HEYWATCH_URL"] || "https://heywatch.com"
9
- VERSION = "1.1.0"
10
-
9
+ VERSION = "1.1.1"
10
+
11
11
  attr_reader :credits_left
12
-
12
+
13
13
  # Authenticate with your HeyWatch credentials
14
14
  #
15
15
  # hw = HeyWatch.new(user, passwd)
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
-
20
- self.account
19
+
20
+ if !user.nil? && !password.nil?
21
+ self.account
22
+ end
21
23
  self
22
24
  end
23
-
25
+
24
26
  def self.register(data={})
25
27
  HeyWatch.new(nil, nil).req("/account", :post, data)
26
28
  end
27
-
29
+
30
+ # Get a specific field from account
31
+ #
32
+ # hw["env"] => "sandbox"
33
+ # hw["login"] => "bruno"
34
+ def [](field)
35
+ @me[field]
36
+ end
37
+
28
38
  def inspect # :nodoc:
29
39
  "#<HeyWatch: " + @me.inspect + ">"
30
40
  end
31
-
41
+
32
42
  # Get account information
33
43
  #
34
44
  # hw.account
@@ -47,14 +57,14 @@ class HeyWatch
47
57
  return results if filters.nil? || filters.empty?
48
58
  return filter_all(results, filters)
49
59
  end
50
-
60
+
51
61
  # Get info about a given resource and id
52
62
  #
53
63
  # hw.info :format, 31
54
64
  def info(resource, id)
55
65
  req("/#{resource}/#{id}")
56
66
  end
57
-
67
+
58
68
  # Count objects from a given resources.
59
69
  # Filters are optional
60
70
  #
@@ -63,7 +73,7 @@ class HeyWatch
63
73
  def count(*resource_and_filters)
64
74
  all(*resource_and_filters).size
65
75
  end
66
-
76
+
67
77
  # Get the binary data of a video / encoded_video
68
78
  #
69
79
  # hw.bin :encoded_video, 12345
@@ -71,12 +81,12 @@ class HeyWatch
71
81
  unless [:encoded_video, :video].include?(resource.to_sym)
72
82
  raise InvalidResource, "Can't retrieve '#{resource}'"
73
83
  end
74
-
84
+
75
85
  req("/#{resource}/#{id}.bin", :head) do |res, req|
76
86
  return RestClient.get(res.headers[:location], :raw_response => true, &block)
77
87
  end
78
88
  end
79
-
89
+
80
90
  # Generate thumbnails in the foreground or background via :async => true
81
91
  #
82
92
  # hw.jpg 12345, :start => 2
@@ -94,14 +104,14 @@ class HeyWatch
94
104
  end
95
105
  req("/encoded_video/#{id}.jpg#{params}")
96
106
  end
97
-
107
+
98
108
  # Create a resource with the give data
99
109
  #
100
110
  # hw.create :download, :url => "http://site.com/video.mp4", :title => "testing"
101
111
  def create(resource, data={})
102
112
  req("/#{resource}", :post, data)
103
113
  end
104
-
114
+
105
115
  # Update an object by giving its resource and ID (except account)
106
116
  #
107
117
  # hw.update :format, 9877, :video_bitrate => 890
@@ -113,7 +123,7 @@ class HeyWatch
113
123
  id, data = params
114
124
  req("/#{resource}/#{id}", :put, data)
115
125
  end
116
-
126
+
117
127
  # Delete a resource
118
128
  #
119
129
  # hw.delete :format, 9807
@@ -144,13 +154,13 @@ class HeyWatch
144
154
  rescue RestClient::BadRequest=> e
145
155
  raise BadRequest, e.http_body
146
156
  end
147
-
157
+
148
158
  private
149
-
159
+
150
160
  def filter_all(results, filters)
151
161
  filters = filters.map{ |f| f.split("=") } if filters.is_a?(Array)
152
162
  filters = Hash[filters.map{ |k, v| [k.to_s, Regexp.new(v.to_s)] }]
153
-
163
+
154
164
  results.select do |result|
155
165
  filters.all?{ |key, matcher| result[key].to_s =~ matcher }
156
166
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 1
8
- - 0
9
- version: 1.1.0
8
+ - 1
9
+ version: 1.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Bruno Celeste
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-08-21 00:00:00 +02:00
17
+ date: 2012-10-19 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency