{"id":135,"date":"2014-10-30T10:59:38","date_gmt":"2014-10-30T01:59:38","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2014\/10\/30\/active-resourrce-patch"},"modified":"2014-10-30T10:59:38","modified_gmt":"2014-10-30T01:59:38","slug":"active-resourrce-patch","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2014\/10\/30\/active-resourrce-patch\/","title":{"rendered":"ActiveResource\u3067GET\u9001\u4fe1\u6642\u306b\u914d\u5217\u3067\u6e21\u3059"},"content":{"rendered":"

\u5148\u65e5\u793e\u5185\u306e\u300c\u696d\u52d9\u52b9\u7387\u5316\u5468\u8fba\u30b0\u30c3\u30ba\u8cfc\u5165\u88dc\u52a9\u300d\u5236\u5ea6\u3092\u5229\u7528\u3057\u3066\u3001mac\u7528\u306b\u30e2\u30d0\u30a4\u30eb\u5145\u96fb\u5668HyperJuice\u3092\u8cfc\u5165\u3057\u3001\u914d\u7dda\u5730\u7344\u306b\u306a\u3063\u3066\u3044\u308b\u524d\u7530\u3067\u3059\u3002 <\/p>\n

\u672c\u65e5\u306fRuby On Rails\u306eGem\u3001AcitiveResource\u3092\u89e6\u3063\u3066\u307f\u305f\u306e\u3067\u3001\u305d\u306e\u304a\u8a71\u3092\u3057\u307e\u3059\u3002 <\/p>\n

\u74b0\u5883
Rails 4.1.5\u3001ruby 2.1.2\u3001activeresource-4.0.0 <\/p>\n

ActiveResorce\u3068\u306f\u300cWeb\u4e0a\u306eRESTfulAPI\u3092ActiveRecord\u306e\u30e2\u30c7\u30eb\u3068\u540c\u3058\u3088\u3046\u306a\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3067\u5229\u7528\u53ef\u80fd\u306b\u3059\u308b\u300d\u3068\u3044\u3046Gem\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002
webAPI\u3092ActiveResource\u3068\u540c\u3058\u3088\u3046\u306b\u64cd\u4f5c\u51fa\u6765\u305f\u3089\u4fbf\u5229\u3067\u3059\u306d\u3002\u3000\u3000<\/p>\n

\u4eca\u56de\u3084\u308a\u305f\u3044\u3068\u601d\u3063\u305f\u306e\u306f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306bGET\u3067\u53e9\u304f\u6642\u306b\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u914d\u5217\u3067\u6e21\u3057\u305f\u3044\u3068\u601d\u3063\u305f\u304b\u3089\u3067\u3059\u3002 <\/p>\n

http:\/\/hoge\/api\/foo?piyo[]=hoge1&piyo[]=hoge2  <\/code><\/pre>\n

active_resource\u3067\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u6e21\u3059\u6642\u306f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u8a2d\u5b9a\u3059\u308b\u3068\u601d\u3044\u307e\u3059\u3002 <\/p>\n

Foo.find(:all, from:'api\/foo', params: { piyo1: :hoge1, piyo2: :hoge2 } )  <\/code><\/pre>\n

\u3053\u308c\u3067\u5b9f\u884c\u3059\u308b\u3068\u3001 <\/p>\n

api\/foo?piyo1=hoge1&piyo2=hoge2  <\/code><\/pre>\n

\u3068\u3044\u3046GET\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u51fa\u6765\u4e0a\u304c\u308a\u307e\u3059\u3002
\u3057\u304b\u3057\u3001\u914d\u5217\u5f62\u5f0f\u3067\u6e21\u3057\u305f\u3044\u304b\u3089\u3068\u3044\u3063\u3066\u4e0b\u8a18\u306e\u3088\u3046\u306b\u6e21\u3059\u3068\u3001<\/p>\n

Foo.find(:all, from:'api\/foo', params: { piyo[]: :hoge1, piyo[]: :hoge2 } )  <\/code><\/pre>\n

\u30a8\u30e9\u30fc\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002
\u4eca\u56de\u306f\u3053\u308c\u3092\u89e3\u6c7a\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

\u3067\u306f\u3001ActiveResource\u306e\u4e2d\u306e\u51e6\u7406\u3092\u9806\u756a\u306b\u898b\u3066\u3044\u304d\u307e\u3059\u3002
\nfind\u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u308b\u306e\u306factive_resource\/base.rb\u3067\u3059\u3002 <\/p>\n

active_resource\/base.rb<\/p>\n

  def find(*arguments)\n    scope   = arguments.slice!(0)\n    options = arguments.slice!(0) || {}\n     case scope\n      when :all   then find_every(options)\n      when :first then find_every(options).first\n      when :last  then find_every(options).last\n      when :one   then find_one(options)\n      else             find_single(scope, options)\n    end\n  end<\/code><\/pre>\n

858\u884c\u76ee\u3067\u3059\u306d\u3002
\u691c\u7d22\u306f\u3053\u3053\u304c\u8d77\u70b9\u306b\u306a\u308a\u307e\u3059\u3002 <\/p>\n

\u7b2c\u4e00\u5f15\u6570\u3092:all\u3067\u6e21\u3059\u306e\u3067\u3001
when :all then find_every(options)
\u306e\u51e6\u7406\u306b\u306a\u308a\u307e\u3059\u3002 <\/p>\n

\u305d\u3057\u3066find_every\u30e1\u30bd\u30c3\u30c9\u306f\u4ee5\u4e0b\u3067\u3059\u3002 <\/p>\n

  # Find every resource\n  def find_every(options)\n    begin\n      case from = options[:from]\n      when Symbol\n        instantiate_collection(get(from, options[:params]), options[:params])\n      when String\n        path = "#{from}#{query_string(options[:params])}"\n        instantiate_collection(format.decode(connection.get(path, headers).body) || [], options[:params])\n      else\n        prefix_options, query_options = split_options(options[:params])\n        path = collection_path(prefix_options, query_options)\n        instantiate_collection( (format.decode(connection.get(path, headers).body) || []), query_options, prefix_options )\n      end\n    rescue ActiveResource::ResourceNotFound\n      # Swallowing ResourceNotFound exceptions and return nil - as per\n      # ActiveRecord.\n      nil\n    end\n  end<\/code><\/pre>\n

\u4eca\u56de\u306f
\nfind(:all, from:'api\/com', params: params )
\n\u3068from\u3092String\u3067\u6e21\u3057\u3066\u3044\u308b\u306e\u3067\u3001<\/p>\n

  when String\n    path = "#{from}#{query_string(options[:params])}"\n    instantiate_collection(format.decode(connection.get(path, headers).body) || [], options[:params])\n  else<\/code><\/pre>\n

\u306e\u4e2d\u306e\u51e6\u7406\u306b\u306a\u308a\u307e\u3059\u3002 <\/p>\n

\u3053\u3053\u3067path\u3092\u751f\u6210\u3057\u307e\u3059\u304c\u3001\u5148\u307b\u3069\u6e21\u3057\u305f from:'api\/foo', params: params \u304b\u3089path\u3092\u751f\u6210\u3057\u3066\u3044\u307e\u3059\u3002
from\u306e\u307b\u3046\u306f\u3044\u3044\u3067\u3059\u304c\u3001:params\u3000\u306e\u307b\u3046\u3067\u3059\u306d\u3002 <\/p>\n

query_string()\u30e1\u30bd\u30c3\u30c9\u304c\u4f55\u3092\u3057\u3066\u3044\u308b\u304b\u898b\u307e\u3059\u3002 <\/p>\n

  # Builds the query string for the request.\n  def query_string(options)\n    "?#{options.to_query}" unless options.nil? || options.empty?\n  end<\/code><\/pre>\n

\u6e21\u3057\u305foptions\u306b\u3001to_query\u3092\u3057\u3066\u3044\u308b\u3060\u3051\u3067\u3059\u306d\u3002
\u3053\u3053\u304c\u30a8\u30e9\u30fc\u306e\u539f\u56e0\u3067\u3059\u3002
rails console\u3067\u8a66\u3057\u3066\u307f\u307e\u3059\u3002 <\/p>\n

# Hash\u3067\u6e21\u3059\n2.1.2 :021 > {piyo1: :hoge1, piyo2: :hoge2}.to_query\n => "piyo1=hoge1&piyo2=hoge2"\n\n# Hash\u306b[]\u3092\u4ed8\u3051\u3066\u6e21\u3059\n2.1.2 :055 > {piyo1[]: :hoge1, piyo2[]: :hoge2}.to_query\nSyntaxError: (irb):55: syntax error, unexpected ':', expecting =>\n\n# \u30a8\u30b9\u30b1\u30fc\u30d7\u3067\u304d\u306a\u3044\u304b\u8a66\u3059\n2.1.2 :057 >   {piyo1[]: :hoge1, piyo2[]: :hoge2}.to_query\nSyntaxError: (irb):57: syntax error, unexpected $undefined, expecting keyword_do or '{' or '('\n\n# Array\u306e\u5834\u5408\n2.1.2 :003 >   ['hoge1', 'hoge2'].to_query\nArgumentError: wrong number of arguments (0 for 1)\n\n# String\u306e\u5834\u5408\n2.1.2 :001 > 'hoge1, hoge2'.to_query\nArgumentError: wrong number of arguments (0 for 1)<\/code><\/pre>\n

\u306a\u306e\u3067\u3001\u3053\u3053\u306e\u51e6\u7406\u306b\u30d1\u30c3\u30c1\u3092\u3042\u3066\u307e\u3059\u3002 <\/p>\n

\u5b9f\u306fto_query\u306f\u4fbf\u5229\u306a\u30e1\u30bd\u30c3\u30c9\u3067\u5f15\u6570\u3092\u6e21\u3057\u3066\u3042\u3052\u308b\u3068\u914d\u5217\u304b\u3089\u30af\u30a8\u30ea\u5f62\u5f0f\u306b\u7d44\u307f\u7acb\u3066\u3066\u304f\u308c\u307e\u3059\u3002 <\/p>\n

2.1.2 :006 > ['hoge1', 'hoge2'].to_query('piyo')\n => "piyo%5B%5D=hoge1&piyo%5B%5D=hoge2"<\/code><\/pre>\n

%5B%5D\u306f[]\u306e\u30a8\u30b9\u30b1\u30fc\u30d7\u3067\u3059\u3002
\u5b9f\u969b\u306eURL\u3092\u53e9\u304f\u6642\u306b\u306fpiyo[]=hoge1&piyo[]=hoge2\u3000\u3000
\n\u3068\u540c\u3058\u52d5\u4f5c\u3092\u3057\u307e\u3059\u3002 <\/p>\n

\u306a\u306e\u3067\u3001\u7d50\u8ad6\u3068\u3057\u3066\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3057\u307e\u3059\u3002 <\/p>\n

\/app\/model\/foo.rb <\/p>\n

require 'active_resource'\n\nclass Foo < ActiveResource::Base\n\n  self.site = 'http:\/\/hoge'\n\n  def self.query_string(options)\n    "?#{options.to_query('piyo')}" unless options.nil? || options.empty?\n  end\nend<\/code><\/pre>\n

\u3053\u306e\u307e\u307e\u3060\u3068\u3053\u306e\u30af\u30e9\u30b9\u306e\u307f\u306e\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3067\u3001\u3053\u308c\u3067\u3082\u826f\u3044\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u3082\u3046\u5c11\u3057\u51e1\u5eb8\u5316\u3057\u3066\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u5168\u4f53\u3067\u4f7f\u3048\u308b\u3088\u3046\u306b\u30d1\u30c3\u30c1\u3092\u3042\u3066\u3066\u307f\u307e\u3059\u3002 <\/p>\n

\/config\/initializers\/active_resource_pach.rb<\/p>\n

require 'active_resource'\nclass ActiveResource::Base\n  class << self\n    def query_string(options)\n      if tag && options.instance_of?(Array)\n        "?#{options.to_query(tag)}" unless options.nil? || options.empty?\n      elsif options.instance_of?(Hash)\n        "?#{options.to_query}" unless options.nil? || options.empty?\n      elsif options.instance_of?(String)\n        "?#{options}" unless options.blank?\n      else\n        raise ArgumentError\n      end\n    end\n\n    def tag\n      return @tag if defined?(@tag)\n      nil\n    end\n\n    def tag=(tag)\n      @tag = tag\n    end\n  end\nend<\/code><\/pre>\n

Hash\u3067\u306e\u51e6\u7406\u306f\u4eca\u307e\u3067\u3069\u304a\u308a\u306b\u3057\u3066\u3044\u307e\u3059\u3002
query_string\u30e1\u30bd\u30c3\u30c9\u306fbase.rb\u306e\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u30e1\u30bd\u30c3\u30c9\u3067\u3001base.rb\u306e\u4e2d\u3067\u4f7f\u7528\u3055\u308c\u3066\u3044\u308b\u5225\u306e\u7b87\u6240\u3092\u898b\u3066\u307f\u307e\u3057\u305f\u304c\u3001\u4e0a\u8a18\u306e\u5b9f\u88c5\u3067\u5f71\u97ff\u306f\u7121\u3044\u3068\u601d\u3044\u307e\u3059\u3002 <\/p>\n

\/app\/models\/foo.rb <\/p>\n

require 'active_resource'\n\nclass Foo < ActiveResource::Base\n\n  self.site = 'http:\/\/hoge'\n  self.tag = 'piyo'\n\nend<\/code><\/pre>\n

\/app\/controllers\/foo_controller.rb <\/p>\n

class FooController < ApplicationController\n\n  def index\n    @foo = Foo.find(:all, from: '\/api\/foo', params: ['hoge1', 'hoge2'])\n    render :template => "foo\/index"\n  rescue => e\n    render :text => e.message\n  end\nend<\/code><\/pre>\n

\u3053\u3093\u306a\u611f\u3058\u3067\u5916\u306e\u30e1\u30bd\u30c3\u30c9\u306b\u3082\u30d1\u30c3\u30c1\u3092\u5f53\u3066\u3066\u3044\u3051\u307e\u3059\u306d\u3002
\u30d1\u30c3\u30c1\u3060\u3089\u3051\u3060\u3068\u3001ActiveResource\u3092\u4f7f\u3046\u610f\u5473\u304c\u3042\u307e\u308a\u7121\u304f\u306a\u308a\u305d\u3046\u3067\u3059\u304c\u3002 <\/p>\n

\u4eca\u56de\u53d6\u308a\u4e0a\u3052\u305fActiveResource\u306e\u30bd\u30fc\u30b9\u306e\u5185\u8a33\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002 <\/p>\n

$ activeresource-4.0.0\n\u251c\u2500\u2500 README.rdoc\n\u2514\u2500\u2500 lib\n    \u251c\u2500\u2500 active_resource\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 associations\n    \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u2514\u2500\u2500 builder\n    \u2502\u00a0\u00a0 \u2502\u00a0\u00a0     \u251c\u2500\u2500 association.rb\n    \u2502\u00a0\u00a0 \u2502\u00a0\u00a0     \u251c\u2500\u2500 belongs_to.rb\n    \u2502\u00a0\u00a0 \u2502\u00a0\u00a0     \u251c\u2500\u2500 has_many.rb\n    \u2502\u00a0\u00a0 \u2502\u00a0\u00a0     \u2514\u2500\u2500 has_one.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 associations.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 base.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 callbacks.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 collection.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 connection.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 custom_methods.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 exceptions.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 formats\n    \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u251c\u2500\u2500 json_format.rb\n    \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u2514\u2500\u2500 xml_format.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 formats.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 http_mock.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 log_subscriber.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 observing.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 railtie.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 reflection.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 schema.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 singleton.rb\n    \u2502\u00a0\u00a0 \u251c\u2500\u2500 validations.rb\n    \u2502\u00a0\u00a0 \u2514\u2500\u2500 version.rb\n    \u2514\u2500\u2500 active_resource.rb<\/code><\/pre>\n

ActiveResource\u306f\u30d5\u30a1\u30a4\u30eb\u304c\u5c11\u306a\u3044\u306e\u3067\u3059\u3054\u304f\u898b\u3084\u3059\u304f\u3001rails\u521d\u5fc3\u8005\u306e\u52c9\u5f37\u306b\u306f\u3061\u3087\u3046\u3069\u826f\u3055\u305d\u3046\u3067\u3059\u306d\uff01 <\/p>\n

PS<\/h2>\n

\u30d6\u30ed\u30b0\u3092\u66f8\u3044\u3066\u3057\u307e\u3063\u305f\u5f8c\u3067\u6c17\u4ed8\u3044\u305f\u306e\u3067\u3059\u304c\u3001 <\/p>\n

2.1.2 :060 > {'piyo1[]' => 'hoge1', 'piyo[]' => 'hoge2'}.to_query\n => "piyo%5B%5D=hoge2&piyo1%5B%5D=hoge1"<\/code><\/pre>\n

\u3053\u3046\u3059\u308b\u3068\u6e21\u305b\u307e\u3057\u305f...
\u307e\u3041\u3067\u3082\u3061\u3087\u3063\u3068\u4f7f\u3044\u3084\u3059\u304f\u306a\u3063\u305f\u3068\u601d\u3044\u307e\u3059\u306e\u3067\u826f\u3057\u3068\u3057\u3066\u4e0b\u3055\u3044...<\/p>\n

Ruby on Rails\u3092\u6d3b\u7528\u3057\u305fWeb\u30b5\u30fc\u30d3\u30b9\u3084\u696d\u52d9\u30b7\u30b9\u30c6\u30e0\u958b\u767a<\/a>\u3092\u3054\u691c\u8a0e\u306e\u4f01\u696d\u69d8\u306f\u3001\u662f\u975eMMM\u306b\u3054\u76f8\u8ac7\u4e0b\u3055\u3044\u307e\u305b\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5148\u65e5\u793e\u5185\u306e\u300c\u696d\u52d9\u52b9\u7387\u5316\u5468\u8fba\u30b0\u30c3\u30ba\u8cfc\u5165\u88dc\u52a9\u300d\u5236\u5ea6\u3092\u5229\u7528\u3057\u3066\u3001mac\u7528\u306b\u30e2\u30d0\u30a4\u30eb\u5145\u96fb\u5668HyperJuice\u3092\u8cfc\u5165\u3057\u3001\u914d\u7dda\u5730\u7344\u306b\u306a\u3063\u3066\u3044\u308b\u524d\u7530\u3067\u3059\u3002 \u672c\u65e5\u306fRuby On Rails\u306eGem\u3001AcitiveResource\u3092\u89e6\u3063\u3066\u307f\u305f\u306e\u3067\u3001\u305d\u306e\u304a\u8a71\u3092\u3057\u307e\u3059\u3002 \u74b0\u5883Rails 4.1.5\u3001ruby 2.1.2\u3001activeresource-4.0.0 ActiveResorce\u3068\u306f\u300cWeb\u4e0a\u306eRESTfu […]<\/p>\n","protected":false},"author":1,"featured_media":826,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[42],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/135"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":0,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media\/826"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}