Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Tuesday, June 9, 2015

Using Peddler to Access Amazon Marketplace Web Service (MWS)

Peddler is a Ruby Gem that can be used to access Amazon Marketplace Web Service (Amazon MWS) from Ruby. The following are some Ruby snippets that can be used to write a Ruby script that accesses MWS or to make ad-hoc queries against MWS from within IRb (Interactive Ruby.

Install Peddler Gem:

gem install peddler
Successfully installed excon-0.45.3
Successfully installed jeff-1.3.0
Successfully installed peddler-0.16.0

You may have to use sudo, depending on how you installed RubyGems.

Setup the Peddler client instance:

require 'peddler'

# Setup client
client = MWS::Orders::Client.new({
  :primary_marketplace_id => "ATVPD00000000",                             # Marketplace ID
  :merchant_id            => "A1UX7000000000",                            # Seller ID
  :aws_access_key_id      => "AKIAJ000000000000000",                      # AWS Access Key ID
  :aws_secret_access_key  => "fT+tcCTUBUsd7w00000000000000000000000000"   # Secret Key
})

# Setup error callback. This helps debugging Amazon API error messages.
client.on_error{|req,resp| puts resp.body }

# Get API Status
puts client.get_service_status

The following are some of the ways you can query MWS orders:

# Retrieve orders. CreatedAfter or LastUpdatedAfter must be specified.
# - created_after
# - created_before
# - last_updated_after
# - last_updated_before
# - order_status  (Unshipped, Shipped, Canceled)

resp1 = client.list_orders(:created_after => '2015-05-01')
puts resp1.body

resp2 = client.list_orders(:created_after => '2015-05-01', :order_status => ['Unshipped', 'PartiallyShipped'])
puts resp2.body

resp3 = client.list_orders(:created_after => '2015-05-01', :order_status => ['Shipped'])
puts resp3.body

These are some documentation pages that were helpful when investigating Peddler:

Peddler
https://github.com/hakanensari/peddler
Peddler API Docs
MWS::Orders::Client
MWS::Orders::Client#list_orders
Amazon API Docs
List Orders

Friday, October 24, 2014

Images in Active Admin Index pages.

An example of how images can be embedded in Active Admin index pages in a Ruby on Rails application:
ActiveAdmin.register ColorGroup do
  index :download_links => [:csv] do
    selectable_column
    column(:id) { |it| auto_link it, it.id }
    column(:name) { |it| auto_link it, it.name }
    column(:slug) { |it| auto_link it, it.slug }
    column(:family) { |it| auto_link it.color_family, it.color_family.name if it.color_family.present? }
    column :asset_url
    column :order
    column :image do |it|
      if it.nil?
        "N/A"
      else
        # Use image_tag here...
        link_to sanitize("<img src=\"#{it.asset_url}\" class=\"cgs-img\" width=\"101\" height=\"24\" />"), it.asset_url
      end
    end
    actions
  end
end

Tuesday, November 23, 2010

Configuring Aptana to Treat Gemfiles as Ruby Files

Aptana Studio 2 does not recognize Gemfiles used by Bundler as Ruby files. This means that Aptana will treat the Gemfile you are editing as an ordinary plain text file. You will not have Ruby syntax highlighting, word completion, and syntax checking available within the editor. In order to specify that the Gemfile (or any other file for that matter) is a Ruby file, do the following:
  1. Open the "Window Menu" --> Preferences
  2. Go to General --> "Content Types"
  3. Expand "Text" node under "Content types"
  4. Select "Ruby Source File"
  5. Click the "Add..." button and enter "Gemfile" as the content type.
  6. Click the "OK" button to save your changes.



The above procedure can be used to add files for the following content types:
  • CSS Source Files
  • ERB Source Files
  • HAML Source Files
  • HTML Source Files
  • JAR Manifest Files
  • Java Property Files
  • Java Source Files
  • JavaScript Source Files
  • SASS Source Files
  • XML Build Files (Ant, etc)
  • XML Source Files (xml, xslt, etc)

Friday, March 26, 2010

Nondiscriptive Rail Error "Errors running test:units!"

Errors Running test:units!


The "Errors running test:units!" error while creating a brand new Rails application under Cygwin:

rails world_app -d mysql
cd world_app


$ rake test

This results in the following error message:

(in /cygdrive/d/workspace/tt/world_app)
/usr/bin/ruby.exe -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"
/usr/bin/ruby.exe -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"
Errors running test:units!

Attempting to execute the Rails tests results in the same eror:

$ rake test --trace
(in /cygdrive/d/workspace/tt/world_app)
** Invoke test (first_time)
** Execute test
** Invoke test:units (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Invoke test:functionals (first_time)
** Invoke db:test:prepare
** Execute test:functionals
/usr/bin/ruby.exe -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"
** Invoke test:integration (first_time)
** Invoke db:test:prepare
** Execute test:integration
/usr/bin/ruby.exe -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"
Errors running test:units!


Fixing the Error


The following steps fixed the error:

  1. Edit config/database.yml
  2. Make sure the db password is correct.
  3. Make sure the db is created.
  4. Change "host: localhost" to "host: 127.0.0.1" if need be.


$ rake test --trace
(in /cygdrive/d/workspace/tt/world_app)
** Invoke test (first_time)
** Execute test
** Invoke test:units (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:load
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
** Execute test:units
/usr/bin/ruby.exe -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"
** Invoke test:functionals (first_time)
** Invoke db:test:prepare
** Execute test:functionals
/usr/bin/ruby.exe -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"
** Invoke test:integration (first_time)
** Invoke db:test:prepare
** Execute test:integration
/usr/bin/ruby.exe -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"

Thursday, October 15, 2009

Rails Error: "interning empty string"

The "interning empty string" error kept popping up in some of our code written prior to upgrading to Ruby on Rails 2.3.4.
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/attribute_methods.rb:344:in `respond_to?'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/attribute_methods.rb:344:in `respond_to?'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:40:in `value'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:79:in `generate_message'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:30:in `message'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:34:in `full_message'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:275:in `full_messages'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:275:in `map'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:275:in `full_messages'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2036:in `inject'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:274:in `each'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:274:in `inject'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:274:in `full_messages'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_view/helpers/active_record_helper.rb:201:in `error_messages_for'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/core_ext/enumerable.rb:59:in `map'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/core_ext/enumerable.rb:59:in `sum'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_view/helpers/active_record_helper.rb:201:in `error_messages_for'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/core_ext/object/misc.rb:78:in `with_options'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_view/helpers/active_record_helper.rb:192:in `error_messages_for'
C:/workspace/epanel_latest1/app/helpers/application_helper.rb:184:in `error_messages_for'
C:/workspace/epanel_latest1/app/views/login_users/login.html.erb:60:in `_run_erb_app47views47login_users47login46html46erb'

The culprit turned out to be:
model.errors.add('', 'Some message')
and the fix was to change this to
model.errors.add_to_base('Some message')

Wednesday, October 7, 2009

Rails: undefined method `use_transactional_fixtures=' in Rails 2.3

Problem: You try to run your Rails tests under Rails 2.3 and you run across the "undefined method `use_transactional_fixtures='" error.
./test/integration/../test_helper.rb:29: undefined method `use_transactional_fixtures=' for Test::Unit::TestCase:Class (NoMethodError)
        from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
        from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
        from ./test/integration/survey_user_interface_test.rb:1
        from /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `load'
        from /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5
        from /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `each'
        from /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5
rake aborted!
Command failed with status (1): [/usr/bin/ruby.exe -I"lib:test" "/usr/lib/r...]

Fix:
Change the class definition in test_helper.rb from
class Test::Unit::TestCase
to
class ActiveSupport::TestCase

Wednesday, September 2, 2009

MySQL Error: Can't connect to local MySQL server through socket

You try to run your Rails project and keep getting the "Can't connect to local MySQL server through socket" MySQL Error.
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/mysql_adapter.rb:548:in `real_connect'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/mysql_adapter.rb:548:in `connect'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/mysql_adapter.rb:198:in `initialize'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/mysql_adapter.rb:74:in `new'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/mysql_adapter.rb:74:in `mysql_connection'

The quickest way to fix this issue is to edit the database.yml file and replace localhost with 127.0.0.1.

Friday, July 24, 2009

Crop Images in Batches Using ImageMagick and Ruby

Let us suppose you have an image named caps-1.png and you want to crop it to a 450x760 area, where top left corner of the crop region is the point (613,130) in the original image. You can easily do this by using the convert command that comes with ImageMagick. The command you might want to use might look like this:

convert "caps-1.png" -crop "450x760+613+130" "out/caps-1.png"

The numbers in the above are:
(613,130)  - top left corner
450x760    - the crop area (x,y)

Now let us suppose you have a directory full of images that need to cropped to the same dimensions as above at the same point as above. You can use a little Ruby snippet to execute the same convert command as above, but for each file in the current directory.

Dir.foreach('.'){|f| system  'convert \"%s\" -crop \"450x760+613+130\" \"out/%s\"' %[f, f.downcase] if f.downcase =~ /\.png$/}

This can be executed from the command line through the ruby interpreter:
ruby -e "Dir.foreach('.'){|f| system  'convert \"%s\" -crop \"450x760+613+130\" \"out/%s\"' %[f, f.downcase] if f.downcase =~ /\.png$/}"

If you want to simply generate script file with a series of convert commands, then simply replace the Ruby "system" statement with a "puts" statement:
ruby -e "Dir.foreach('.'){|f| puts    'convert \"%s\" -crop \"450x760+613+130\" \"out/%s\"' %[f, f.downcase] if f.downcase =~ /\.png$/}" > convert_files.sh

There the puts statements write the convert commands with parameters to standard out (STDOUT), and that output is redirected to the file convert_files.sh. Now you can set the executable bit of the script file
chmod +x convert_files.sh
and execute it over and over again.

If your source files are, say, PNG files but you want the convert command to save the output as JPEG files, use the following:
ruby -e "Dir.foreach('.'){|f| system  'convert \"%s\" -crop \"450x760+613+130\" \"out/%s\"' %[f, f.downcase.sub('.png', '.jpg')] if f.downcase =~ /\.png$/}"

For more information take a look at the "Command Line Processing" section of the ImageMagick manual:
http://www.imagemagick.org/script/command-line-processing.php#geometry


The commands listed here were used to extract the areas of interest of frames grabbed from an iPhone application demo video.