Last active
April 22, 2018 14:02
-
-
Save enpedasi/c42f01e0424eb7d3b4ec048aa2874500 to your computer and use it in GitHub Desktop.
Recruit Hot Pepper API for Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule HotPepperApi do | |
@hit_per_page 50 | |
def get_shops({addr, freeword}) do | |
iget_shops({addr, freeword}, 1, []) | |
end | |
# ページング:最終ページ | |
def iget_shops({_addr, _freeword}, _offset, [%{:islast => true}|result] ) do | |
result | |
end | |
def iget_shops({addr, freeword}, offset, res_shops) do | |
page_max_cnt = @hit_per_page | |
res = | |
HTTPoison.get!( access_url("http://webservice.recruit.co.jp/hotpepper/gourmet/v1/",[ | |
"address=" <> addr, | |
"keyword=" <> freeword, | |
"count=" <> to_string(page_max_cnt), | |
"start=" <> Integer.to_string((offset - 1)* page_max_cnt + 1) | |
]) ) | |
|> parse_json | |
|> case do | |
%{"error" => e} -> e | |
%{"results" => res} -> | |
res["shop"] | |
|> Enum.map( fn r -> { r["name"], "N/A", r["genre"]["catch"], r["open"] } end) | |
|> case do shop_list -> [ shops: shop_list ++ | |
case res_shops do | |
[] -> [] | |
_ -> res_shops[:shops] | |
end, | |
total_hit_count: res["results_available"], | |
hit_per_page: @hit_per_page, | |
hit_count: res["results_returned"], | |
offset: offset] end | |
end | |
# ページング:残行カウント | |
rest_count = res[:total_hit_count] - offset * res[:hit_per_page] | |
IO.inspect res: res | |
IO.inspect res_cnt: rest_count | |
iget_shops({addr, freeword}, offset+1, [%{:islast => (rest_count <= 0)}] ++ res) | |
end | |
def access_url(url_base, opts\\[]) do | |
api_key = Application.get_env(:groumet, :apis)[:recruit_api_key] | |
url_base | |
<> "?" | |
<> Enum.join( [ "format="<>"json", "key="<>api_key ] ++ opts, "&") | |
end | |
def parse_json( %HTTPoison.Response{status_code: 200, body: body} ) do | |
body | |
|> Poison.decode! | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment