play_asia 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ bin/
1
2
  *.sublime-*
2
3
  .idea
3
4
  *.gem
data/README.md CHANGED
@@ -20,9 +20,13 @@ Or install it yourself as:
20
20
 
21
21
  api = PlayAsia::Api.new
22
22
  key: # your API key,
23
- user: # your API user,
23
+ user: # your API user
24
24
 
25
- api.listing mask: [:price, :name], type: [:game]
25
+ api.listing(mask: [:price, :name], type: [:game]).exec
26
+ # => PlayAsia::Response
27
+
28
+ api.listing(mask: [:price, :name], type: [:game]).url
29
+ # => http://www.play-asia.com/__api__.php?...
26
30
 
27
31
  ## Contributing
28
32
 
data/Rakefile CHANGED
@@ -1,4 +1,3 @@
1
- #!/usr/bin/env rake
2
1
  require "bundler/gem_tasks"
3
2
  require 'rake/testtask'
4
3
 
data/lib/play_asia.rb CHANGED
@@ -2,7 +2,8 @@ require "play_asia/version"
2
2
  require "play_asia/response"
3
3
  require "play_asia/http_client"
4
4
  require "play_asia/api"
5
- require "play_asia/api_listing"
5
+ require "play_asia/query"
6
+ require "play_asia/listing_query"
6
7
 
7
8
  module PlayAsia
8
9
  end
data/lib/play_asia/api.rb CHANGED
@@ -1,35 +1,25 @@
1
- require 'nokogiri'
2
-
3
1
  class PlayAsia::Api
4
- attr_accessor :endpoint
5
2
  attr_accessor :headers
6
- attr_accessor :http_client
7
3
 
8
4
  def initialize(opts = {})
9
5
  @opts = opts
10
- @endpoint = 'http://www.play-asia.com/__api__.php'
11
6
  @http_client = PlayAsia::HttpClient.new
12
7
  end
13
8
 
14
9
  def query(opts = {})
15
- opts = @opts.merge opts
16
- url = build_url @endpoint, opts
17
- headers = @headers || {}
18
-
19
- PlayAsia::Response.new @http_client.request(url, headers).read
10
+ query = PlayAsia::Query.new http_client
11
+ query.build(@headers || {}, @opts.merge(opts))
12
+ query
20
13
  end
21
-
22
- private
23
- def build_url(endpoint, opts)
24
- ensure_required_parameters opts
25
-
26
- params = opts.map { |k,v| "#{k}=#{v}" }.join '&'
27
- "#{endpoint}?#{params}"
14
+
15
+ def listing(opts = {})
16
+ query = PlayAsia::ListingQuery.new http_client
17
+ query.build(@headers || {}, @opts.merge(opts))
18
+ query
28
19
  end
29
20
 
30
- def ensure_required_parameters opts
31
- raise ArgumentError, 'Missing API key (expected :key)' unless opts[:key]
32
- raise ArgumentError, 'Missing User ID (expected :user)' unless opts[:user]
33
- raise ArgumentError, 'Missing Query type (expected :query)' unless opts[:query]
21
+ private
22
+ def http_client
23
+ @http_client
34
24
  end
35
25
  end
@@ -1,13 +1,4 @@
1
- class PlayAsia::Api
2
- def listing(opts = {})
3
- opts = process_friendly_keys opts.merge({ query: :listing })
4
- response = query opts
5
-
6
- raise "Query error: '#{response.error_message}'" if response.error?
7
-
8
- response
9
- end
10
-
1
+ class PlayAsia::ListingQuery < PlayAsia::Query
11
2
  private
12
3
  MASK = {
13
4
  price: 'p',
@@ -318,7 +309,8 @@ class PlayAsia::Api
318
309
  us: 4
319
310
  }
320
311
 
321
- def process_friendly_keys opts
312
+ def prepare_opts(opts)
313
+ opts[:query] = :listing
322
314
  opts[:mask] = map_array_to_string opts[:mask], MASK if opts[:mask]
323
315
  opts[:type] = map_array_to_string opts[:type], TYPES, ',' if opts[:type]
324
316
  opts[:genre] = map_array_to_string opts[:genre], GENRES, ',' if opts[:genre]
@@ -0,0 +1,34 @@
1
+ class PlayAsia::Query
2
+ attr_reader :url
3
+
4
+ def initialize(http_client)
5
+ @endpoint = 'http://www.play-asia.com/__api__.php'
6
+ @http_client = http_client
7
+ end
8
+
9
+ def build(headers, opts)
10
+ @url = build_url(@endpoint, opts)
11
+ end
12
+
13
+ def exec
14
+ PlayAsia::Response.new @http_client.request(@url, headers).read
15
+ end
16
+
17
+ private
18
+ def build_url(endpoint, opts)
19
+ opts = prepare_opts opts
20
+ ensure_required_parameters opts
21
+ params = opts.map { |k,v| "#{k}=#{v}" }.join '&'
22
+ "#{endpoint}?#{params}"
23
+ end
24
+
25
+ def prepare_opts(opts)
26
+ opts
27
+ end
28
+
29
+ def ensure_required_parameters opts
30
+ raise ArgumentError, 'Missing API key (expected :key)' unless opts[:key]
31
+ raise ArgumentError, 'Missing User ID (expected :user)' unless opts[:user]
32
+ raise ArgumentError, 'Missing Query type (expected :query)' unless opts[:query]
33
+ end
34
+ end
@@ -1,3 +1,5 @@
1
+ require 'nokogiri'
2
+
1
3
  class PlayAsia::ResponseError < RuntimeError
2
4
  end
3
5
 
@@ -1,3 +1,3 @@
1
1
  module PlayAsia
2
- VERSION = '1.0.0'
3
- end
2
+ VERSION = '2.0.0'
3
+ end
data/play_asia.gemspec CHANGED
@@ -9,6 +9,7 @@ Gem::Specification.new do |gem|
9
9
  gem.homepage = 'http://www.onthegame.com.au/about/opensource'
10
10
 
11
11
  gem.add_runtime_dependency 'nokogiri'
12
+ gem.add_development_dependency 'rake'
12
13
  gem.add_development_dependency 'minitest-reporters'
13
14
 
14
15
  gem.files = `git ls-files`.split($\)
data/test/test_api.rb CHANGED
@@ -8,19 +8,20 @@ class ApiTest < MiniTest::Unit::TestCase
8
8
  def setup
9
9
  @api = PlayAsia::Api.new
10
10
  @http_client = StubbedHttpClient.new STUBBED_RESPONSE
11
- @api.http_client = @http_client
11
+ def @api.http_client
12
+ @http_client
13
+ end
12
14
  end
13
15
 
14
16
  def test_query_defaults_to_using_play_asia_domain
15
- @api.query key: 'api_key', user: 1, query: 'test'
17
+ query = @api.query(key: 'api_key', user: 1, query: 'test')
16
18
 
17
- assert @http_client.last_url.start_with? 'http://www.play-asia.com/__api__.php'
19
+ assert query.url.start_with? 'http://www.play-asia.com/__api__.php'
18
20
  end
19
21
 
20
22
  def test_query_uses_arguments_as_query_string
21
- @api.query key: 'api_key', user: 1, query: 'test', a: 1, b: 2, c: 3
22
- url = @http_client.last_url
23
- query_string = parse_hash_from_query_string url.split('?').last
23
+ query = @api.query(key: 'api_key', user: 1, query: 'test', a: 1, b: 2, c: 3)
24
+ query_string = parse_hash_from_query_string query.url.split('?').last
24
25
 
25
26
  assert_equal query_string['key'], 'api_key'
26
27
  assert_equal query_string['user'], '1'
@@ -4,137 +4,106 @@ require 'minitest/mock'
4
4
  require_relative 'stubs/stubbed_http_client'
5
5
 
6
6
  class ApiListingTest < MiniTest::Unit::TestCase
7
+ KEY = 'abc'
8
+ USER = 1
7
9
  OK_RESPONSE = PlayAsia::Response.new '<message><status><query>test</query><error>0</error><errorstring /><items>1</items></status><content><item>1</item></content></message>'
8
10
 
11
+ def setup
12
+ @api = PlayAsia::Api.new
13
+ @http_client = StubbedHttpClient.new OK_RESPONSE
14
+ def @api.http_client
15
+ @http_client
16
+ end
17
+ end
18
+
9
19
  def test_should_map_mask_array_to_string
10
- api = api_with_stubbed_query
11
- api.listing mask: [:price, :barcodes, :manufacturer_codes, :sale_info, :release_date, :name, :image,
12
- :url, :affiliate_url, :genre, :version, :encoding, :compatible]
20
+ query = @api.listing(key: KEY, user: USER, mask: [:price, :barcodes, :manufacturer_codes, :sale_info, :release_date, :name, :image,
21
+ :url, :affiliate_url, :genre, :version, :encoding, :compatible])
13
22
 
14
- assert_equal 'pjmsrnilagvec', api.queried_options[:mask]
23
+ assert_equal 'http://www.play-asia.com/__api__.php?key=abc&user=1&mask=pjmsrnilagvec&query=listing', query.url
15
24
  end
16
25
 
17
26
  def test_should_use_string_for_mask_if_given
18
- api = api_with_stubbed_query
19
- api.listing mask: 'string'
27
+ query = @api.listing key: KEY, user: USER, mask: 'string'
20
28
 
21
- assert_equal 'string', api.queried_options[:mask]
29
+ assert_equal 'http://www.play-asia.com/__api__.php?key=abc&user=1&mask=string&query=listing', query.url
22
30
  end
23
31
 
24
32
  def test_should_map_type_array_to_csv_of_integers
25
- api = api_with_stubbed_query
26
- api.listing type: [:accessory, :cd, :groceries, :apparel, :electronics, :movie, :book, :game, :toy]
33
+ query = @api.listing key: KEY, user: USER, type: [:accessory, :cd, :groceries, :apparel, :electronics, :movie, :book, :game, :toy]
27
34
 
28
- assert_equal '2,3,7,8,9,4,6,1,5', api.queried_options[:type]
35
+ assert_equal 'http://www.play-asia.com/__api__.php?key=abc&user=1&type=2,3,7,8,9,4,6,1,5&query=listing', query.url
29
36
  end
30
37
 
31
38
  def test_should_use_string_for_type_if_given
32
- api = api_with_stubbed_query
33
- api.listing type: 'string'
39
+ query = @api.listing key: KEY, user: USER, type: 'string'
34
40
 
35
- assert_equal 'string', api.queried_options[:type]
41
+ assert_equal 'http://www.play-asia.com/__api__.php?key=abc&user=1&type=string&query=listing', query.url
36
42
  end
37
43
 
38
44
  def test_should_map_genre_array_to_csv_of_integers
39
- api = api_with_stubbed_query
40
- api.listing genre: [:strategy_video_game, :racing_video_game, :simulation_video_game, :tv_series_dvd_vcd]
45
+ query = @api.listing key: KEY, user: USER, genre: [:strategy_video_game, :racing_video_game, :simulation_video_game, :tv_series_dvd_vcd]
41
46
 
42
- assert_equal '10,11,12,122', api.queried_options[:genre]
47
+ assert_equal 'http://www.play-asia.com/__api__.php?key=abc&user=1&genre=10,11,12,122&query=listing', query.url
43
48
  end
44
49
 
45
50
  def test_should_use_string_for_genre_if_given
46
- api = api_with_stubbed_query
47
- api.listing genre: 'string'
51
+ query = @api.listing key: KEY, user: USER, genre: 'string'
48
52
 
49
- assert_equal 'string', api.queried_options[:genre]
53
+ assert_equal 'http://www.play-asia.com/__api__.php?key=abc&user=1&genre=string&query=listing', query.url
50
54
  end
51
55
 
52
56
  def test_should_map_compatible_array_to_csv_of_integers
53
- api = api_with_stubbed_query
54
- api.listing compatible: [:dreamcast, :ipad]
57
+ query = @api.listing key: KEY, user: USER, compatible: [:dreamcast, :ipad]
55
58
 
56
- assert_equal '9,111', api.queried_options[:compatible]
59
+ assert_equal 'http://www.play-asia.com/__api__.php?key=abc&user=1&compatible=9,111&query=listing', query.url
57
60
  end
58
61
 
59
62
  def test_should_use_string_for_compatible_if_given
60
- api = api_with_stubbed_query
61
- api.listing compatibility: 'string'
63
+ query = @api.listing key: KEY, user: USER, compatibility: 'string'
62
64
 
63
- assert_equal 'string', api.queried_options[:compatibility]
65
+ assert_equal 'http://www.play-asia.com/__api__.php?key=abc&user=1&compatibility=string&query=listing', query.url
64
66
  end
65
67
 
66
68
  def test_should_map_encoding_array_to_csv_of_integers
67
- api = api_with_stubbed_query
68
- api.listing encoding: [:pal, :aac]
69
+ query = @api.listing key: KEY, user: USER, encoding: [:pal, :aac]
69
70
 
70
- assert_equal '4,31', api.queried_options[:encoding]
71
+ assert_equal 'http://www.play-asia.com/__api__.php?key=abc&user=1&encoding=4,31&query=listing', query.url
71
72
  end
72
73
 
73
74
  def test_should_use_string_for_encoding_if_given
74
- api = api_with_stubbed_query
75
- api.listing encoding: 'string'
75
+ query = @api.listing key: KEY, user: USER, encoding: 'string'
76
76
 
77
- assert_equal 'string', api.queried_options[:encoding]
77
+ assert_equal 'http://www.play-asia.com/__api__.php?key=abc&user=1&encoding=string&query=listing', query.url
78
78
  end
79
79
 
80
80
  def test_should_map_version_array_to_csv_of_integers
81
- api = api_with_stubbed_query
82
- api.listing version: [:asia, :hong_kong]
81
+ query = @api.listing key: KEY, user: USER, version: [:asia, :hong_kong]
83
82
 
84
- assert_equal '6,19', api.queried_options[:version]
83
+ assert_equal 'http://www.play-asia.com/__api__.php?key=abc&user=1&version=6,19&query=listing', query.url
85
84
  end
86
85
 
87
86
  def test_should_use_version_for_encoding_if_given
88
- api = api_with_stubbed_query
89
- api.listing version: 'string'
87
+ query = @api.listing key: KEY, user: USER, version: 'string'
90
88
 
91
- assert_equal 'string', api.queried_options[:version]
89
+ assert_equal 'http://www.play-asia.com/__api__.php?key=abc&user=1&version=string&query=listing', query.url
92
90
  end
93
91
 
94
92
  def test_should_map_skip_preowned_boolean_true_to_1
95
- api = api_with_stubbed_query
96
- api.listing skip_preowned: true
93
+ query = @api.listing key: KEY, user: USER, skip_preowned: true
97
94
 
98
- assert_equal '1', api.queried_options[:skip_preowned]
95
+ assert_equal 'http://www.play-asia.com/__api__.php?key=abc&user=1&skip_preowned=1&query=listing', query.url
99
96
  end
100
97
 
101
98
  def test_should_map_skip_preowned_boolean_false_to_0
102
- api = api_with_stubbed_query
103
- api.listing skip_preowned: false
99
+ query = @api.listing key: KEY, user: USER, skip_preowned: false
104
100
 
105
- assert_equal '0', api.queried_options[:skip_preowned]
101
+ assert_equal 'http://www.play-asia.com/__api__.php?key=abc&user=1&skip_preowned=0&query=listing', query.url
106
102
  end
107
103
 
108
104
  def test_should_allow_string_for_skip_preowned
109
- api = api_with_stubbed_query
110
- api.listing skip_preowned: 'yes'
111
-
112
- assert_equal 'yes', api.queried_options[:skip_preowned]
113
- end
114
-
115
- def test_should_only_use_supplied_parameters_and_query
116
- api = api_with_stubbed_query
117
- api.listing type: [:game], mask: [:name]
118
-
119
- refute_nil api.queried_options[:type]
120
- refute_nil api.queried_options[:mask]
121
- assert_equal [:type, :mask, :query], api.queried_options.keys
122
- end
123
-
124
- private
125
- def api_with_stubbed_query
126
- api = PlayAsia::Api.new
127
-
128
- # overwrite the query method so we can grab the options passed to it
129
- class << api
130
- attr_reader :queried_options
131
-
132
- def query(opts = {})
133
- @queried_options = opts
134
- OK_RESPONSE
135
- end
136
- end
105
+ query = @api.listing key: KEY, user: USER, skip_preowned: 'yes'
137
106
 
138
- api
107
+ assert_equal 'http://www.play-asia.com/__api__.php?key=abc&user=1&skip_preowned=yes&query=listing', query.url
139
108
  end
140
109
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: play_asia
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-02-21 00:00:00.000000000 Z
13
+ date: 2013-03-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: nokogiri
@@ -28,6 +28,22 @@ dependencies:
28
28
  - - ! '>='
29
29
  - !ruby/object:Gem::Version
30
30
  version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
31
47
  - !ruby/object:Gem::Dependency
32
48
  name: minitest-reporters
33
49
  requirement: !ruby/object:Gem::Requirement
@@ -58,8 +74,9 @@ files:
58
74
  - Rakefile
59
75
  - lib/play_asia.rb
60
76
  - lib/play_asia/api.rb
61
- - lib/play_asia/api_listing.rb
62
77
  - lib/play_asia/http_client.rb
78
+ - lib/play_asia/listing_query.rb
79
+ - lib/play_asia/query.rb
63
80
  - lib/play_asia/response.rb
64
81
  - lib/play_asia/version.rb
65
82
  - play_asia.gemspec
@@ -79,15 +96,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
79
96
  - - ! '>='
80
97
  - !ruby/object:Gem::Version
81
98
  version: '0'
99
+ segments:
100
+ - 0
101
+ hash: 4515185138541930384
82
102
  required_rubygems_version: !ruby/object:Gem::Requirement
83
103
  none: false
84
104
  requirements:
85
105
  - - ! '>='
86
106
  - !ruby/object:Gem::Version
87
107
  version: '0'
108
+ segments:
109
+ - 0
110
+ hash: 4515185138541930384
88
111
  requirements: []
89
112
  rubyforge_project:
90
- rubygems_version: 1.8.24
113
+ rubygems_version: 1.8.25
91
114
  signing_key:
92
115
  specification_version: 3
93
116
  summary: Play Asia API