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.
Files changed (2) hide show
  1. data/lib/hot_uk_deals.rb +46 -51
  2. 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
- class HotUKDeals
4
+ module HotUKDeals
5
5
 
6
- API_URI = 'http://api.hotukdeals.com/rest_api/v2/'
6
+ API = 'http://api.hotukdeals.com/rest_api/v2/'
7
7
 
8
- def self.developer_key
9
- class_variable_defined?(:@@developer_key) ? @@developer_key : nil
8
+ def self.method_missing(*args)
9
+ (@shim ||= Shim.new).send(*args)
10
10
  end
11
11
 
12
- def self.developer_key=(developer_key)
13
- @@developer_key = developer_key
14
- end
15
-
16
- def self.results
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(HotUKDeals::API_URI)
31
- api_uri.query = URI.encode_www_form(parameters)
32
- response = Net::HTTP.get(api_uri)
33
- parse_response(response)
34
- rescue
35
- set_network_error
36
- return false
37
- end
38
- end
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
- true
58
- end
59
-
60
- def self.set_network_error
61
- @@error = 'Network error'
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.3
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: 2011-12-27 00:00:00.000000000Z
12
+ date: 2012-01-23 00:00:00.000000000Z
13
13
  dependencies: []
14
- description: A gem to be used for accessing the HotUKDeals API
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 Access
44
+ summary: HotUKDeals API Shim
45
45
  test_files: []