pricegrabber 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c4c354be927afee774eeb1e6df2b4641b93d7c3
4
+ data.tar.gz: bf1bb8a37bdcd40aa96aa9f11604a53ef4da8ad6
5
+ SHA512:
6
+ metadata.gz: b28c0b4a7c5e2b49bdd5a321a6a8a76920dbd5c1c0db29e9c45c8ac6388756d74ef6dbcac6cba3f4523e21245c5ae5bccd8c1fa4634b158746a1d74f22739a77
7
+ data.tar.gz: a64d3a69ea70c449a4e969b7463f494666affdabbe37215a47a5f48a6426023eb92fd3a143453190eb80a1c6f90016dbcf040a052242c3eb786e3beefd19dfa9
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pricegrabber.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Pricegrabber
2
+
3
+ Pricegrabber is a gem that allows you to request product and pricing information from the Pricegrabber API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pricegrabber'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install pricegrabber
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pricegrabber.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
39
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pricegrabber"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ require 'pricegrabber/version'
2
+ require 'pricegrabber/client'
3
+ require 'pricegrabber/request'
4
+ require 'pricegrabber/api_error'
5
+ require 'pricegrabber/response'
@@ -0,0 +1,4 @@
1
+ module PriceGrabber
2
+ class APIError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,15 @@
1
+ module PriceGrabber
2
+ class Client
3
+ def initialize(api_key:)
4
+ @api_key = api_key
5
+ end
6
+
7
+ def find_by_id(asin: nil, upc: nil)
8
+ Request.new(asin: asin, upc: upc, version: '2.55', pid: '1107', key: @api_key)
9
+ end
10
+
11
+ def search(query)
12
+ Request.new(q: query, upc: '1', version: '2.55', pid: '1107', key: @api_key)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,98 @@
1
+ require 'uri'
2
+ require 'httpi'
3
+
4
+ module PriceGrabber
5
+ class Request
6
+ def initialize(version:, pid:, key:, asin: nil, q: nil, masterid: nil, upc: nil, driver: :net_http)
7
+ @version = version
8
+ @pid = pid
9
+ @key = key
10
+ @asin = asin
11
+ @q = q
12
+ @upc = upc
13
+ @masterid = [*masterid]
14
+ @driver = driver
15
+ @wants = []
16
+ end
17
+
18
+ def to_uri
19
+ uri = ::URI::HTTP.build({})
20
+ uri.scheme = "http"
21
+ uri.host = "sws.api.pricegrabber.com"
22
+ uri.path = "/search_xml.php"
23
+ uri.query = [
24
+ version,
25
+ pid,
26
+ key,
27
+ asin,
28
+ q,
29
+ upc,
30
+ masterid,
31
+ ].compact.sort.join("&")
32
+ uri
33
+ end
34
+
35
+ def to_s
36
+ to_uri.to_s
37
+ end
38
+
39
+ def to_curl
40
+ "curl -G '#{to_s}'"
41
+ end
42
+
43
+ def call
44
+ request = HTTPI::Request.new
45
+ request.url = to_s
46
+ resp = HTTPI.get(request, @driver)
47
+ PriceGrabber::Response.new(resp, @wants)
48
+ end
49
+
50
+ def pluck(*attributes)
51
+ @wants = attributes.map(&:to_s)
52
+ self
53
+ end
54
+
55
+ private
56
+
57
+ def version
58
+ "version=#{@version}"
59
+ end
60
+
61
+ def pid
62
+ "pid=#{@pid}"
63
+ end
64
+
65
+ def key
66
+ "key=#{@key}"
67
+ end
68
+
69
+ def asin
70
+ if @asin
71
+ "asin=#{@asin}"
72
+ end
73
+ end
74
+
75
+ def q
76
+ if @q
77
+ "q=#{@q.gsub(/\s/, '+')}"
78
+ end
79
+ end
80
+
81
+ def upc
82
+ if @upc
83
+ "upc=#{@upc}"
84
+ end
85
+ end
86
+
87
+ def masterid
88
+ case @masterid.length
89
+ when 0
90
+ nil
91
+ when 1
92
+ "masterid=#{@masterid.join(",")}"
93
+ else
94
+ "masterids=#{@masterid.join(",")}"
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,40 @@
1
+ require 'active_support/core_ext/hash/conversions'
2
+ require 'pricegrabber/response_item'
3
+
4
+ module PriceGrabber
5
+ # Represents a response from the PriceGrabber API.
6
+ class Response
7
+ include Enumerable
8
+ # @param response [HTTPI::Response] The XML response from Pricegrabber's API
9
+ # @param wants [Array<String>] Attributes from the response that should be made available within this response
10
+ def initialize(response, wants)
11
+ @status = response.code
12
+ resp_hash = Hash.from_xml(response.body)
13
+ @error = resp_hash["document"]["error"]
14
+ @results = []
15
+ [resp_hash["document"]["product"]].flatten.each do |product|
16
+ curr_result = ResponseItem.with_attributes(wants)
17
+ wants.map do |want|
18
+ parts = want.split(".")
19
+ curr = parts.shift
20
+ curr_value = product
21
+ while curr && curr_value
22
+ curr_value = curr_value[curr]
23
+ curr = parts.shift
24
+ end
25
+
26
+ curr_result.public_send(:"#{want.tr(".", "_")}=", curr_value)
27
+ end
28
+ @results << curr_result
29
+ end
30
+ end
31
+
32
+ def each(*args, &block)
33
+ @results.each(*args, &block)
34
+ end
35
+
36
+ def successful?
37
+ @status < 300 && @status >= 200 && @error == nil
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,23 @@
1
+ module PriceGrabber
2
+ class ResponseItem
3
+ def self.with_attributes(attrs)
4
+ ins = new
5
+ eigenclass = class << ins
6
+ self
7
+ end
8
+ attrs.each do |attr|
9
+ attr_clean = attr.tr(".", "_")
10
+ eigenclass.instance_eval do
11
+ define_method(attr_clean.to_sym) do
12
+ instance_variable_get(:"@#{attr_clean}")
13
+ end
14
+
15
+ define_method(:"#{attr_clean}=") do |val|
16
+ instance_variable_set(:"@#{attr_clean}", val)
17
+ end
18
+ end
19
+ end
20
+ ins
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Pricegrabber
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pricegrabber/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pricegrabber"
8
+ spec.version = Pricegrabber::VERSION
9
+ spec.authors = ["Timothy Raymond"]
10
+ spec.email = ["xtjraymondx@gmail.com"]
11
+
12
+ spec.summary = %q{A gem for fetching price information from Pricegrabber's API}
13
+ spec.homepage = "http://www.example.com"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "httpi", "~> 2.4"
22
+ spec.add_dependency "activesupport", "~> 4.2"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pricegrabber
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Timothy Raymond
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httpi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - xtjraymondx@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/pricegrabber.rb
100
+ - lib/pricegrabber/api_error.rb
101
+ - lib/pricegrabber/client.rb
102
+ - lib/pricegrabber/request.rb
103
+ - lib/pricegrabber/response.rb
104
+ - lib/pricegrabber/response_item.rb
105
+ - lib/pricegrabber/version.rb
106
+ - pricegrabber.gemspec
107
+ homepage: http://www.example.com
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.8
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: A gem for fetching price information from Pricegrabber's API
131
+ test_files: []
132
+ has_rdoc: