Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
Change Tumblife::API helper methods to use active_support/core_ext.
Browse files Browse the repository at this point in the history
  • Loading branch information
rummelonp committed Jan 8, 2012
1 parent 82a2f9c commit 9be202e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 27 deletions.
18 changes: 5 additions & 13 deletions lib/tumblife/api.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-

require 'active_support/core_ext/object/to_query'
require 'active_support/core_ext/hash/keys'

module Tumblife
class API
# @private
Expand All @@ -15,13 +18,13 @@ def initialize(options = {})

# Perform an HTTP GET request
def get(path, params = {})
response = access_token.get(path + '?' + parse_params(params), header)
response = access_token.get(path + '?' + params.to_param, header)
parse_response(response)
end

# Perform an HTTP POST request
def post(path, params = {})
response = access_token.post(path, stringify_params(params), header)
response = access_token.post(path, params.stringify_keys, header)
parse_response(response)
end

Expand All @@ -37,17 +40,6 @@ def access_token
OAuth::AccessToken.new(consumer, oauth_token, oauth_token_secret)
end

def parse_params(params = {})
params.map {|k, v| k.to_s + '=' + CGI.escape(v.to_s)}.join('&')
end

def stringify_params(params = {})
params.inject({}) do |h, (k, v)|
h[k.to_s] = v.to_s
h
end
end

def header
{'User-Agent' => user_agent}
end
Expand Down
14 changes: 0 additions & 14 deletions spec/tumblife/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,4 @@ def create_response(status, msg)
}.should raise_error(Tumblife::APIError)
end
end

context :helpers do
before do
@params = {name: 'mitukiii', age: 23}
end

it 'should convert query string from Hash' do
@api.parse_params(@params).should == 'name=mitukiii&age=23'
end

it 'should stringify keys from Hash' do
@api.stringify_params(@params).should == {'name' => 'mitukiii', 'age' => '23'}
end
end
end
1 change: 1 addition & 0 deletions tumblife.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency("oauth", "~> 0.4.5")
s.add_runtime_dependency("json", "~> 1.6.4") if RUBY_VERSION < "1.9"
s.add_runtime_dependency("hashie", "~> 1.2.0")
s.add_runtime_dependency("activesupport", "~> 3.1.3")
s.add_development_dependency("rspec", "~> 2.8.0")

s.files = `git ls-files`.split("\n")
Expand Down

0 comments on commit 9be202e

Please sign in to comment.