hot_uk_deals 0.0.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hot_uk_deals.rb +46 -51
- metadata +4 -4
data/lib/hot_uk_deals.rb
CHANGED
@@ -1,64 +1,59 @@
|
|
1
1
|
require 'net/http'
|
2
2
|
require 'json'
|
3
3
|
|
4
|
-
|
4
|
+
module HotUKDeals
|
5
5
|
|
6
|
-
|
6
|
+
API = 'http://api.hotukdeals.com/rest_api/v2/'
|
7
7
|
|
8
|
-
def self.
|
9
|
-
|
8
|
+
def self.method_missing(*args)
|
9
|
+
(@shim ||= Shim.new).send(*args)
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
class_variable_defined?(:@@results) ? @@results : nil
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.error
|
21
|
-
class_variable_defined?(:@@error) ? @@error : nil
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.send_request(parameters = {})
|
25
|
-
begin
|
26
|
-
@@developer_key = nil unless class_variable_defined?(:@@developer_key)
|
27
|
-
@@results = nil
|
28
|
-
@@error = nil
|
12
|
+
class Shim
|
13
|
+
|
14
|
+
attr_accessor :developer_key, :results, :error, :total_results
|
15
|
+
|
16
|
+
def send_request(parameters = {})
|
29
17
|
parameters.merge!(mandatory_parameters)
|
30
|
-
api_uri = URI(
|
31
|
-
api_uri.query =
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
def self.mandatory_parameters
|
41
|
-
{
|
42
|
-
'key' => @@developer_key,
|
43
|
-
'output' => 'json'
|
44
|
-
}
|
45
|
-
end
|
46
|
-
|
47
|
-
def self.parse_response(response)
|
48
|
-
begin
|
49
|
-
@@results = JSON.parse(response)
|
50
|
-
if @@error = @@results['error']
|
51
|
-
return false
|
18
|
+
api_uri = URI(API)
|
19
|
+
api_uri.query = encode_www_form(parameters)
|
20
|
+
reset_instance_variables!
|
21
|
+
begin
|
22
|
+
response = Net::HTTP.get(api_uri)
|
23
|
+
parse_response(response)
|
24
|
+
rescue
|
25
|
+
self.error = 'Network error'
|
26
|
+
false
|
52
27
|
end
|
53
|
-
rescue
|
54
|
-
set_network_error
|
55
|
-
return false
|
56
28
|
end
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
29
|
+
|
30
|
+
def send_request!(parameters = {})
|
31
|
+
raise error unless send_request(parameters)
|
32
|
+
true
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse_response(response)
|
36
|
+
response_hash = JSON.parse(response)
|
37
|
+
return false if self.error = response_hash['error']
|
38
|
+
self.results = response_hash['deals']['items']
|
39
|
+
self.total_results = response_hash['total_results']
|
40
|
+
true
|
41
|
+
end
|
42
|
+
|
43
|
+
def mandatory_parameters
|
44
|
+
{ 'key' => developer_key, 'output' => 'json' }
|
45
|
+
end
|
46
|
+
|
47
|
+
def encode_www_form(parameters)
|
48
|
+
parameters.map { |k, v| "#{k}=#{v}" }.join('&')
|
49
|
+
end
|
50
|
+
|
51
|
+
def reset_instance_variables!
|
52
|
+
self.results = nil
|
53
|
+
self.error = nil
|
54
|
+
self.total_results = 0
|
55
|
+
end
|
56
|
+
|
62
57
|
end
|
63
58
|
|
64
59
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hot_uk_deals
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,9 +9,9 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-01-23 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
|
-
description: A
|
14
|
+
description: A shim for the HotUKDeals API.
|
15
15
|
email: chris.patuzzo@gmail.com
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
@@ -41,5 +41,5 @@ rubyforge_project:
|
|
41
41
|
rubygems_version: 1.8.10
|
42
42
|
signing_key:
|
43
43
|
specification_version: 3
|
44
|
-
summary: HotUKDeals API
|
44
|
+
summary: HotUKDeals API Shim
|
45
45
|
test_files: []
|