Skip to content

Commit

Permalink
change Naver::Papago.romanization return value. replace s_first_name …
Browse files Browse the repository at this point in the history
…to first_name, a_items

to items
  • Loading branch information
kimsuelim committed Jun 26, 2017
1 parent 6a73f03 commit 5f81d6b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file.

## [unreleased]
- [breaking] JSON 출력 결과를 CamelCase 에서 underscore 변경
- [breaking] Naver::Papago.romanization 리턴값의 key 변경. s_first_name -> first_name,
a_items -> items

## [0.3.0] - 2017-06-26
- [new] 얼굴인식(Beta)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ file = File.open("tts.mp3", "wb") { |f| f.write(response) }
```ruby
response = Naver::Papago.romanization(query: "김수림")
=> #<Naver::ObjectifiedHash:70284972145960 {hash: {
"s_first_name"=>"",
"a_items"=>[
"first_name"=>"",
"items"=>[
{"name"=>"Kim Soorim", "score"=>"99"},
{"name"=>"Kim Soolim", "score"=>"97"},
{"name"=>"Kim Surim", "score"=>"78"},
{"name"=>"Kim Sulim", "score"=>"76"}
]}
}

puts response.a_items[0].name
puts response.items[0].name
# => "Kim Soorim"
```

Expand Down
3 changes: 2 additions & 1 deletion lib/naver/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ def connection

# Converts the response body to an ObjectifiedHash.
def self.parse(body)
body = body.to_underscore_keys
if body.is_a?(Hash)
body = body.to_underscore_keys
ObjectifiedHash.new(body)
elsif body.is_a?(Array)
body = body.to_underscore_keys
body.collect! { |e| ObjectifiedHash.new(e) }
elsif body
true
Expand Down
7 changes: 6 additions & 1 deletion lib/naver/papago.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ def translate(source:, target:, text:)
# @return [Hash] 변환 결과
def romanization(query:)
params = { query: query }
parse(JSON.parse(connection.get("/v1/krdict/romanization", params).body)["aResult"][0])
result = JSON.parse(connection.get("/v1/krdict/romanization", params).body)["aResult"][0]
if result
result["firstName"] = result.delete("sFirstName")
result["items"] = result.delete("aItems")
end
parse(result)
end
end
end
Expand Down
18 changes: 9 additions & 9 deletions test/papago_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ class PapagoTest < Minitest::Spec

it "romanization" do
response = Naver::Papago.romanization(query: "김수림")
response.s_first_name.must_equal "김"
response.a_items[0].name.must_equal "Kim Soorim"
response.a_items[0].score.must_equal "99"
response.first_name.must_equal "김"
response.items[0].name.must_equal "Kim Soorim"
response.items[0].score.must_equal "99"

response.a_items[1].name.must_equal "Kim Soolim"
response.a_items[1].score.must_equal "97"
response.items[1].name.must_equal "Kim Soolim"
response.items[1].score.must_equal "97"

response.a_items[2].name.must_equal "Kim Surim"
response.a_items[2].score.must_equal "78"
response.items[2].name.must_equal "Kim Surim"
response.items[2].score.must_equal "78"

response.a_items[3].name.must_equal "Kim Sulim"
response.a_items[3].score.must_equal "76"
response.items[3].name.must_equal "Kim Sulim"
response.items[3].score.must_equal "76"
end
end

0 comments on commit 5f81d6b

Please sign in to comment.