has_offers 0.2.0 → 0.2.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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
data/has_offers.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{has_offers}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["jkongie"]
12
- s.date = %q{2010-09-27}
12
+ s.date = %q{2010-09-28}
13
13
  s.description = %q{This gem is a wrapper around the HasOffers API. In order to use the API, your network must be on the Enterprise pricing plan or a Dedicated solution.}
14
14
  s.email = %q{james.kong@sentia.com.au}
15
15
  s.extra_rdoc_files = [
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
26
26
  "has_offers.gemspec",
27
27
  "lib/has_offers.rb",
28
28
  "lib/has_offers/api.rb",
29
+ "lib/has_offers/response.rb",
29
30
  "spec/has_offers_spec.rb",
30
31
  "spec/spec.opts",
31
32
  "spec/spec_helper.rb"
@@ -15,15 +15,23 @@ module HasOffers
15
15
 
16
16
  API_SERVICE_HOST = 'https://api.hasoffers.com/Api'
17
17
 
18
- # TODO - Load Auth from a YAML file as well
19
- def initialize(authentication = {})
20
- @authentication = authentication
18
+ # Initialize a HasOffers object with the authentication values
19
+ # This accepts
20
+ # 1. A string declaring a YAML file with the correct api_key and network_id
21
+ # 2. A hash containing the api_key and network_id
22
+ # 3. By default, if left empty, it will look for config/has_offer.yml file to declare the authentication
23
+ def initialize(authentication = nil)
24
+ if authentication.nil? || authentication.is_a?(String)
25
+ config_file = authentication.nil? ? 'config/has_offers.yml' : authentication
26
+ @authentication = YAML::load(IO.read(config_file)) if File.exists?(config_file)
27
+ elsif authentication.is_a?(Hash)
28
+ @authentication = authentication
29
+ end
21
30
  @default_params = { 'Format' => 'json',
22
31
  'Service' => 'HasOffers',
23
32
  'Version' => '2',
24
- 'NetworkId' => authentication[:network_id],
25
- 'NetworkToken' => authentication[:api_key] }
26
-
33
+ 'NetworkId' => @authentication['network_id'],
34
+ 'NetworkToken' => @authentication['api_key'] }
27
35
  self.debug_mode = false
28
36
  self.test_mode = false
29
37
  end
@@ -45,6 +53,7 @@ module HasOffers
45
53
  http = new_http uri
46
54
  raw_request = Net::HTTP::Get.new(uri.request_uri)
47
55
  http_response = http.request raw_request
56
+ Response.new http_response
48
57
  end
49
58
  end
50
59
 
@@ -0,0 +1,69 @@
1
+ module HasOffers
2
+
3
+ class Response
4
+
5
+ attr_reader :body, :http_status_code, :http_message, :http_headers
6
+
7
+ def success?
8
+ @http_status_code.to_s == '200' and status == 1
9
+ end
10
+
11
+ def status
12
+ @body['response']['status']
13
+ end
14
+
15
+ # allows specific api calls to post-process the data for ease of use
16
+ def set_data(data)
17
+ @processed_data = data
18
+ end
19
+
20
+ def raw_data
21
+ @body
22
+ end
23
+
24
+ def data
25
+ @processed_data || (paginated_response? ? @body['response']['data']['data'] : @body['response']['data'])
26
+ end
27
+
28
+ def page_info
29
+ if paginated_response?
30
+ {'page_count' => @body['response']['data']['pageCount'],
31
+ 'current' => @body['response']['data']['current'],
32
+ 'count' => @body['response']['data']['count'],
33
+ 'page' => @body['response']['data']['page']}
34
+ else
35
+ {}
36
+ end
37
+ end
38
+
39
+ def validation_error?
40
+ status == -1 and data['error_code'] == 1
41
+ end
42
+
43
+ def error_messages
44
+ if data.is_a? Hash and data["errors"] and data["errors"]["error"]
45
+ data["errors"]["error"].map { |error| error["err_msg"] }
46
+ elsif @body["response"]["errors"]
47
+ @body["response"]["errors"].map { |error| error["err_msg"] }
48
+ else
49
+ []
50
+ end
51
+ end
52
+
53
+ def initialize(response)
54
+ @test = false
55
+ @body = JSON.parse(response.body)
56
+ @http_status_code = response.code
57
+ @http_message = response.message
58
+ @http_headers = response.to_hash
59
+ end
60
+
61
+ protected
62
+
63
+ def paginated_response?
64
+ @body['response']['data'] and @body['response']['data'].is_a?(Hash) and @body['response']['data']['pageCount']
65
+ end
66
+
67
+ end
68
+
69
+ end
data/lib/has_offers.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  %w(
2
2
  has_offers/api
3
+ has_offers/response
3
4
  ).each do |lib|
4
5
  require File.join(File.dirname(__FILE__), lib)
5
6
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - jkongie
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-27 00:00:00 +10:00
17
+ date: 2010-09-28 00:00:00 +10:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -51,6 +51,7 @@ files:
51
51
  - has_offers.gemspec
52
52
  - lib/has_offers.rb
53
53
  - lib/has_offers/api.rb
54
+ - lib/has_offers/response.rb
54
55
  - spec/has_offers_spec.rb
55
56
  - spec/spec.opts
56
57
  - spec/spec_helper.rb