invisiblehand 0.1.2 → 0.1.3
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/README.md +17 -0
 - data/invisiblehand.sample.yml +2 -2
 - data/lib/invisiblehand/api.rb +14 -2
 - data/lib/invisiblehand/version.rb +1 -1
 - data/spec/api_spec.rb +75 -65
 - metadata +4 -4
 
    
        data/README.md
    CHANGED
    
    | 
         @@ -86,6 +86,10 @@ If the API returns any error information, an `InvisibleHand::Error::APIError` is 
     | 
|
| 
       86 
86 
     | 
    
         
             
            thrown and the `#message` method of the error object will contain the error
         
     | 
| 
       87 
87 
     | 
    
         
             
            output from the API.
         
     | 
| 
       88 
88 
     | 
    
         | 
| 
      
 89 
     | 
    
         
            +
            The `InvisibleHand::Error::APIError` object also contains `#url` and
         
     | 
| 
      
 90 
     | 
    
         
            +
            `#raw_response` methods that give you what URL on the API got hit and what the
         
     | 
| 
      
 91 
     | 
    
         
            +
            raw response from the server was for debugging purposes.
         
     | 
| 
      
 92 
     | 
    
         
            +
             
     | 
| 
       89 
93 
     | 
    
         
             
            ### Logging
         
     | 
| 
       90 
94 
     | 
    
         | 
| 
       91 
95 
     | 
    
         
             
            The InvisibleHand gem does have debug logging that goes to an internal `Logger`
         
     | 
| 
         @@ -108,6 +112,19 @@ The gem looks for a DEBUG environment variable. If DEBUG is set, debugging 
     | 
|
| 
       108 
112 
     | 
    
         
             
            information will be printed out to the screen. This includes URL information
         
     | 
| 
       109 
113 
     | 
    
         
             
            every time an API call is made.
         
     | 
| 
       110 
114 
     | 
    
         | 
| 
      
 115 
     | 
    
         
            +
            Alternately, if you want to do ad-hoc debugging on single API calls you can pass
         
     | 
| 
      
 116 
     | 
    
         
            +
            in a debugging option:
         
     | 
| 
      
 117 
     | 
    
         
            +
             
     | 
| 
      
 118 
     | 
    
         
            +
            ``` ruby
         
     | 
| 
      
 119 
     | 
    
         
            +
            require 'invisiblehand'
         
     | 
| 
      
 120 
     | 
    
         
            +
             
     | 
| 
      
 121 
     | 
    
         
            +
            api = InvisibleHand::API.new :app_id => "id", :app_key => "key"
         
     | 
| 
      
 122 
     | 
    
         
            +
             
     | 
| 
      
 123 
     | 
    
         
            +
            api.products query: "nickelback", debug: true
         
     | 
| 
      
 124 
     | 
    
         
            +
            #=> Result is the same as normal, you'll just get verbose debugging output to
         
     | 
| 
      
 125 
     | 
    
         
            +
            #   the gem's internal logger (which you can override, see Logging above).
         
     | 
| 
      
 126 
     | 
    
         
            +
            ```
         
     | 
| 
      
 127 
     | 
    
         
            +
             
     | 
| 
       111 
128 
     | 
    
         
             
            ## Development
         
     | 
| 
       112 
129 
     | 
    
         | 
| 
       113 
130 
     | 
    
         
             
            To run tests, first you will need a valid `invisiblehand.yml` config file inside
         
     | 
    
        data/invisiblehand.sample.yml
    CHANGED
    
    | 
         @@ -1,11 +1,11 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            :app_id:  "your app id here"
         
     | 
| 
       2 
2 
     | 
    
         
             
            :app_key: "your app key here"
         
     | 
| 
       3 
3 
     | 
    
         | 
| 
       4 
     | 
    
         
            -
            # The  
     | 
| 
      
 4 
     | 
    
         
            +
            # The :region is the region of the API you would like to hit. The regions are
         
     | 
| 
       5 
5 
     | 
    
         
             
            # detailed in the developer documentation:
         
     | 
| 
       6 
6 
     | 
    
         
             
            #
         
     | 
| 
       7 
7 
     | 
    
         
             
            #   https://developer.getinvisiblehand.com/documentation#intro
         
     | 
| 
       8 
8 
     | 
    
         
             
            #
         
     | 
| 
       9 
9 
     | 
    
         
             
            # The option will default to the US if you do not explicitly specify it.
         
     | 
| 
       10 
10 
     | 
    
         
             
            #
         
     | 
| 
       11 
     | 
    
         
            -
            # : 
     | 
| 
      
 11 
     | 
    
         
            +
            # :region: us
         
     | 
    
        data/lib/invisiblehand/api.rb
    CHANGED
    
    | 
         @@ -44,7 +44,6 @@ module InvisibleHand 
     | 
|
| 
       44 
44 
     | 
    
         
             
                  end
         
     | 
| 
       45 
45 
     | 
    
         | 
| 
       46 
46 
     | 
    
         
             
                  @config[:protocol] = @config[:use_ssl] == false ? "http://" : "https://"
         
     | 
| 
       47 
     | 
    
         
            -
                  @config[:endpoint] ||= "us.api.invisiblehand.co.uk"
         
     | 
| 
       48 
47 
     | 
    
         
             
                end
         
     | 
| 
       49 
48 
     | 
    
         | 
| 
       50 
49 
     | 
    
         
             
                def products opts = {}
         
     | 
| 
         @@ -69,7 +68,7 @@ module InvisibleHand 
     | 
|
| 
       69 
68 
     | 
    
         | 
| 
       70 
69 
     | 
    
         
             
                def api_call method, path, opts = {}
         
     | 
| 
       71 
70 
     | 
    
         
             
                  query = url_params_from opts
         
     | 
| 
       72 
     | 
    
         
            -
                  url   = "#{@config[:protocol]}#{ 
     | 
| 
      
 71 
     | 
    
         
            +
                  url   = "#{@config[:protocol]}#{endpoint}#{path}?#{query}"
         
     | 
| 
       73 
72 
     | 
    
         | 
| 
       74 
73 
     | 
    
         
             
                  if opts[:debug]
         
     | 
| 
       75 
74 
     | 
    
         
             
                    debug { api_raw_request method, url }
         
     | 
| 
         @@ -80,6 +79,19 @@ module InvisibleHand 
     | 
|
| 
       80 
79 
     | 
    
         | 
| 
       81 
80 
     | 
    
         
             
                private
         
     | 
| 
       82 
81 
     | 
    
         | 
| 
      
 82 
     | 
    
         
            +
                # Gets the endpoint of the API to hit. Prioritises the :region config
         
     | 
| 
      
 83 
     | 
    
         
            +
                # parameter over :endpoint. In the event that neither are presents, defaults
         
     | 
| 
      
 84 
     | 
    
         
            +
                # to the US.
         
     | 
| 
      
 85 
     | 
    
         
            +
                def endpoint
         
     | 
| 
      
 86 
     | 
    
         
            +
                  if @config[:region]
         
     | 
| 
      
 87 
     | 
    
         
            +
                    "#{@config[:region]}.api.invisiblehand.co.uk"
         
     | 
| 
      
 88 
     | 
    
         
            +
                  elsif @config[:endpoint]
         
     | 
| 
      
 89 
     | 
    
         
            +
                    @config[:endpoint]
         
     | 
| 
      
 90 
     | 
    
         
            +
                  else
         
     | 
| 
      
 91 
     | 
    
         
            +
                    "us.api.invisiblehand.co.uk"
         
     | 
| 
      
 92 
     | 
    
         
            +
                  end
         
     | 
| 
      
 93 
     | 
    
         
            +
                end
         
     | 
| 
      
 94 
     | 
    
         
            +
             
     | 
| 
       83 
95 
     | 
    
         
             
                def debug &block
         
     | 
| 
       84 
96 
     | 
    
         
             
                  old_log_level = logger.level
         
     | 
| 
       85 
97 
     | 
    
         
             
                  logger.level  = ::Logger::DEBUG
         
     | 
    
        data/spec/api_spec.rb
    CHANGED
    
    | 
         @@ -5,86 +5,96 @@ describe InvisibleHand::API do 
     | 
|
| 
       5 
5 
     | 
    
         
             
              # yourself if you wish to run tests.
         
     | 
| 
       6 
6 
     | 
    
         
             
              let(:api_config) { File.join(File.dirname(__FILE__), 'invisiblehand.yml') }
         
     | 
| 
       7 
7 
     | 
    
         | 
| 
       8 
     | 
    
         
            -
               
     | 
| 
       9 
     | 
    
         
            -
             
     | 
| 
       10 
     | 
    
         
            -
             
     | 
| 
       11 
     | 
    
         
            -
             
     | 
| 
       12 
     | 
    
         
            -
             
     | 
| 
       13 
     | 
    
         
            -
              describe "#products" do
         
     | 
| 
       14 
     | 
    
         
            -
                subject    { api.products }
         
     | 
| 
       15 
     | 
    
         
            -
                it         { should be_a Hash }
         
     | 
| 
       16 
     | 
    
         
            -
                its(:keys) { should include "results" }
         
     | 
| 
       17 
     | 
    
         
            -
                its(:keys) { should include "info" }
         
     | 
| 
       18 
     | 
    
         
            -
              end
         
     | 
| 
      
 8 
     | 
    
         
            +
              ["uk", "us", "ca", "de"].each do |region|
         
     | 
| 
      
 9 
     | 
    
         
            +
                describe "Region: #{region}" do
         
     | 
| 
      
 10 
     | 
    
         
            +
                  let :api do
         
     | 
| 
      
 11 
     | 
    
         
            +
                    conf = YAML.load_file(api_config)
         
     | 
| 
      
 12 
     | 
    
         
            +
                    conf = conf.merge :region => region
         
     | 
| 
       19 
13 
     | 
    
         | 
| 
       20 
     | 
    
         
            -
             
     | 
| 
       21 
     | 
    
         
            -
             
     | 
| 
       22 
     | 
    
         
            -
                it      { should_not be_nil }
         
     | 
| 
       23 
     | 
    
         
            -
              end
         
     | 
| 
      
 14 
     | 
    
         
            +
                    InvisibleHand::API.new(conf)
         
     | 
| 
      
 15 
     | 
    
         
            +
                  end
         
     | 
| 
       24 
16 
     | 
    
         | 
| 
       25 
     | 
    
         
            -
             
     | 
| 
       26 
     | 
    
         
            -
             
     | 
| 
       27 
     | 
    
         
            -
                   
     | 
| 
       28 
     | 
    
         
            -
                  it      { should be_a Float }
         
     | 
| 
       29 
     | 
    
         
            -
                end
         
     | 
| 
      
 17 
     | 
    
         
            +
                  let(:product)    { api.products["results"].first }
         
     | 
| 
      
 18 
     | 
    
         
            +
                  let(:product_id) { product["id"] }
         
     | 
| 
      
 19 
     | 
    
         
            +
                  let(:page)       { product["best_page"] }
         
     | 
| 
       30 
20 
     | 
    
         | 
| 
       31 
     | 
    
         
            -
             
     | 
| 
       32 
     | 
    
         
            -
             
     | 
| 
       33 
     | 
    
         
            -
             
     | 
| 
       34 
     | 
    
         
            -
             
     | 
| 
       35 
     | 
    
         
            -
             
     | 
| 
      
 21 
     | 
    
         
            +
                  describe "#products" do
         
     | 
| 
      
 22 
     | 
    
         
            +
                    subject    { api.products }
         
     | 
| 
      
 23 
     | 
    
         
            +
                    it         { should be_a Hash }
         
     | 
| 
      
 24 
     | 
    
         
            +
                    its(:keys) { should include "results" }
         
     | 
| 
      
 25 
     | 
    
         
            +
                    its(:keys) { should include "info" }
         
     | 
| 
      
 26 
     | 
    
         
            +
                  end
         
     | 
| 
       36 
27 
     | 
    
         | 
| 
       37 
     | 
    
         
            -
             
     | 
| 
       38 
     | 
    
         
            -
             
     | 
| 
       39 
     | 
    
         
            -
                   
     | 
| 
       40 
     | 
    
         
            -
             
     | 
| 
       41 
     | 
    
         
            -
                  end.to_not raise_error
         
     | 
| 
       42 
     | 
    
         
            -
                end
         
     | 
| 
       43 
     | 
    
         
            -
              end
         
     | 
| 
      
 28 
     | 
    
         
            +
                  describe "#product" do
         
     | 
| 
      
 29 
     | 
    
         
            +
                    subject { product }
         
     | 
| 
      
 30 
     | 
    
         
            +
                    it      { should_not be_nil }
         
     | 
| 
      
 31 
     | 
    
         
            +
                  end
         
     | 
| 
       44 
32 
     | 
    
         | 
| 
       45 
     | 
    
         
            -
             
     | 
| 
       46 
     | 
    
         
            -
             
     | 
| 
       47 
     | 
    
         
            -
             
     | 
| 
       48 
     | 
    
         
            -
             
     | 
| 
       49 
     | 
    
         
            -
             
     | 
| 
       50 
     | 
    
         
            -
                end
         
     | 
| 
       51 
     | 
    
         
            -
              end
         
     | 
| 
      
 33 
     | 
    
         
            +
                  describe "#live_price" do
         
     | 
| 
      
 34 
     | 
    
         
            +
                    describe "with live price url" do
         
     | 
| 
      
 35 
     | 
    
         
            +
                      subject { api.live_price(page["live_price_url"]) }
         
     | 
| 
      
 36 
     | 
    
         
            +
                      it      { should be_a Float }
         
     | 
| 
      
 37 
     | 
    
         
            +
                    end
         
     | 
| 
       52 
38 
     | 
    
         | 
| 
       53 
     | 
    
         
            -
             
     | 
| 
       54 
     | 
    
         
            -
             
     | 
| 
       55 
     | 
    
         
            -
                   
     | 
| 
       56 
     | 
    
         
            -
                     
     | 
| 
       57 
     | 
    
         
            -
                      api.product "not a real id at all, lol"
         
     | 
| 
       58 
     | 
    
         
            -
                    end.to raise_error InvisibleHand::Error::APIError
         
     | 
| 
      
 39 
     | 
    
         
            +
                    describe "with vanilla page url" do
         
     | 
| 
      
 40 
     | 
    
         
            +
                      subject { api.live_price(page["original_url"]) }
         
     | 
| 
      
 41 
     | 
    
         
            +
                      it      { should be_a Float }
         
     | 
| 
      
 42 
     | 
    
         
            +
                    end
         
     | 
| 
       59 
43 
     | 
    
         
             
                  end
         
     | 
| 
       60 
     | 
    
         
            -
                end
         
     | 
| 
       61 
44 
     | 
    
         | 
| 
       62 
     | 
    
         
            -
             
     | 
| 
       63 
     | 
    
         
            -
             
     | 
| 
       64 
     | 
    
         
            -
             
     | 
| 
       65 
     | 
    
         
            -
             
     | 
| 
       66 
     | 
    
         
            -
             
     | 
| 
      
 45 
     | 
    
         
            +
                  describe "ad-hoc debug flag" do
         
     | 
| 
      
 46 
     | 
    
         
            +
                    specify "the debug option to a single call should not break things" do
         
     | 
| 
      
 47 
     | 
    
         
            +
                      expect do
         
     | 
| 
      
 48 
     | 
    
         
            +
                        api.live_price(page["original_url"], :debug => true)
         
     | 
| 
      
 49 
     | 
    
         
            +
                      end.to_not raise_error
         
     | 
| 
      
 50 
     | 
    
         
            +
                    end
         
     | 
| 
       67 
51 
     | 
    
         
             
                  end
         
     | 
| 
       68 
     | 
    
         
            -
                end
         
     | 
| 
       69 
     | 
    
         
            -
              end
         
     | 
| 
       70 
52 
     | 
    
         | 
| 
       71 
     | 
    
         
            -
             
     | 
| 
       72 
     | 
    
         
            -
             
     | 
| 
       73 
     | 
    
         
            -
             
     | 
| 
       74 
     | 
    
         
            -
             
     | 
| 
      
 53 
     | 
    
         
            +
                  describe "invalid config" do
         
     | 
| 
      
 54 
     | 
    
         
            +
                    specify "no app_id or api_key should throw error" do
         
     | 
| 
      
 55 
     | 
    
         
            +
                      expect do
         
     | 
| 
      
 56 
     | 
    
         
            +
                        InvisibleHand::API.new
         
     | 
| 
      
 57 
     | 
    
         
            +
                      end.to raise_error InvisibleHand::Error::InvalidConfig
         
     | 
| 
      
 58 
     | 
    
         
            +
                    end
         
     | 
| 
      
 59 
     | 
    
         
            +
                  end
         
     | 
| 
       75 
60 
     | 
    
         | 
| 
       76 
     | 
    
         
            -
             
     | 
| 
       77 
     | 
    
         
            -
             
     | 
| 
       78 
     | 
    
         
            -
             
     | 
| 
       79 
     | 
    
         
            -
             
     | 
| 
      
 61 
     | 
    
         
            +
                  describe "invalid api calls" do
         
     | 
| 
      
 62 
     | 
    
         
            +
                    describe "#product" do
         
     | 
| 
      
 63 
     | 
    
         
            +
                      specify "should throw InvisibleHand::Error::APIError on invalid ID" do
         
     | 
| 
      
 64 
     | 
    
         
            +
                        expect do
         
     | 
| 
      
 65 
     | 
    
         
            +
                          api.product "not a real id at all, lol"
         
     | 
| 
      
 66 
     | 
    
         
            +
                        end.to raise_error InvisibleHand::Error::APIError
         
     | 
| 
      
 67 
     | 
    
         
            +
                      end
         
     | 
| 
       80 
68 
     | 
    
         
             
                    end
         
     | 
| 
       81 
69 
     | 
    
         | 
| 
       82 
     | 
    
         
            -
                     
     | 
| 
      
 70 
     | 
    
         
            +
                    describe "#live_price" do
         
     | 
| 
      
 71 
     | 
    
         
            +
                      specify "should throw InvisibleHand::Error::APIError on invalid URL" do
         
     | 
| 
      
 72 
     | 
    
         
            +
                        expect do
         
     | 
| 
      
 73 
     | 
    
         
            +
                          api.live_price "not a real url, rofl"
         
     | 
| 
      
 74 
     | 
    
         
            +
                        end.to raise_error InvisibleHand::Error::APIError
         
     | 
| 
      
 75 
     | 
    
         
            +
                      end
         
     | 
| 
      
 76 
     | 
    
         
            +
                    end
         
     | 
| 
       83 
77 
     | 
    
         
             
                  end
         
     | 
| 
       84 
78 
     | 
    
         | 
| 
       85 
     | 
    
         
            -
                   
     | 
| 
       86 
     | 
    
         
            -
             
     | 
| 
       87 
     | 
    
         
            -
             
     | 
| 
      
 79 
     | 
    
         
            +
                  describe "errors" do
         
     | 
| 
      
 80 
     | 
    
         
            +
                    describe InvisibleHand::Error::APIError do
         
     | 
| 
      
 81 
     | 
    
         
            +
                      subject do
         
     | 
| 
      
 82 
     | 
    
         
            +
                        error = nil
         
     | 
| 
      
 83 
     | 
    
         
            +
             
     | 
| 
      
 84 
     | 
    
         
            +
                        begin
         
     | 
| 
      
 85 
     | 
    
         
            +
                          api.live_price "not a real url"
         
     | 
| 
      
 86 
     | 
    
         
            +
                        rescue InvisibleHand::Error::APIError => e
         
     | 
| 
      
 87 
     | 
    
         
            +
                          error = e
         
     | 
| 
      
 88 
     | 
    
         
            +
                        end
         
     | 
| 
      
 89 
     | 
    
         
            +
             
     | 
| 
      
 90 
     | 
    
         
            +
                        error
         
     | 
| 
      
 91 
     | 
    
         
            +
                      end
         
     | 
| 
      
 92 
     | 
    
         
            +
             
     | 
| 
      
 93 
     | 
    
         
            +
                      its(:url)          { should be_a String }
         
     | 
| 
      
 94 
     | 
    
         
            +
                      its(:raw_response) { should be_a String }
         
     | 
| 
      
 95 
     | 
    
         
            +
                      its(:message)      { should be_a String }
         
     | 
| 
      
 96 
     | 
    
         
            +
                    end
         
     | 
| 
      
 97 
     | 
    
         
            +
                  end
         
     | 
| 
       88 
98 
     | 
    
         
             
                end
         
     | 
| 
       89 
99 
     | 
    
         
             
              end
         
     | 
| 
       90 
100 
     | 
    
         
             
            end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: invisiblehand
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.1.3
         
     | 
| 
       5 
5 
     | 
    
         
             
              prerelease: 
         
     | 
| 
       6 
6 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       7 
7 
     | 
    
         
             
            authors:
         
     | 
| 
         @@ -9,7 +9,7 @@ authors: 
     | 
|
| 
       9 
9 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       10 
10 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       11 
11 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       12 
     | 
    
         
            -
            date: 2013-02- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2013-02-26 00:00:00.000000000 Z
         
     | 
| 
       13 
13 
     | 
    
         
             
            dependencies:
         
     | 
| 
       14 
14 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       15 
15 
     | 
    
         
             
              name: rest-client
         
     | 
| 
         @@ -110,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       110 
110 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       111 
111 
     | 
    
         
             
                  segments:
         
     | 
| 
       112 
112 
     | 
    
         
             
                  - 0
         
     | 
| 
       113 
     | 
    
         
            -
                  hash:  
     | 
| 
      
 113 
     | 
    
         
            +
                  hash: 1556147975806628177
         
     | 
| 
       114 
114 
     | 
    
         
             
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
       115 
115 
     | 
    
         
             
              none: false
         
     | 
| 
       116 
116 
     | 
    
         
             
              requirements:
         
     | 
| 
         @@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       119 
119 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       120 
120 
     | 
    
         
             
                  segments:
         
     | 
| 
       121 
121 
     | 
    
         
             
                  - 0
         
     | 
| 
       122 
     | 
    
         
            -
                  hash:  
     | 
| 
      
 122 
     | 
    
         
            +
                  hash: 1556147975806628177
         
     | 
| 
       123 
123 
     | 
    
         
             
            requirements: []
         
     | 
| 
       124 
124 
     | 
    
         
             
            rubyforge_project: 
         
     | 
| 
       125 
125 
     | 
    
         
             
            rubygems_version: 1.8.24
         
     |